Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 13, 2006, 11:55 AM   #1
GeeYouEye
macrumors 68000
 
GeeYouEye's Avatar
 
Join Date: Dec 2001
Location: State of Denial
Send a message via AIM to GeeYouEye Send a message via Yahoo to GeeYouEye
C++ template class linking issue

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
Quote:
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.
Visit Softyards Software
NEW DEFINITION OF GEEK
Like politics, free speech, computers, entertainment, and more? Join us at Wordforge.net
GeeYouEye is offline   0 Reply With Quote
Old Feb 13, 2006, 12:38 PM   #2
zimv20
macrumors 601
 
zimv20's Avatar
 
Join Date: Jul 2002
Location: chicago
been a while since i've done this stuff, but...

are you calling the linker properly? it looks like it doesn't know about OpCode.o and symtab.o.

also, from the coding sign, i guess i'm a little suprised that you have template classes that don't have constructors taking AT as an argument.
__________________
Oct 2011: check out my band's first album @ boxsetauthentic.com
zimv20 is offline   0 Reply With Quote
Old Feb 13, 2006, 01:40 PM   #3
iSee
macrumors 68040
 
iSee's Avatar
 
Join Date: Oct 2004
All template function definitions need to be in in the header file, either with the class delcaration or inlined after the class declaration.

That is, everything in symtab.cpp needs to be put in symtab.h, either embedded in the class declaration or inline'd afterwards.

Why? In order for a template class to be instantiated for a particular type, ALL the code needs to be available to the compiler. At their heart, templates are automated copy, seatch and replace engines. When you use SymTab<OpCode>, the compiler basically copies everything to do with SymTab from the header file and replaces AT with OpCode and then compiles it. It doesn't have access to the code in symtab.cpp, because that isn't included.
iSee is offline   0 Reply With Quote
Old Feb 13, 2006, 01:40 PM   #4
SamMiller0
macrumors member
 
Join Date: Aug 2004
Location: San Jose, CA
Please post a tarball of your code and a makefile, I would like to take a look at it.
SamMiller0 is offline   0 Reply With Quote
Old Feb 13, 2006, 01:56 PM   #5
zimv20
macrumors 601
 
zimv20's Avatar
 
Join Date: Jul 2002
Location: chicago
Quote:
Originally Posted by iSee
All template function definitions need to be in in the header file
ahh, yeah, that's it. i forgot about that.
__________________
Oct 2011: check out my band's first album @ boxsetauthentic.com
zimv20 is offline   0 Reply With Quote
Old Feb 15, 2006, 05:40 AM   #6
GeeYouEye
Thread Starter
macrumors 68000
 
GeeYouEye's Avatar
 
Join Date: Dec 2001
Location: State of Denial
Send a message via AIM to GeeYouEye Send a message via Yahoo to GeeYouEye
Yep, it was a combination of splitting it into two files (one bit of C++ I'd forgotten) and the templated constructor issue. Now to just get the rest of this code working...
__________________
I bring order to chaos. You are in chaos Windows, you are the contradiction, a bug wishing to be an OS.
Visit Softyards Software
NEW DEFINITION OF GEEK
Like politics, free speech, computers, entertainment, and more? Join us at Wordforge.net
GeeYouEye is offline   0 Reply With Quote
Old Feb 15, 2006, 06:14 AM   #7
GeeYouEye
Thread Starter
macrumors 68000
 
GeeYouEye's Avatar
 
Join Date: Dec 2001
Location: State of Denial
Send a message via AIM to GeeYouEye Send a message via Yahoo to GeeYouEye
And on that note, I'm trying to read in a hex value from a file. currently, I'm using ifstream infile; infile.open("file.txt"); infile >> hex >> myChar; and the 18 is only being read as 1, with the 8 being parsed later and leading to all sorts of screwups.

Dang how I wish I could just use the Foundation framework.
__________________
I bring order to chaos. You are in chaos Windows, you are the contradiction, a bug wishing to be an OS.
Visit Softyards Software
NEW DEFINITION OF GEEK
Like politics, free speech, computers, entertainment, and more? Join us at Wordforge.net
GeeYouEye is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Referencing a child class in a parent class, good or bad? SAIRUS iPhone/iPad Programming 11 Aug 28, 2013 12:59 PM
Mid 2013 Macbrick Air wi-fi issue-class action lawsuit time! grh2013 MacBook Air 77 Aug 24, 2013 02:29 PM
Class issue mystudioos Mac Programming 8 Jul 23, 2013 09:55 AM
Strange Chrome linking issue in Joomla! rainman::|:| Web Design and Development 3 Jun 12, 2013 08:31 PM
Difference between Public class and private class. zijianz iPhone/iPad Programming 4 Jun 29, 2012 10:29 PM

Forum Jump

All times are GMT -5. The time now is 08:50 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC