473,883 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[VC++] Operator overloading: error C2679 / C2678

Hi all,

I try to compile the following classes:

main.cpp:

#include <algorithm>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <functional>
#include "student.h"

using namespace std;

int main(int argc, char **argv) {
if (argc < 3 || argc > 3) {
cerr << "Usage: " << argv[0] << " <infile> <outfile>" << endl; exit(1);
}
ifstream input(argv[1]);
ofstream output(argv[2]);
istream_iterato r<Student> inputIterator(i nput);
ostream_iterato r<Student> outputIterator( output);
vector<Student> Students;

copy(inputItera tor, istream_iterato r<Student>(),
back_inserter(S tudents));

sort(Students.b egin(), Students.end()) ;
vector<Student> ::iterator new_end = unique(Students .begin(),
Students.end()) ;
stable_sort(Stu dents.begin(), new_end, cmpGrade);
for_each(Studen ts.begin(), new_end, mem_fun_ref(&St udent::toCout)) ;

output << "<?xml version=\"1.0\" ?>" << endl << "<Students> " << endl;
copy(Students.b egin(), new_end, outputIterator) ;
output << "</Students>" << endl;

return 0;
}

student.h:

#include <algorithm>
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <functional>

using namespace std;

class Student {

private:
string name, first, place, gender, grade;

public:
Student() { }
~Student() { }
string getName() const { return name; }
string getFirst() const { return first; }
string getPlace() const { return place; }
string getGender() const { return gender; }
string getGrade() const { return grade; }
void toCout() { cout << *this; }

friend ostream& operator <<(ostream &os, const Student &s) {
os << "Student name=\"" << s.getName() << "\" "
<< "first=\"" << s.getFirst() << "\" "
<< "place=\"" << s.getPlace() << "\" "
<< "gender=\"" << s.getGender() << "\" "
<< "grade=\"" << s.getGrade() << "\" />"
<< endl;
return os;
};

friend istream& operator >>(istream &is, Student &s) {
string line;
char buf[128];
is.getline(buf, sizeof(buf), ','); s.name.assign(b uf);
is.getline(buf, sizeof(buf), ','); s.first.assign( buf);
is.getline(buf, sizeof(buf), ','); s.place.assign( buf);
is.getline(buf, sizeof(buf), ','); s.gender.assign (buf);
is.getline(buf, sizeof(buf), '\n'); s.grade.assign( buf);
return is;
};

friend bool operator<(const Student &s1, const Student &s2) {
return lexicographical _compare(
s1.getName().be gin(), s1.getName().en d(),
s2.getName().be gin(), s2.getName().en d());
};

friend bool cmpGrade(const Student &s1, const Student &s2) {
return lexicographical _compare(
s1.getGrade().b egin(), s1.getGrade().e nd(),
s2.getGrade().b egin(), s2.getGrade().e nd());
};

friend bool operator==(cons t Student &s1, const Student &s2) {
return s1.getName() == s2.getName() &&
s1.getFirst() == s2.getFirst() &&
s1.getPlace() == s2.getPlace() &&
s1.getGender() == s2.getGender() &&
s1.getGrade() == s2.getGrade();
};
};

Compiling these classes gives the following output:

d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(31) :
error C2679: binary '<<' : no operator defined which takes a right-hand
operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char>
' (or there is no acceptable conversion)

d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(64) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char> >
' (or there is no acceptable conversion)
d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(65) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char> >
' (or there is no acceptable conversion)
d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(66) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char> >
' (or there is no acceptable conversion)
d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(67) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char> >
' (or there is no acceptable conversion)
d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(68) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char> >
' (or there is no acceptable conversion)

I already tried to explicitly import <string> in student.h, but then I
receive the following error:

d:\program files\microsoft visual studio\vc98\inc lude\functional (263) :
error C2562: '()' : 'void' function returning a value
d:\program files\microsoft visual
studio\vc98\inc lude\functional (262) : see declaration of '()'
d:\program files\microsoft visual
studio\vc98\inc lude\functional (263) : while compiling class-template member
function 'void __thiscall std::mem_fun_re f_t<void,class Student>::opera tor
()(class Student &) const'

Can anyone help please?

Regards,

Matthijs.


Jul 22 '05 #1
6 12557

"c++newbie" <ma**********@y ahoo.com> wrote in message
news:cs******** **@netlx020.civ .utwente.nl...
Hi all,

I try to compile the following classes:

main.cpp:
[snip] Can anyone help please?

I see that you have used std::string but there is no inclusion of <string>
i.e. add this line - #include <string>.

Sharad

Jul 22 '05 #2

"Sharad Kala" <no************ *****@yahoo.com > wrote in message
news:34******** *****@individua l.net...

"c++newbie" <ma**********@y ahoo.com> wrote in message
news:cs******** **@netlx020.civ .utwente.nl...
Hi all,

I try to compile the following classes:

main.cpp:

[snip]
Can anyone help please?

I see that you have used std::string but there is no inclusion of <string>
i.e. add this line - #include <string>.

Sharad


I already tried to explicitly import <string> in student.h, but then I
receive the following error:

