Reputation: 144
I Have the following class definition in a header file I copied from the net
#ifndef A2DD_H
#define A2DD_H
class A2DD
{
int gx;
int gy;
public:
A2DD();
A2DD(int x,int y);
};
#endif
And in the implementation file i have `
#include "A2DD.h"
A2DD::A2DD()
{
}
A2DD::A2DD(int x,int y)
{
gx = x;
gy = y;
}
Now the problem is that i get the following error when the constructor with parameters are called.
Info :Building...
Info :Compiling C:\Users...\Desktop\main.cpp
Info :Linking C:\Users...\Desktop\main.exe
Error: Error: Unresolved external 'A2DD::A2DD(int,int)' referenced from C:\USERS...\DESKTOP\MAIN.OBJ
#include "A2DD.h"
int main()
{
A2DD add(2,3);
return 0;
}
However when A2DD add();
is called without parameters the program works fine.
Now you might be wondering why I copied code from the internet, well I have a programming assignment for some electrical engineering course that involves classes and we can only use Borland and yes the code worked in visual c++...helpp please
Upvotes: 1
Views: 335
Reputation: 56903
You need to compile and link the file where you implement the constructors (A2DD.cpp
). You need to tell Borland that this is an implementation file that belongs to your project.
What you misunderstood is this:
A2DD add();
It does not call the default constructor, it only declares a function called add
which returns a A2DD
and has no parameters. This function declaration therefore basically does nothing in your code and the default constructor is never called.
If you want to create an object with the default constructor, you need
A2DD add;
Upvotes: 4