GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Aug-2003, 12:32
gmn gmn is offline
Junior Member
 
Join Date: Aug 2003
Posts: 33
gmn is on a distinguished road
Question

Operator overloading (not happening)


This is the second listing where I've had this exact same problem now.

I'm learning C++ from a book (Sams Teach Yourself C++ in 21 days, hmm), and have matched the code letter for letter (I think) but am getting a compile error.

CPP / C++ / C Code:
ostream& operator<< (ostream& os, const student& rhs)
{
	os <<rhs.get_name() <<" is " <<rhs.get_age() <<" years old.";
	return os;
}

This comes back with -
Code:
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<ch ar> >' (or there is no acceptable conversion)

overloading "=" worked fine.

Any help is greatly appreciated as I'm now having to modify code from the book just to get stuff to compile, and its making my brain ache.

It's MS Visual C++ v6.
  #2  
Old 27-Aug-2003, 04:16
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Hmmm, that's tricky. Does it give you an error line number?

Could you attach the entire code if possible?

The error is essentially saying rubbish that there is no << operator defined that can output strings. You have included string library?

GF
  #3  
Old 27-Aug-2003, 07:48
gmn gmn is offline
Junior Member
 
Join Date: Aug 2003
Posts: 33
gmn is on a distinguished road
Cheers for the reply. That was including string.h, which I had changed from <string> in an attempt to get this working - which I'll guess was daft, anyway.
including <string> gets this error.....

maybe a bit long for posting I dont know but.....full listing with <string> -

CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;


class student
{
public:
	student();
	student(const string& name, const int age);
	student(const student& rhs);
	~student();

	void set_name(const string& name);
	string get_name() const;
	void set_age(const int age);
	int get_age() const;

	student& operator=(const student& rhs);
	
	

private:
	string its_name;
	int its_age;
};

student::student():
its_name("new student"), its_age(16)
{}

student::student(const string& name, const int age):
its_name(name), its_age(age)
{}

student::student(const student& rhs):
its_name(rhs.get_name()), its_age(rhs.get_age())
{}

student::~student()
{}

void student::set_name(const string& name)
{
	its_name = name;
}

string student::get_name() const
{
	return its_name;
}

void student::set_age(const int age)
{
	its_age = age;
}

int student::get_age() const
{
	return its_age;
}

student& student::operator =(const student& rhs)
{
	its_name = rhs.get_name();
	its_age = rhs.get_age();
	return * this;
}


ostream& operator<<(ostream& os, const student& rhs)
{
	os <<rhs.get_name()<<" is " <<rhs.get_age() <<" years old.";
	return os;
}

	template<class T>
	void show_vector(const vector<T>& v)
	{
		cout<<"max_size() = "<<v.max_size();
		cout<<"\tsize() = "<<v.size();
		cout<<"\tcapacity() = "<<v.capacity();
		cout<<"\t"<<(v.empty() ? "empty": "not empty");
		cout<<"\n";

		for(int i=0; i<v.size(); i++)
		{
			cout<<v[i]<<"\n";
		}

		cout<<endl;
	}


typedef vector<student>  school_class;


int main()
{
	student harry;
	student sally("sally", 15);
	student bill("bill", 17);
	student peter("peter", 16);

	school_class empty_class;
	cout<<"empty class: \n";
	show_vector(empty_class);

	school_class growing_class(3);
	cout<<"growing_class(3): \n";
	show_vector(growing_class);

	growing_class[0] = harry;
	growing_class[1] = sally;
	growing_class[2] = bill;
	cout<<"growing_class(3) after assigning students: \n";
	show_vector(growing_class);

	growing_class.push_back(peter);
	cout<<"growing_class after adding 4th student: \n";
	show_vector(growing_class);

	growing_class[0].set_name("bob");
	growing_class[1].set_age(18);
	cout<<"growing_class after set_age(): \n";
	show_vector(growing_class);

	return 0;
}

Compiling...
vectors.cpp
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/vectors.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

