Welcome! Log In Create A New Profile

Advanced

HELP: Linker errors compiling linked lists code

Posted by rleeman 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
HELP: Linker errors compiling linked lists code
February 25, 2007 09:37PM
Has anyone had problems when clicking F9 (Compile & run) the linked lists code in Malik Chapter 5?

Background:

Initially I reproduced the code in Malik by typing it in myself into the Dev-C++ IDE. Trying to follow what I believed is good programming practice, I had separate header and implementation files. However it failed on compiling with numerous linker errors. After endless frustration and general tail-chasing I discovered that where class templates are used, you have to have the implementation in the same file as the header file (see bottom half of page 114 in Malik for confirmation). So I thought, it should compile now...however when I tried to use the overloaded insertion operator << with statements such as cout << y; to test the template class, where y was a linked list object, or ordered linked list object, it failed giving another linker error. To show you what I mean, I have produced a greatly simplified version of the Malik code where the overloaded insertion operator << causes problems.

Sample code


//starts
#include <iostream>
using namespace std;

template<class Type>
class A{

friend ostream& operator<<(ostream&, A<Type>& ) ;
public:
A(Type c = 5);
private:
Type b ;

};

template<class Type>
A<Type>::A(Type c){
b = c;
}

template<class Type>
ostream& operator<<(ostream& o, A<Type>& x){
o << x.b << endl;
return o;
}

int main() {
A<int> y;
cout << y;
return 0;
}
//ends



The error I get is:

[Linker error] undefined reference to `operator<<(std:: ostream&, A<int>& )'

Observations:

remove the code cout << y by replacing it with //cout << y and the code compiles.

OR

take away the template bits and make it less generic with say int to get

#include <iostream>
using namespace std;


class A{

friend ostream& operator<<(ostream&, A& ) ;
public:
A(int c = 5);
private:
int b ;

};

A::A(int c){
b = c;
}

ostream& operator<<(ostream& o, A& x){
o << x.b << endl;
return o;
}

int main() {
A y;
cout << y;
return 0;
}


and the class compiles fine with no linker errors.

Other information:

To make sure that I had not mistyped in any of the code, I downloaded the code from http://www.course.com/malik/cpp/, click student downloads on the top rhs, enter ISBN 0619159073, click download student files on the top left and then source code.

In addition I have ensured that my installation of Dev-C++ conforms exactly with the instructions on the UNISA supplied 2007 CD-ROM, the trouble-shooting section of the CD-ROM and also Tutorial letter 101 (section 9b)

Summary:

The overloaded insertion operators << in the linked list and ordered linked list sections of Malik don't seem to work when trying to use them with simple examples. The code above demonstrates what I mean. I don't know if I'm missing something obvious or if there is something I need to know. All I can say is that I am frustrated and would be grateful for any help.

Best wishes,

Richard
Re: HELP: Linker errors compiling linked lists code
February 27, 2007 08:50AM
Hi Richard,

I have the same problem. I cannot compile the linked list examples in chapter 5 of the text book.

I am currently trying to get help from one of the lecturers, if I find out what to do, I will let you know.

Cheers
Re: HELP: Linker errors compiling linked lists code
February 27, 2007 10:24AM
Thanks Dougie,

I have written to the lecturers too, and if they send a response directly to me then I will post it here for the benefit of everyone.

One other point I noted in my e-mail to the lecturers is that the material in Chapter 4 does not seem to be covered by any of the assignments - has anyone else picked this up or obtained clarification on this?

Richard
Re: HELP: Linker errors compiling linked lists code
February 27, 2007 11:17AM
Hi

Inorder to slove this problem you need to read the errata on page 30 of tutorial letter 101. It says the following:


While compiling the code from Malik, you may encounter warnings similar to the ones
below:
(a) linkedList.h:18: warning: friend declaration
'std :: ostream & operator<<(std :: ostream &, const
linkedListType<Type> & ) ' declares a non-template
function

With warning (a) insert <> after the operator<<

regards
Lazarus Aron
Re: HELP: Linker errors compiling linked lists code
February 27, 2007 01:50PM
Thank you very much for your assistance.

For everyone's information the <> is for for the prototype declaration only, NOT in the implementation as well.

Following my simplified example above, the following code now compiles on my computer without those annoying linker errors:

//starts
#include <iostream>
using namespace std;

template<class Type>
class A{

friend ostream& operator<<<>(ostream&, A<Type>& ) ;
public:
A(Type c = 5);
private:
Type b ;

};

template<class Type>
A<Type>::A(Type c){
b = c;
}

template<class Type>
ostream& operator<<(ostream& o, A<Type>& x){
o << x.b << endl;
return o;
}

int main() {
A<int> y;
cout << y;
return 0;
}
//ends


Following the same logic as above, the linked list and ordered linked list code both compile as well.

Best wishes and thanks,

Richard
Sorry, only registered users may post in this forum.

Click here to login