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.

OliKlima
  • 41
  • 6

2 Answers2

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
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