vectors.exe - 2 error(s), 0 warning(s)

gone over this a thousand times...but got it working fine without the << operator bit, changing show_vector to access member functions in turn.
  #4  
Old 27-Aug-2003, 07:51
gmn gmn is offline
Junior Member
 
Join Date: Aug 2003
Posts: 33
gmn is on a distinguished road
where'd those faces come from? wasnt me.
  #5  
Old 27-Aug-2003, 17:35
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Those 'faces' otherwise known as 'smilies', are automatically translated by the forum script when it detects e.g. :) in your post.

You can disable this 'translation', especially when you're pasting code in your posts by checking the little box Disable Smilies in This Post under Options: and below the main box (where you usually type your message/post to this board).

By the way, you can wrap your C++ code posting your code like this:
Code:
[cpp]insert your C++ code in between these tags[/cpp]

This will result in syntax highlighted code (my work on getting the syntax highlighting working well for C++ is as yet incomplete, so bear with me for a while), see your edited post above for the after-effects using these bbcode tags.
  #6  
Old 28-Aug-2003, 03:37
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
It's a peculiar error. I notice that the overloaded function isn't declared anywhere.

You could try inserting the following into the class definition (under public):

CPP / C++ / C Code:
friend ostream& operator<<(ostream& os, const student& rhs)

This could add validity to the function, explicitly allowing it access to the class's components.

I've done a few searches for errata for this book, hope some of these might clean up some typos:

www.libertyassociates.com - from the authors mouth
www.computersciencelab.com - if you're trying GCC

GF
  #7  
Old 29-Aug-2003, 04:43
gmn gmn is offline
Junior Member
 
Join Date: Aug 2003
Posts: 33
gmn is on a distinguished road
thats something i tried actually, and again i get a new but equally annoying error.........

D:\Program Files\Microsoft Visual Studio\MyProjects\vectors\vectors.cpp(92) : error C2593: 'operator <<' is ambiguous
D:\Program Files\Microsoft Visual Studio\MyProjects\vectors\vectors.cpp(111) : see reference to function template instantiation 'void __cdecl show_vector(const class std::vector<class student,class std::allocator<class student> > &)' being c
ompiled
Error executing cl.exe.

does the fact that i had the same problem with an earlier listing, again copying code letter for letter, mean anything to anybody. are there buggy releases of the compiler or anything?
  #8  
Old 29-Aug-2003, 05:05
gmn gmn is offline
Junior Member
 
Join Date: Aug 2003
Posts: 33
gmn is on a distinguished road
cheers for the links.

i dropped the source code from the book directly into a new project - and it worked.
copied the operator<< function directly into my code (changing back the function names), and i get -

LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/vectors.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

again.

must be a typo somewhere on my part but i'm damned if i can find it, even scanned the white space in the window for rogue characters. and whats stopping the compiler flagging a specific line in the code?

i'll wait for the next listing that uses this and see what happens.
  #9  
Old 29-Aug-2003, 05:48
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
I've done a little Googling, and most of the time when this error comes up, there's a library file that is not in the right place. Make sure that these library files are in the correct directories too, if they're missing bad things will happen.

Otherwise, I'm stuck!


GF
  #10  
Old 29-Aug-2003, 23:50
sdeming sdeming is offline
VIP
 
Join Date: May 2003
Location: Michigan, US
Posts: 8
sdeming will become famous soon enough
Quote:
Originally posted by gmn

LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/vectors.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Hello gmn,

It looks to me like you are creating a Win32 application instead of a Win32 console application. It doesn't appear that there is a problem with your code, it's during the link phase that it is breaking.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help not sure wat is happening saxon Computer Software Forum - Windows 2 26-Mar-2006 17:37
What's happening to nature? priyanka Open Discussion Forum 7 30-Oct-2004 13:58
Looking like a Google 'DeepCrawl'? JdS Search Engine Optimization Forum 13 27-Dec-2003 02:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:08.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.