template problem with ostream operator



 DEVELOP > c-Plus-Plus > template problem with ostream operator

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "keit6736"
Date: 15 Oct 2003 09:25:08 PM
Object: template problem with ostream operator
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
.

User: "Victor Bazarov"

Title: Re: template problem with ostream operator 15 Oct 2003 10:35:25 PM
"keit6736" <member44365@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
.

User: "Sergiy Kanilo"

Title: Re: template problem with ostream operator 16 Oct 2003 08:03:55 PM
keit6736 <member44365@dbforums.com> wrote in message news:<3487195.1066271108@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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER