473,574 Members | 3,218 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 12521

"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
2366
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
1498
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
5360
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...
4
2383
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
4444
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
1607
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
2050
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
2451
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
4329
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
7760
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8101
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. ...
0
8271
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...
0
8137
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...
0
5335
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...
0
3774
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1099
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.