473,804 Members | 3,453 Online
Bytes | Software Development & Data Engineering Community
+ 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 2612
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<<(ostr eam& outPut, const linkedQueue<Typ e>& 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
1742
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 file easy: ----------------------------------- class Out { private: const char *path; ofstream outfile; public: Out(const char *c);
2
3830
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 operator so that I can read in one digit at a time from the buffer and if the digit is 0 - 9 then put it into the dynamic array which stores each digit of this large int. #include <iostream> #include <string> #include "myint.h"
7
4674
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++ copyconstructor1.cpp #copyconstructor1.cpp: In function `int main()': #copyconstructor1.cpp:86: no match for `sample& = sample' operator #copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&) ...
8
2752
by: bipod.rafique | last post by:
Hello All, I need your help in understanding something. I have a simple class class test{ };
20
3873
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 a named namespace namespace foo { class Foo
6
3597
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 syntax is used. The reason for this is that I have a log file class that needs to know how many lines have been written. When the line exceeds a certain number, I need to close the log file, and open a new one. I would like to be able to track...
9
2389
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 attempt to check to see if the object is null: "The call is ambiguous between the following methods or properties: 'TestObject.operator ==(TestObject, string)' and 'TestObject.operator ==(TestObject, TestObject)" Does anyone have any idea how to...
2
7379
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 number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. // Definition of class Complex #ifndef COMPLEX1_H
3
4692
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: double operator+ ( char* left , char* right) { // some code here }
0
9705
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
10567
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
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
9138
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
7613
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
5515
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...
1
4291
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
3
2983
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.