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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Templated Classes in MS Visual C++ 6.0

Status
Not open for further replies.

Zaid

Programmer
Jul 21, 2000
6
CA
I'm trying to create a simple, templated binary tree class in Microsoft Visual C++ 6.0. I've done this before using a Borland Compiler (I forget which version exactly) and it worked fine, but with MS Visual C++, I get linking errors that look like this:<br><br>ttest.obj : error LNK2001: unresolved external symbol &quot;public: __thiscall TREE&lt;int&gt;::~TREE&lt;int&gt;(void)&quot; (??1?$TREE@H@@QAE@XZ)<br><br>ttest.obj : error LNK2001: unresolved external symbol &quot;public: void __thiscall TREE&lt;int&gt;::Destroy(void)&quot; (?Destroy@?$TREE@H@@QAEXXZ)<br><br>I get one of these linking errors for every member function in my class (including Constructor and Destructor). The errors disappear when I put a pair of brackets after the name of my object at instantiation (i.e. TREE&lt;int&gt; Animal( ) instead of TREE&lt;int&gt; Animal), but then all my member functions produce &quot;left of [function] must have struct/class/union&quot; compiling errors, understandably. <br><br>Also, I'm using both a *.h and a *.cpp file to define my class. Can anyone help?<br><br>Thanks,<br>Zaid<br><br>P.S.<br>Just in case, I threw in a sample function below:<br><br>template &lt;class ANYTYPE&gt;<br>bool TREE&lt;ANYTYPE&gt;::IsEmpty()<br>{<br>&nbsp;&nbsp;if (myRoot == NULL)<br>&nbsp;&nbsp;&nbsp;return true;<br>&nbsp;&nbsp;return false;<br>}<br><br>(from the *.cpp file)
 
Zaid,<br><br>It sounds like your .cpp file is not included in your project.<br><br>Tom
 
You know what, I had originally included the *.cpp file (include statement at the bottom of the *.h file, which is what I used to do when using the Borland Compiler), but that gave me errors that looked like this:<br><br>c:\ap\tree.cpp(257) : error C2084: function 'void __thiscall TREE::postOrderDisplay(void)' already has a body<br><br>When I commented out the include statement, it worked fine (well, until I ran into the aforementioned error). <br><br>Again, thanks for any help.
 
You know what, I figured out what was going on. I included my *.cpp file in the project (a no-no for templated classes - it should be at the bottom of the *.h file). Everything works fine now.<br><br>Zaid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top