Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C++ template class linking issue

C++ template class linking issue
Thread Tools
GeeYouEye
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status: Offline
Reply With Quote
Feb 13, 2006, 01:20 PM
 
Hopefully someone can figure out what's going on... I sure can't...

in main.cpp:
Code:
#include <iostream> #include <fstream> #include <string> #include "symtab.h" #include "OpCode.h" using namespace std; int main(int argc, char **argv) { ... }
in OpCode.h:
Code:
#ifndef OPCODE_H #define OPCODE_H #include <string> using namespace std; class OpCode { public: OpCode(string mnem); OpCode(string mnem, char hex, int bytes, string otherInfo); string getMnem() { return iMnem;} char getHex() {return iHex;} int getBytes() {return length;} string getOtherInfo() {return info;} private: string iMnem; char iHex; int length; string info; }; #endif
in OpCode.cpp
Code:
OpCode::OpCode(string mnem){iMnem = mnem;} OpCode::OpCode(string mnem, char hex, int bytes, string otherInfo) { iMnem = mnem; iHex = hex; length = bytes; info = otherInfo; }
Note that all the other functions in OpCode are defined inline.

in symtab.h (where I suspect I'm missing something):
Code:
#ifndef SYMTAB_H #define SYMTAB_H #include <string> using namespace std; template <class AT> class Item { public: Item(string s); string getname(); // returns the name AT attr; // attributes other than name private: string name; }; template <class AT> struct Node{ Item<AT> * info; Node<AT> * link;}; template <class AT> class SymTab { public: SymTab(); //initializes to the empty table Item<AT> * find(string s); //returns Item pointer or null Item<AT> * insert(string s); //inserts if necessary and //returns Item pointer ~SymTab(); //This seems almost more necessary than the constructor private: Node<AT> hashTab[137]; int hash(string s); Node<AT> * cons(Item<AT> * x, Node<AT> * y); //returns a new list node // with x in the info field and y in the link field }; #endif
and in symtab.cpp
Code:
#include "symtab.h" template <class AT> Item<AT>::Item(string s) { name = s; } template <class AT> string Item<AT>::getname() { return name;} // returns the name template <class AT> SymTab<AT>::SymTab() { } template <class AT> Item<AT> * SymTab<AT>::find(string s) { ... } template <class AT> Item<AT> * SymTab<AT>::insert(string s) { ... } template <class AT> int SymTab<AT>::hash(string s) { ... } template <class AT> Node<AT> * SymTab<AT>::cons(Item<AT> *x, Node<AT> * y) { Node<AT> * ret = new Node<AT>; ret->info = x; ret->link = y; return ret; } template <class AT> SymTab<AT>::~SymTab() { ... }
It compiles fine, but the linker is choking somewhere. Specifically, I get
Originally Posted by /usr/bin/ld
Undefined symbols:
Item<OpCode>::getname()
SymTab<OpCode>::find(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
SymTab<OpCode>::insert(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
SymTab<OpCode>::SymTab()
SymTab<OpCode>::~SymTab()
collect2: ld returned 1 exit status
Everything's defined (although the SymTab constructor doesn't do anything since the only actual data is a fixed array), everything's properly included as far as I can tell... this is driving me nuts. Someone tell me I'm misspelling something somewhere.
I bring order to chaos. You are in chaos windows, you are the contradiction, a bug wishing to be an OS.
     
GeeYouEye  (op)
Junior Member
Join Date: Mar 2003
Location: Walnut Creek, CA
Status: Offline
Reply With Quote
Feb 13, 2006, 01:31 PM
 
Okay, disregard. Of everything to forget, I forget that you can't separate template class interface and implementation.
I bring order to chaos. You are in chaos windows, you are the contradiction, a bug wishing to be an OS.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:04 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,