Re: template class, methods and friend, unable to link
On Dec 17, 8:31 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
suresh.amritapuri wrote:
Hi,
This is my code:
//tree.h file
#ifndef TREE_H_
#define TREE_H_
#include "factorGraph.h"
template <typename Vertex>
class Tree{ [..]
};
#endif /* TREE_H_ */
//tree.cpp file
#include "tree.h"
template<typename Vertex>
ostream & operator<< <>(ostream& o, const Tree<Vertex>& t ){
[..] }
template<typename Vertex>
void Tree<Vertex>::test(){ cout << "Namasivayah" << endl;}
I compile like this:
g++ -c tree.cpp -o tree.o
and link with other object files in my project.
I get the following linker errors:
undefined reference to `Tree<vertex_properties>::test()'
undefined reference to `operator<<(std::basic_ostream<char,
std::char_traits<char> >&, Tree<vertex_properties> const&)'
collect2: ld returned 1 exit status
I tried including the tree.cpp file at the end of tree.h. But even
then operator<< function is not compiling.
How do I go further?
Read the FAQ section 35. Especially the "linker error" questions there=
..
It is always a good idea to read the FAQ before posting. You can find
the FAQ here:http://www.parashift.com/c++-faq-lite/
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Hi,
Thanks, but reading the faq did not help me :( Here is my modification
and still g++ cribs. has indicated error against the two offending
lines
Any suggestions pls?
suresh
//tree.h
#include "factorGraph.h"
template <typename Vertex>
class Tree{
public:
Tree(FactorGraph f):fg(f){}
//Vertex root(){return root;} //returns the root
void root(Vertex rt){rootVertex=rt;} //sets the root
friend ostream & operator<< (ostream& o, const Tree<Vertex>& );
void test();
private:
Vertex rootVertex;
FactorGraph fg;
};
//#include "tree.cpp"
#endif /* TREE_H_ */
//tree.cpp
#include "tree.h"
template<typename Vertex> class Tree;
template<typename Vertex> ostream & operator<<(ostream& o, const
Tree<Vertex>& t );
template<typename Vertex> void Tree<Vertex>::test(void); //error:
declaration of =91void Tree<Vertex>::test()' outside of class is not
definition
template<typename Vertex>
ostream & operator<<(ostream& o, const Tree<Vertex>& t ){
//o << t.fg << endl;
return o;
}
template<typename Vertex>
void Tree<Vertex>::test(){ cout << "Namasivayah" << endl;}
template class Tree<Vertex>;
template ostream& operator<<<Vertex>(ostream& o, const Tree<Vertex>&
t );
template void Tree<Vertex>::test<Vertex>();//error: variable or field
=91test' declared void, expected `;' before =91<' token