d:\program files\microsoft visual studio\vc98\inc lude\functional (263) :
error C2562: '()' : 'void' function returning a value
d:\program files\microsoft visual
studio\vc98\inc lude\functional (262) : see declaration of '()'
d:\program files\microsoft visual
studio\vc98\inc lude\functional (263) : while compiling class-template member
function 'void __thiscall std::mem_fun_re f_t<void,class Student>::opera tor
()(class Student &) const'

Matthijs.
Jul 22 '05 #3

"c++newbie" <ma**********@y ahoo.com> wrote in message

"Sharad Kala" <no************ *****@yahoo.com > wrote in message
news:34******** *****@individua l.net... I already tried to explicitly import <string> in student.h, but then I
receive the following error:


Which VC ? It compiled for me on VC 8 after inclusion of the header.

Sharad

PS - I can't swear that there are no other problems in the code though.

Jul 22 '05 #4

"Sharad Kala" <no************ *****@yahoo.com > wrote in message
news:34******** *****@individua l.net...

"c++newbie" <ma**********@y ahoo.com> wrote in message

"Sharad Kala" <no************ *****@yahoo.com > wrote in message
news:34******** *****@individua l.net...

I already tried to explicitly import <string> in student.h, but then I
receive the following error:


Which VC ? It compiled for me on VC 8 after inclusion of the header.

Sharad

PS - I can't swear that there are no other problems in the code though.

VC++ 6.0.
Jul 22 '05 #5

c++newbie wrote:
Hi all,

I try to compile the following classes:

main.cpp: .... Compiling these classes gives the following output:

d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(31)
: error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char>
' (or there is no acceptable conversion)

d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(64)
: error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char> >
' (or there is no acceptable conversion)


That means you forgot both <string> and <ostream>

BTW, I saw you're using VC6.0, and you're a Dutch student. You
should be able to get a cheap VC7 license from SurfSpot. It's
a lot better.

Regards,
Michiel Salters

Jul 22 '05 #6
"msalters" <Mi************ *@logicacmg.com > wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .

c++newbie wrote:
Hi all,

I try to compile the following classes:

main.cpp:

...
Compiling these classes gives the following output:

d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(31)
:
error C2679: binary '<<' : no operator defined which takes a

right-hand
operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char>
' (or there is no acceptable conversion)

d:\develop\acpp \generiekealgor tihmen\generiek e_algortimen\st udent.h(64)
:
error C2678: binary '==' : no operator defined which takes a

left-hand
operand of type 'class std::basic_stri ng<char,struct
std::char_trait s<char>,class std::allocator< char> >
' (or there is no acceptable conversion)


That means you forgot both <string> and <ostream>

BTW, I saw you're using VC6.0, and you're a Dutch student. You
should be able to get a cheap VC7 license from SurfSpot. It's
a lot better.

Regards,
Michiel Salters


Yeah should try at least 7.0 as I experienced problems with an earlier
assignment as well. Sharad Kala could compile the same code as is without
any problems using VC8. Hope that helps.

Greetz.
Matthijs.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2378
by: Jason Heyes | last post by:
Stupid templates won't compile. Here is my program: #include <fstream> #include <iostream> #include <algorithm> #include <iterator> #include <map> using namespace std; template <class T, class U>
4
1508
by: twonjosh | last post by:
Hello, i'm trying to make a simple friend function for multipling a vector and a matrix together. I'm getting a very wierd error and I don't know why. Here is the code snipits: Friend function prototype: jVector4 operator*(const jVector4& vec, const jMatrix44& mat); Code sample...........
2
5371
by: michael.rygh | last post by:
This is just one of the sources for a project that deals with a library. I am having problems, here is the errors. all these.. i belive it has something to do with it needs a subscript? but i'm not exactly sure what to do.. (33): error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'char ' (or there is no acceptable conversion) 37): error C2679: binary '>>' : no operator found which takes a
4
2392
by: danip | last post by:
Hi, I used to have the following: class CArchive { public: // create and specify the filename to use CArchive(CString filename, DWORD size); virtual ~CArchive(); CArchive& operator<<(bool b);
2
4461
by: linq936 | last post by:
Hi, I have this piece code, struct TriStr { MyString str1; MyString str2; MyString str3; TriStr(MyString s1, MyString s2, MyString s3){ this->str1 = s1;
4
1616
by: Hans | last post by:
Hi All, Can anybody explain how to fix the operator in the code below, Visual C++ gives the following error message: error C2678: binary '; for (i=0; i<sz; i++) v=0; }; ~bool_vector() { delete v;
9
2063
by: Matthias Pospiech | last post by:
I have a Class for complex operations: class CComplex { public: inline CComplex() { re=im=0.0; } inline CComplex operator*(CComplex c) { return CComplex(re*c.re-im*c.im,re*c.im+im*c.re);} inline CComplex operator*(double &d) { return CComplex(re*d,im*d);} double re,im;
5
2471
by: raan | last post by:
What I am trying to achieve here is depicted in the small program below. // Wrapit.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> #include <list>
16
4362
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include <string>
0
9933
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
11114
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...
0
10730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10835
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
10407
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
9563
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...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4205
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.