...

View Full Version : Issues when placing functions from template class into seperate .cpp file [C++]



Shaitan00
07-29-2009, 07:04 AM
I'm having an odd problem writing some template classes and placing the implementation in a separate .CPP file - specifically I keep getting the following errors:
Error 1 error C2143: syntax error : missing ';' before 'List<T>::begin'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Here is my dummy test code:
(proof of concept)

-= HEADER FILE =-


template<typename T> class List
{
public:
class A
{
};

A begin() const;
};



-= IMPLEMENTATION FILE =-


template<typename T> A List<T>::begin() const
{
return A();
}


Now, if I place this code directly into the header (.h) file it compiles without any errors, I only encounter issues when placing my functions within the separate implementation (CPP) file...

Any clues would be much appreciated.
Thanks,

oracleguy
07-29-2009, 07:31 AM
See: http://www.experts-exchange.com/articles/Programming/Languages/CPP/Seperating-C-template-declaration-and-implementation.html

But I personally think when it comes to templated classes, putting it all in the header file is nicer because they are usually generic objects that get reused and having one file is easier.

Shaitan00
07-30-2009, 06:26 AM
Sounds good to me, and I don't mind keeping everything within the header (that much) but I would still like to segment the implementation - like moving it to the bottom (as Alex F recommended) - but it still doesn't work...

If I include everything in the single .h file (as shown below) I get a new set of errors:
error C2143: syntax error : missing ';' before 'List<T>::begin'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

As well as the following warning
warning C4346: 'List<T>::A' : dependent name is not a type



template<typename T> class List
{
public:
class A
{
};

A begin() const;
};

template<typename T> List<T>::A List<T>::begin() const
{
return A();
}


It really only seems to work if I put the implementation directly in the class itself... which makes the class defintion rather busy and hard to read...

Any clues what could be going wrong?
Thanks,

ralph l mayo
08-11-2009, 12:16 AM
Try:


template<typename T> typename List<T>::A List<T>::begin() const



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum