Connecting Tech Pros Worldwide Forums | Help | Site Map

C++ Constructor help. Please help.

Newbie
 
Join Date: Oct 2008
Posts: 2
#1: Oct 14 '08
Can someone please show me how I would create these constructors in the source file...my compiler tells me I have errors. I know I set up my main, header, and source file correctly because I am getting no errors until I start implementing the constructors.

In the HEADER file:(xArray.h)
class xArray{
public:
//constructors
xArray();
xArray(const xArray& a);
xArray(const int x);
private:
size_t len;
size_t size;
//data of int array
int *data;
};

In the source file: (xArray.cpp)

//this is the constructor I am unsure about
xArray::xArray(const xArray& a){
// ? ? ? ? ? ? ? ? ?
}


//these two constructors I believe are correct...my compiler does not return any
//errors

//constructor 1
xArray::xArray(){
len = 0;
size = 0;
data = new int[size];
}

//constructor 3
xArray::xArray(const int x){
len = 0;
size = x;
data = new int[x];
}

All help is greatly appreciated.

gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#2: Oct 14 '08

re: C++ Constructor help. Please help.


What error you are getting?

Raghu
Newbie
 
Join Date: Oct 2008
Posts: 2
#3: Oct 14 '08

re: C++ Constructor help. Please help.


Quote:

Originally Posted by gpraghuram

What error you are getting?

Raghu

Ok, I think I've created all three of my constructors correctly now: But when I try to create a new xArray in my MAIN file (xArray a;), I am getting an error: (the error is posted below after the constructors which are directly below this.)

//constructor 1
xArray::xArray(){
len = 0;
size = 0;
data = new int[size];
}

//constructor 2
xArray::xArray(const xArray& a){
len = a.len;
size = len*2;
data = new int[size];
}

//constructor 3
xArray::xArray(const int x){
len = 0;
size = x;
data = new int[x];
}



//MAIN.cpp

cout << "Testing the constructors." << endl;
xArray a; //trying to create an xArray

HERE IS THE ERROR I AM GETTING: (USING VISUAL BASIC C++):
MAIN.obj : error LNK2019: unresolved external symbol "public: _thiscall xArray::~xArray(void)" (??1xArray@@QAE@XZ) referenced in function_main
MAIN.exe : fatal error LNK1120: 1 unresolved externals
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#4: Oct 15 '08

re: C++ Constructor help. Please help.


Have you declared the destructor in header and not defined the body for it?

Raghu
Reply