By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,958 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,958 IT Pros & Developers. It's quick & easy.

template problem with ostream operator

P: n/a

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.
--
Posted via http://dbforums.com
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"keit6736" <me*********@dbforums.com> wrote...

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 "<>".

{

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
Jul 19 '05 #2

P: n/a
keit6736 <me*********@dbforums.com> wrote in message news:<34****************@dbforums.com>...
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
BinomialTree<Object>& tree);

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& ::operator<<(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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.