By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a

"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

P: n/a

"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

P: n/a

"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

P: n/a

"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

P: n/a

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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.