template problem with ostream operator

K

keit6736

Hi, I'm using the Borland compiler and I've created two templated
classes in which I've overloaded the ostream << operator. However, when
I try and use the operator on objects of either class I get the
following error:



Error: Unresolved external 'operator <<(std::basic_ostream<char,
std::char_traits<char> >&, const BinomialTree<int>&)' referenced from
C:\DOCUMENTS AND SETTINGS\RYAN\DESKTOP\COMPUTER SCIENCE\CS
2413\PROJECTS\PROJECT 3\PROJ3.OBJ





I'm almost certain that this is somehow caused by the templates, because
when I remove all 'templating' from the classes, everything works
perfectly. Here is the relevant code:





template<class Object>

class BinomialTree

{

friend ostream& operator<< <>(ostream& stream, const
BinomialTree<Object>& tree);



private:

//array of Binomial Node pointers

BinomialNode<Object>* locations[100];

...stuff here

};



template<class Object>

ostream& operator<< (ostream& stream, const BinomialTree<Object>& tree)

{

for(int j = 0; j<100; j++)

{

if(tree.locations[j] != NULL)

{

stream<<(*(tree.locations[j]));

}

}

return stream;

}





template <class Object>

class BinomialNode

{



//overloaded ostream operator, for displaying



friend ostream& operator<< <>(ostream& stream, const
BinomialNode<Object>& node);



private:

void copy(const BinomialNode<Object>& rhs);

Object _key; //the key for this class, ideally an integer value



int _degree;

BinomialNode* next; //pointer to the next node (a sibling)

BinomialNode* down; //the pointer to the child

BinomialNode* up; //the pointer to this node's parent

...more stuff here

};



template<class Object>

ostream& operator<< <>(ostream& stream, const
BinomialNode<Object>& node)

{

stream<<node._key;



if(node.down != NULL)

return stream<<(*(node.down));



else {

if(node.next == NULL)

return stream;

else

return stream<<(*(node.next));

}

}



void main()

{

BinomialTree<int> myTree;

cout<<myTree;

}





Any help would be greatly appreciated. Thanks.
 
V

Victor Bazarov

keit6736 said:
Hi, I'm using the Borland compiler and I've created two templated
classes in which I've overloaded the ostream << operator. However, when
I try and use the operator on objects of either class I get the
following error:



Error: Unresolved external 'operator <<(std::basic_ostream<char,
std::char_traits<char> >&, const BinomialTree<int>&)' referenced from
C:\DOCUMENTS AND SETTINGS\RYAN\DESKTOP\COMPUTER SCIENCE\CS
2413\PROJECTS\PROJECT 3\PROJ3.OBJ





I'm almost certain that this is somehow caused by the templates, because
when I remove all 'templating' from the classes, everything works
perfectly. Here is the relevant code:





template<class Object>

class BinomialTree

{

friend ostream& operator<< <>(ostream& stream, const
BinomialTree<Object>& tree);

This is not the right way to declare a friend. Do you want to
declare a particular specialisation a friend? Then it's not
a template, and you shouldn't put <> there. If you want to make
all specialisations friends, you need to say 'template' at the
beginning (before 'friend')...

What's your intention here? Try

friend ostream& operator << (ostream&, const [blah]);
private:

//array of Binomial Node pointers

BinomialNode<Object>* locations[100];

BinomialNode is undefined here.
..stuff here

};



template<class Object>

ostream& operator<< (ostream& stream, const BinomialTree<Object>& tree)

{

for(int j = 0; j<100; j++)

{

if(tree.locations[j] != NULL)

{

stream<<(*(tree.locations[j]));

}

}

return stream;

}





template <class Object>

class BinomialNode

{



//overloaded ostream operator, for displaying



friend ostream& operator<< <>(ostream& stream, const
BinomialNode<Object>& node);
Again...




private:

void copy(const BinomialNode<Object>& rhs);

Object _key; //the key for this class, ideally an integer value



int _degree;

BinomialNode* next; //pointer to the next node (a sibling)

BinomialNode* down; //the pointer to the child

BinomialNode* up; //the pointer to this node's parent

..more stuff here

};



template<class Object>

ostream& operator<< <>(ostream& stream, const
BinomialNode<Object>& node)

This is not the right way to define a template. Drop the said:
{

stream<<node._key;



if(node.down != NULL)

return stream<<(*(node.down));



else {

if(node.next == NULL)

return stream;

else

return stream<<(*(node.next));

}

}



void main()

'main' should return 'int'.
{

BinomialTree<int> myTree;

cout<<myTree;

}





Any help would be greatly appreciated. Thanks.

HTH

Victor
 
S

Sergiy Kanilo

keit6736 said:
Hi, I'm using the Borland compiler and I've created two templated
classes in which I've overloaded the ostream << operator. However, when
I try and use the operator on objects of either class I get the
following error:



Error: Unresolved external 'operator <<(std::basic_ostream<char,
std::char_traits<char> >&, const BinomialTree<int>&)' referenced from
C:\DOCUMENTS AND SETTINGS\RYAN\DESKTOP\COMPUTER SCIENCE\CS
2413\PROJECTS\PROJECT 3\PROJ3.OBJ

I'm almost certain that this is somehow caused by the templates, because
when I remove all 'templating' from the classes, everything works
perfectly. Here is the relevant code:

yes, for Borland, you must declare your template function before
the your class that declares it as a friend

template<class Object> class BinomialTree;
template<class Object> ostream& operator<<(ostream& stream, const
template<class Object>

class BinomialTree

{

friend ostream& operator<< <>(ostream& stream, const
BinomialTree<Object>& tree);

if you know that friend function have been declared
you you can use

friend ostream& ::eek:perator<<(ostream& stream, const
BinomialTree<Object>& tree);

that works for both template and non-template functions
private:

//array of Binomial Node pointers

BinomialNode<Object>* locations[100];

..stuff here

};



template<class Object>

ostream& operator<< (ostream& stream, const BinomialTree<Object>& tree)

{

for(int j = 0; j<100; j++)

{

if(tree.locations[j] != NULL)

{

stream<<(*(tree.locations[j]));

}

}

return stream;

}

[...]

Cheers,
Serge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top