473,487 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

MSVS2005 Standard: insertion operator overload: LNK2028

67 New Member
I'm currently a student, but this problem isn't directly related to what I have to do on an assignment. It's just a problem I've had with some supporting features.

First of all, I'm using MSVS 2005 standard edition. I haven't downloaded or acquired any additional libraries or tools, just what gets installed with the studio by default. Also, I'm using C++ and started the problem projects as an empty CLR project.

Currently I've had several data structure assignments that required me to make class templates for linked lists, stacks, queues, etc. As a feature of these structures, I like to overload the insertion operator (<<) so that it's simpler to output the entire structure. I've followed everything mentioned from my text books, and even resorted to copying an entire example word for word, and the problem still persists.

And all of my files include the iostream header file and the usingnamespace std; statement. And the function definition is in the same header file as the class definition.

In the class template definition, my prototype for overloading the operator looks like this:
Expand|Select|Wrap|Line Numbers
  1. friend ostream& operator<<(ostream&, const linkedQueue<Type>&);
The definition of the overload is as follows:
Expand|Select|Wrap|Line Numbers
  1. template<class Type>
  2. ostream& operator<<(ostream& outPut, const linkedQueue<Type>& thisQueue)
  3. {
  4.     //makes a pointer to the first element in the queue
  5.     queueNode<Type> *current;
  6.     current = thisQueue.first;
  7.  
  8.     //steps through the elements of the queue without
  9.     //changing anything.
  10.     while (current != NULL)
  11.     {
  12.         //outputs the data stored in the current node
  13.         outPut << current->data << endl;
  14.  
  15.         //moves current to the next node
  16.         current = current->next;
  17.     }
  18.  
  19.     return outPut;
  20.  
  21. }//end insertion overload
When I include in my function main() the line ("object" is an object of the class template the function was overloaded for):
Expand|Select|Wrap|Line Numbers
  1. cout << object;
I get 3 errors:

main.obj : error LNK2028: unresolved token (0A00029B)
main.obj : error LNK2019: unresolved external symbol
(project path and name).exe : fatal error LNK1120: 2 unresolved externals

I'd just like to know how to get the insertion operator to work with class templates.

I appreciate any help I receive.
Jun 28 '07 #1
5 2588
weaknessforcats
9,208 Recognized Expert Moderator Expert
What does your main() look like and how are you creating the object?
Jun 29 '07 #2
phiefer3
67 New Member
What does your main() look like and how are you creating the object?
Well for the sake space, I won't post the entire main(), but the following is an example, that has the same results.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. //header file containing the template mentioned earlier
  4. #include “linkedQueue.h”
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     linkedQueue<int> myQueue;
  11.  
  12.     //add items to the queue…
  13.     for (int i = 0; i < 10; i++)
  14.         myQueue.addElement(i);
  15.  
  16.     cout << myQueue;
  17.  
  18.     return 0;
  19. }
also, the addElement function works fine, that's not the problem. I can replace the overloaded insertion operator with a function that takes an ostream as a parameter to output the contents, and it works fine, it's just when I try to use the overloaded operator<<

also, if I make my queue class into a regular class instead of a template, it works fine.
Jun 29 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:
linkedQueue<int> myQueue;
....
needs an operator<< like this:
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream&, const linkedQueue<int>);
  2.  
The int has been used in the friend declaration of the LinkeQueue class.

but you have this template outside the class:

template<class Type>
ostream& operator<<(ostream& outPut, const linkedQueue<Type>& thisQueue)
......
So when you code:
Expand|Select|Wrap|Line Numbers
  1. cout << myQueue;
  2.  
your operator<< template specializes as:
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& outPut, const linkedQueue<linkedQueue<int> >& thisQueue)
  2.  
because Type is now LinkedQueue<int> and not just int.
and this is not the operator<< you need so
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream&, const linkedQueue<int>);
  2.  
is missing and that's what the linker is reporting.

All of this is happening because your friend operator<< is bound to the class template. When you specialize the class template with an int you also specialize the friend with an int.

You can have a friend be unbound to the class by declaring the friend to be template in the class template itself:
Expand|Select|Wrap|Line Numbers
  1. class LinkedQueue
  2. {
  3.  
  4. public:
  5.     template<class Type>    //unbound friend
  6.     friend ostream& operator<<(ostream& os, const LinkedQueue<Type>& q);
  7. };
  8.  
When you do this, the friend function becomes a friend to every class specialization.

Now you have your
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream&, const linkedQueue<int>);
  2.  
and off you go.
Jun 29 '07 #4
phiefer3
67 New Member
So in other words, the operator<< template is using the right hand argument as the Type parameter?

Or, I still don't quite grasp the logic behind it. Anyways, it works now, and that's good enough for now.

Thanks for the help.
Jun 29 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
When you create LinkedQueue<int>, you also create a template function:

Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, const LinkedQueue<int>& q);
  2.  
that is a friend to LinkedQueue<int>.

Later you do:
Expand|Select|Wrap|Line Numbers
  1. cout << myQueue;
  2.  
which needs
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, const LinkedQueue<int>& );
  2.  
you have already created that template function, so off you go.

If you don't do this, then
Expand|Select|Wrap|Line Numbers
  1. cout << myQueue;
  2.  
creates
Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, const LinkedQueue<LinkedQueue<int> >& );
  2.  
from the operator<< template and that's not the function you need.
Jul 2 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1728
by: cpp | last post by:
Ok, I have been tearing my hair out trying to get this to work, so anything you can do I will greatly appreciate. I have this small class which is supposed to make, for its users, outputting to a...
2
3810
by: ryan.fairchild | last post by:
I have a problem I am trying to create a MyInt class to hanlde very large ints. Its for a class, therefore I can only do what the teach tells me. I want to be able to overload the insertion...
7
4656
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
8
2732
by: bipod.rafique | last post by:
Hello All, I need your help in understanding something. I have a simple class class test{ };
20
3829
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
6
3578
by: jack | last post by:
I have a class which overloads the insertion operator '<<' for every type that ostream handles. I do this so that I can use my class a direct replacement for cout and cerr where the insertion...
9
2363
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
2
7346
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
3
4673
by: kvnsmnsn | last post by:
I've been asked to overload the insertion operator. What exactly is the insertion operator in C++, and how would one overload it? I think that in C I could overload the "+" operator like so: ...
0
7106
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,...
0
6967
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...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7349
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...
0
5442
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,...
1
4874
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...
0
1381
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 ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.