473,378 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
Jul 19 '05 #1
2 5941
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
8
by: Plissken.s | last post by:
I have a template function which print out the content of a list: The following compiles: void PrintInt2 (int i) { cout << i << " "; } template <class T> void printList(T& list) {
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
2
by: syang8 | last post by:
Dear all, I am trying to design classes with stream support. Basically, I want the operator << work for the base class and all the derived classes. If the base class is a template class, and...
1
by: Stuart Golodetz | last post by:
Hi guys, I'm trying to making an instance of a templated operator<< for a templated class a friend of that class (see below), to allow it to access the class internals for output purposes. ...
3
by: laikon | last post by:
this question is about how to define friend functions for a class template. the following is an example. template <typename T> class Array { private: T* parr; int sz;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.