Template Troubles (Solved)
-
I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?
-
I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?
Well, your syntax for using templates is wrong. The template declaration is as follows:
template<class mytype, ...>
class MyClass
{
...This allows you to use a type called mytype or whatever. The class' member functions should be declared as follows:
template<class mytype, ...>
MyClass<mytype, ...>::MemberFunctionFinally, declare instances of the template class like this:
myClass<type (int, class, CString, whatever)> myVar;
This is the worst explanation of template syntax ever. You should read the MSDN documentation of templates. I'm surprised the code got through the compiler.
-
I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?
First, you probably should send the source again with the "Display this message as-is (no HTML)" checked, so that it can be read. Second, you need to have the template code available when you are compiling main.cpp - if you just have the template code in test.cpp but no actual code using it for a specific type, nothing gets expanded. Typically, you would put your template code in the test.h include file. You should not have a test.cpp file for this template class. Any source file that needs to use the templated class can then include test.h, and the templated functions will be expanded as needed. Best regards, John
-
Well, your syntax for using templates is wrong. The template declaration is as follows:
template<class mytype, ...>
class MyClass
{
...This allows you to use a type called mytype or whatever. The class' member functions should be declared as follows:
template<class mytype, ...>
MyClass<mytype, ...>::MemberFunctionFinally, declare instances of the template class like this:
myClass<type (int, class, CString, whatever)> myVar;
This is the worst explanation of template syntax ever. You should read the MSDN documentation of templates. I'm surprised the code got through the compiler.
-
I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?
Sorry this is my first post and I did not notice the <> disapearing. I found the answer to my problem at another forum. Thanks for the help.
-
it's because everything between a pair of tags, <>, is interpreted as an html tag and therefore remove cause he forgot to turn off html interpretation.
Hehe!;)
-
Sorry this is my first post and I did not notice the <> disapearing. I found the answer to my problem at another forum. Thanks for the help.
If you post your answer, then other people can learn from this post as well. What was your solution?
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life! -
I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?
That isn't specific to VC6. I don't even know of a compiler that supports templates where the definition isn't fully known to the module in question. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
-
I am using a template class and everyithing works fine until I try to break my code into multiple files. Here is a minimal test case that demonstrates my problem. I am using Visual C++ 6 SP 5 Window XP. ******Solved************* Visual C++ 6 requires that the implementation and declaration of a template reside in the same file. I can either put everything in the .cpp file or the .h but it has to be in one file. //Onefile.cpp #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } This compiles links and executes just fine. Here is the problem. //Test.h #include #include using namespace std; template class Test { public: // constructors Test(int size); ~Test() {}; // opertors T & operator[](int offset) { return itsVector[offset];} Show(); vector itsVector; int itsLength; }; //test.cpp #include "Test.h" template Test::Test(int size) : itsLength(size), itsVector(size) { for (int i = 1; i itsVector[i] = i; } template Test::Show() { for (int i = 1; i < itsLength; i++) cout << itsVector[i] << ((i == itsLength-1) ? ('\n') : (',')); } //main.cpp #include #include "Test.h" using namespace std; int main() { Test v(4); v.Show(); v[1] = 9; v.Show(); return 0; } When I try to build I get the following linking error. main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Test::Show(void)" (?Show@?$Test@H@@QAEHXZ) main.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(int)" (??0?$Test@H@@QAE@H@Z) Debug/TestTemplate.exe : fatal error LNK1120: 2 unresolved externals All the errors disapear if quit using templates. Does anyone know what I am doing wrong?
The solution is to put the implementation in the .h file. I tried putting it in a .cpp and VC++ added it to the project resulting in multiple definitions. I even tried inclusion guards (#ifndef). So itstead I left it in the .h, .txt workes as well (I really don't like putting the implementation in a header file).