Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class templates

Status
Not open for further replies.

McBugzz

Programmer
Joined
Sep 17, 2002
Messages
90
Location
JP
I have smth like this:

template <class T, int i>
class Array
{
public:
Array( void );
~Array();
private:
//...
};

Then, when I try to create an object of this class in the main() function, I get a linker error: unresolved external symbol: ...the constructor... ...the destructor... ...all other members... Why?

Looks like this:

Array<int, 10> myArray; // This is what causes the linker error

The most interesting thing is that if I make
struct data
{
int i;
};
and then call
Array<data, 10> myArray;
eveything works.

The exact code can be found here:
 
>> Array<int, 10> myArray; // This is what causes the
>> linker error

Because the template definition for Array does not define a default constructor, so change your template to this:

Array(){}
~Array(){}


>> The most interesting thing is that if I make

That's because the data structure has a default constuctor provided by the compiler where a template does not.

Hope this helps
-pete
 
No, that's not the problem. If you've looked at the source code, which I provided a link to, you would have noticed that I do have a default constructor there. It's body is in the SortArray.cpp.
 
Yep, by bad. We just had this same problem in the C++;Microsoft forum. According to chipperMDW you have to use the export keyword to seperate your implementation code from the template declaration and VC does not support it.

The whole thread is here: thread207-411782

-pete
 
That thread looks familiar!

In my case, I just moved the function definitions into the header file and my problems were solved.

McBugzz,

It would help to post the source code. People like me won't open a zip file from an unknown person.

I assume you are trying to create a 10 element array of ints with Array< int, 10 >. I wonder if something funky goes on in the initialization, since there doesn't appear to be a default value. Then again I can only guess at this point.
 
I've read the thread in the Microsoft C++ forum, so everything's pretty much clear now.

Thanx everybody
 
Hi all,
I went thru the c++ forum thread. But how McBugzz is not getting error when he is simply declaring a data struct of int(Have not included definitions in driver).
 
I have no idea, because this just doesn't fit into the whole picture.

I had no problems using the stuff, mentioned above, with a struct or a class, but probably this is playing the odds.
 
McBugzz, sskumar,

>> how McBugzz is not getting error when he is simply
>> declaring a data struct

That's because in C++ a struct is a class so there is a compiler supplied default constructor. Change the struct to have a protected default constructor and you will get an error.

struct data{
int i;
protected:
data(){}
};


Hope this helps
-pete
 
Ok, but how does it change the whole linking process?

I mean, what's the big difference for the linker between finding a &quot;local symbol&quot; when we use int type or data struct?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top