LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c++ template help (https://www.linuxquestions.org/questions/programming-9/c-template-help-255435/)

ashirazi 11-16-2004 03:32 AM

c++ template help
 
Hi,

Ive been yanking my brains on this stupid error since 6 hrs streight, and I havent reached no where. Can some guru please help me out....? Im sure its something cause I get this freaking referencing error even when eventhing (except the construcor) has been comented.

Thanks guys


Code:


//  BinomialNode.h

#ifndef BINOMIALNODE_H
#define BINOMIALNODE_H
       
#include <iostream>
       
template <typename Type>
class BinomialNode {
public:
        BinomialNode( );
}
#endif



Code:

#include "BinomialNode.h"
#include <iostream>

using namespace std;
template BinomialNode<int>;

/*
*      Default Constructor
*      PRE:    none
*      POST:  Instance of BinomialNode with no siblings,
*                      children or parent.
*
*/
template <typename Type>
BinomialNode<Type>::BinomialNode(){
}


Code:

//
//        ERROR MESSAGE
//
g++ -R/util/gnu/lib -Wno-non-template-friend  -g -c Driver.cpp
g++ -R/util/gnu/lib -Wno-non-template-friend  -g -o Driver Driver.o 
Undefined                      first referenced
 symbol                            in file
BinomialNode<int>::BinomialNode[in-charge]()Driver.o
ld: fatal: Symbol referencing errors. No output written to Driver
collect2: ld returned 1 exit status
gmake: *** [Driver] Error 1


kev82 11-16-2004 04:46 AM

havnt done anything with templates in a while but if i remember right any templated member functions must be in the header file, not the implementation file, im sure someone whos done stuff more recently can either correct or elaborate on that though.

rjlee 11-16-2004 05:13 AM

This is a linker error, which means that you're using a symbol that you haven't defined.

First of all, in any program, you need one of the following functions; it won't link without exactly one of these:
Code:

int main();
int main(int argc);
int main(int argc, char** argv );
int main(int argc, char** argv, char** envp);

The symbol it's complaining about is the BinomialNode<int>::BinomialNode() constructor; since you haven't used BinomialNode<int> anywhere in your code, it's difficult to see why this error occurs.

You might try moving the implementation to the header file; though I don't recall that limitation myself, there is a thread on it here: http://www.linuxquestions.org/questi...light=template

deiussum 11-16-2004 09:17 AM

kev82 is correct. Templated classes must be implemented completely in the header file. The link in the response above discusses a bit more on why that is...

ashirazi 11-16-2004 09:52 AM

Thanks for the replies guys, I finally got it working. The issue wasnt only the "int main()" thing, but it seems i also had to implement the .cpp file at the end of the header file. Found this tip on one of the websites. Thanks a lot for the replies....


All times are GMT -5. The time now is 11:38 PM.