0

I have an unresolved external in Visual Studio C++ compiler that's driving me absolutely crackers. The class header and source files are extremely simple.

Header file:

// Header file: Rational.h
class Rational
{
public:
    Rational ( int = 0, int = 1 ); 
private:
    int numerator;
    int denominator;    
};

Source file:

// Source file: Rational.cpp
#include <iostream>
#include "Rational.h"

using namespace std;

Rational::Rational( int n, int d )
{
    numerator = n;
    denominator = d;
}

The error messages are:

error LNK2019: unresolved external symbol _main referenced in function    ___tmainCRTStartup    

error LNK1120: 1 unresolved externals

How is this possible? I must be missing something very fundamental here but now I am at the end of my tether.

I have looked at other questions on this topic but can't find an answer.

4
  • 7
    Have you got a main()? Commented Jan 5, 2014 at 21:39
  • You forgot to define the entry point... create a new file and add int main(int argc, char **argv) Commented Jan 5, 2014 at 21:40
  • @OliKlima Well that is pretty important Commented Jan 5, 2014 at 21:42
  • Apologies all I didn't realise I needed main. This is fixed now. Thanks, and apologies for the silly question. Commented Jan 5, 2014 at 21:43

2 Answers 2

4

Have you got a main()? – Alan Stokes
@ Alan. Not yet. – OliKlima

Well, there you go then.

It's the main that's not being found, as the error message pretty much states.

Sign up to request clarification or add additional context in comments.

Comments

1

If you're trying to link Rational.cpp into an executable, it needs an entry point (main). If you're simply creating an object file, then there is no linking done. Your entry point can be placed in another file, i.e main.cpp, and then link it together.

First, put this in main.cpp:

int main()
{
}

Then run the following commands:

g++ -c Rational.cpp
g++ -c main.cpp
g++ -o main main.o rational.o

1 Comment

Thanks, I won't be making that mistake again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.