473,498 Members | 1,998 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_iterator<Student> inputIterator(input);
ostream_iterator<Student> outputIterator(output);
vector<Student> Students;

copy(inputIterator, istream_iterator<Student>(),
back_inserter(Students));

sort(Students.begin(), Students.end());
vector<Student>::iterator new_end = unique(Students.begin(),
Students.end());
stable_sort(Students.begin(), new_end, cmpGrade);
for_each(Students.begin(), new_end, mem_fun_ref(&Student::toCout));

output << "<?xml version=\"1.0\"?>" << endl << "<Students>" << endl;
copy(Students.begin(), 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(buf);
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().begin(), s1.getName().end(),
s2.getName().begin(), s2.getName().end());
};

friend bool cmpGrade(const Student &s1, const Student &s2) {
return lexicographical_compare(
s1.getGrade().begin(), s1.getGrade().end(),
s2.getGrade().begin(), s2.getGrade().end());
};

friend bool operator==(const 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\generiekealgortihmen\generieke_alg ortimen\student.h(31) :
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<char>
' (or there is no acceptable conversion)

d:\develop\acpp\generiekealgortihmen\generieke_alg ortimen\student.h(64) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >
' (or there is no acceptable conversion)
d:\develop\acpp\generiekealgortihmen\generieke_alg ortimen\student.h(65) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >
' (or there is no acceptable conversion)
d:\develop\acpp\generiekealgortihmen\generieke_alg ortimen\student.h(66) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >
' (or there is no acceptable conversion)
d:\develop\acpp\generiekealgortihmen\generieke_alg ortimen\student.h(67) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >
' (or there is no acceptable conversion)
d:\develop\acpp\generiekealgortihmen\generieke_alg ortimen\student.h(68) :
error C2678: binary '==' : no operator defined which takes a left-hand
operand of type 'class std::basic_string<char,struct
std::char_traits<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\include\functional(263) :
error C2562: '()' : 'void' function returning a value
d:\program files\microsoft visual
studio\vc98\include\functional(262) : see declaration of '()'
d:\program files\microsoft visual
studio\vc98\include\functional(263) : while compiling class-template member
function 'void __thiscall std::mem_fun_ref_t<void,class Student>::operator
()(class Student &) const'

Can anyone help please?

Regards,

Matthijs.


Jul 22 '05 #1
6 12505

"c++newbie" <ma**********@yahoo.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*************@individual.net...

"c++newbie" <ma**********@yahoo.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\include\functional(263) :
error C2562: '()' : 'void' function returning a value
d:\program files\microsoft visual
studio\vc98\include\functional(262) : see declaration of '()'
d:\program files\microsoft visual
studio\vc98\include\functional(263) : while compiling class-template member
function 'void __thiscall std::mem_fun_ref_t<void,class Student>::operator
()(class Student &) const'

Matthijs.
Jul 22 '05 #3

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

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:34*************@individual.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*************@individual.net...

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

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:34*************@individual.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\generiekealgortihmen\generieke_alg ortimen\student.h(31)
: 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<char>
' (or there is no acceptable conversion)

d:\develop\acpp\generiekealgortihmen\generieke_alg ortimen\student.h(64)
: error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'class std::basic_string<char,struct
std::char_traits<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.googlegr oups.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\generiekealgortihmen\generieke_alg ortimen\student.h(31)
:
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<char>
' (or there is no acceptable conversion)

d:\develop\acpp\generiekealgortihmen\generieke_alg ortimen\student.h(64)
:
error C2678: binary '==' : no operator defined which takes a

left-hand
operand of type 'class std::basic_string<char,struct
std::char_traits<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
2358
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,...
4
1493
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...
2
5352
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...
4
2368
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...
2
4436
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
1595
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() {...
9
2048
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);}...
5
2440
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...
16
4317
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...
0
7197
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...
1
6881
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...
0
5456
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
4899
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
3088
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
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
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
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...

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.