What's wrong with my code? (class template)
-
I have the following codes, all of them are in the same file "test.cpp" //declaration template <class T> class temp { public: class nested { public: void test(); }; void test() { nested nes; nes.test(); } }; //definition template <class T> void temp<T>::nested::test() {} int main( /* int argc, char* argv[] */ ) { temp<int> tem; tem.test(); return 0; } error message: test.obj : error LNK2001: unresolved external symbol "public: void __thiscall temp<int>::nested::test(void)" (?test@nested@?$temp@H@@QAEXXZ) test.exe : fatal error LNK1120: 1 unresolved externals what's wrong with my code? thanks :(( Lirong
-
I have the following codes, all of them are in the same file "test.cpp" //declaration template <class T> class temp { public: class nested { public: void test(); }; void test() { nested nes; nes.test(); } }; //definition template <class T> void temp<T>::nested::test() {} int main( /* int argc, char* argv[] */ ) { temp<int> tem; tem.test(); return 0; } error message: test.obj : error LNK2001: unresolved external symbol "public: void __thiscall temp<int>::nested::test(void)" (?test@nested@?$temp@H@@QAEXXZ) test.exe : fatal error LNK1120: 1 unresolved externals what's wrong with my code? thanks :(( Lirong
Lirong - Pls try reposting with "<" and ">" for the < and > symbols. Some key statements are missing.. But I think I might know the answer, some compilers are really anal about this stuff. They expect all template functions to be defined in one file (.h or .cpp), in other words ONE Translation Unit.. (boo hoo !! ) I suspect VC++ is one of those compilers. ( Maybe an expert can confirm). So, try writing the "test" function of the inner class within the class defn. things should be ok. Eg. ... class nested { public: void test() { // put your function body here }; ... Hooe this helps- Vivek
-
Lirong - Pls try reposting with "<" and ">" for the < and > symbols. Some key statements are missing.. But I think I might know the answer, some compilers are really anal about this stuff. They expect all template functions to be defined in one file (.h or .cpp), in other words ONE Translation Unit.. (boo hoo !! ) I suspect VC++ is one of those compilers. ( Maybe an expert can confirm). So, try writing the "test" function of the inner class within the class defn. things should be ok. Eg. ... class nested { public: void test() { // put your function body here }; ... Hooe this helps- Vivek
Thanks. ;) I have made some correctness on my question; Yes, If I write the definition of "test" of the nested class within the class declaration, it does work. So my question is: Is it possible to separate the definition and declaration of a function of a nested class ( which is nested in a template ). Is there anything wrong with my syntax? Or it is just because of the compiler(vc++6)? Thanks. Lirong
-
Thanks. ;) I have made some correctness on my question; Yes, If I write the definition of "test" of the nested class within the class declaration, it does work. So my question is: Is it possible to separate the definition and declaration of a function of a nested class ( which is nested in a template ). Is there anything wrong with my syntax? Or it is just because of the compiler(vc++6)? Thanks. Lirong
-
Thanks. ;) I have made some correctness on my question; Yes, If I write the definition of "test" of the nested class within the class declaration, it does work. So my question is: Is it possible to separate the definition and declaration of a function of a nested class ( which is nested in a template ). Is there anything wrong with my syntax? Or it is just because of the compiler(vc++6)? Thanks. Lirong
Problem is that The compiler does not generate code for the member function in the nested class template. It is a bug with MS VC <= 6.0 see here "ID: Q128789" To work around it do this define your class internally to the nested class
#include #include "header.h"
//declaration
template class temp
{
public:
class nested
{
public:
void test()
{
cout << "Hello world"<< endl;
}
};void test() { nested nes; nes.test(); }
};
int main( /* int argc, char* argv[] */ )
{
temp tem;
tem.test();
return 0;
}Hope that helped. Cheers Alfadhly It is Illogical to define an inventor by his invention
-
Problem is that The compiler does not generate code for the member function in the nested class template. It is a bug with MS VC <= 6.0 see here "ID: Q128789" To work around it do this define your class internally to the nested class
#include #include "header.h"
//declaration
template class temp
{
public:
class nested
{
public:
void test()
{
cout << "Hello world"<< endl;
}
};void test() { nested nes; nes.test(); }
};
int main( /* int argc, char* argv[] */ )
{
temp tem;
tem.test();
return 0;
}Hope that helped. Cheers Alfadhly It is Illogical to define an inventor by his invention
Thank all.
;)
I have try it in C++ Builder. It runs well there.
I could have defined the function within the class declaration. However, I am not able to do that. The reason is illustrated by the following example. In the example, A<T>::nested uses B<T> and B<T>::nested used A<T>.
// begin of A.h
#pragma once// foreword declaration, but we still can not use class B.
//that is why I can't put the definition of A<T>::nested::test() inside the declaration;template <class T> class B;
template <class T> class A
{
public:
class nested
{
public:
void test(B<T> b);
};
nested GetNested() const
{
return nested();
}
}
#include A.imp
// end of A.h
<
-
I have the following codes, all of them are in the same file "test.cpp" //declaration template <class T> class temp { public: class nested { public: void test(); }; void test() { nested nes; nes.test(); } }; //definition template <class T> void temp<T>::nested::test() {} int main( /* int argc, char* argv[] */ ) { temp<int> tem; tem.test(); return 0; } error message: test.obj : error LNK2001: unresolved external symbol "public: void __thiscall temp<int>::nested::test(void)" (?test@nested@?$temp@H@@QAEXXZ) test.exe : fatal error LNK1120: 1 unresolved externals what's wrong with my code? thanks :(( Lirong
You can't do the implementation of temp::nested::test() like you're doing. Even though Bjarne say's it's possible (C++ 3rd edition), the VC++ compiler does NOT support this. All the implementation MUST be in line - i.e. //declaration template class temp { public: class nested { public: //this must be implemented here void test() { //whatever } }; void test() { nested nes; nes.test(); } }; Hope this helps ! Jim Crafton