473,626 Members | 3,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_o stream<char,
std::char_trait s<char> >&, const BinomialTree<in t>&)' referenced from
C:\DOCUMENTS AND SETTINGS\RYAN\D ESKTOP\COMPUTER SCIENCE\CS
2413\PROJECTS\P ROJECT 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<Ob ject>& tree);

private:

//array of Binomial Node pointers

BinomialNode<Ob ject>* locations[100];

...stuff here

};

template<class Object>

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

{

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

{

if(tree.locatio ns[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<Ob ject>& node);

private:

void copy(const BinomialNode<Ob ject>& 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<Ob ject>& node)

{

stream<<node._k ey;

if(node.down != NULL)

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

else {

if(node.next == NULL)

return stream;

else

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

}

}

void main()

{

BinomialTree<in t> myTree;

cout<<myTree;

}

Any help would be greatly appreciated. Thanks.
--
Posted via http://dbforums.com
Jul 19 '05 #1
2 5961
"keit6736" <me*********@db forums.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_o stream<char,
std::char_trait s<char> >&, const BinomialTree<in t>&)' referenced from
C:\DOCUMENTS AND SETTINGS\RYAN\D ESKTOP\COMPUTER SCIENCE\CS
2413\PROJECTS\P ROJECT 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<Ob ject>& 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<Ob ject>* locations[100];
BinomialNode is undefined here.

..stuff here

};

template<class Object>

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

{

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

{

if(tree.locatio ns[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<Ob ject>& node);
Again...

private:

void copy(const BinomialNode<Ob ject>& 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<Ob ject>& node)
This is not the right way to define a template. Drop the "<>".

{

stream<<node._k ey;

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<in t> myTree;

cout<<myTree;

}

Any help would be greatly appreciated. Thanks.


HTH

Victor
Jul 19 '05 #2
keit6736 <me*********@db forums.com> wrote in message news:<34******* *********@dbfor ums.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_o stream<char,
std::char_trait s<char> >&, const BinomialTree<in t>&)' referenced from
C:\DOCUMENTS AND SETTINGS\RYAN\D ESKTOP\COMPUTER SCIENCE\CS
2413\PROJECTS\P ROJECT 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<<(ostr eam& stream, const
BinomialTree<Ob ject>& tree);

template<class Object>

class BinomialTree

{

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

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

friend ostream& ::operator<<(os tream& stream, const
BinomialTree<Ob ject>& tree);

that works for both template and non-template functions

private:

//array of Binomial Node pointers

BinomialNode<Ob ject>* locations[100];

..stuff here

};

template<class Object>

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

{

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

{

if(tree.locatio ns[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
10149
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 online at http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.15 Below are the two files that I compile with g++ foo.cpp -o foo or
1
3334
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 to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
6
2329
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 data from the class. Here's the gist of the code: template <class T> struct NodeType { T data; NodeType<T> * link;
4
2495
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
3053
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
3748
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
2
1970
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 the operator << is also designed as a template function. The program has following linking errors: "error LNK2019: unresolved external symbol "class
1
1403
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. #include <iostream> template <typename T> class TC
3
2396
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
8262
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.