Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > [VC++] Operator overloading: error C2679 / C2678

Reply
Thread Tools

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

 
 
c++newbie
Guest
Posts: n/a
 
      01-14-2005
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(6 :
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>:perator
()(class Student &) const'

Can anyone help please?

Regards,

Matthijs.




 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      01-14-2005

"c++newbie" <> wrote in message
news:cs87fo$i13$...
> 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



 
Reply With Quote
 
 
 
 
c++newbie
Guest
Posts: n/a
 
      01-14-2005

"Sharad Kala" <> wrote in message
news:...
>
> "c++newbie" <> wrote in message
> news:cs87fo$i13$...
> > 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>:perator
()(class Student &) const'

Matthijs.


 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      01-14-2005

"c++newbie" <> wrote in message
>
> "Sharad Kala" <> wrote in message
> news:...


> 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.



 
Reply With Quote
 
c++newbie
Guest
Posts: n/a
 
      01-14-2005

"Sharad Kala" <> wrote in message
news:...
>
> "c++newbie" <> wrote in message
> >
> > "Sharad Kala" <> wrote in message
> > news:...

>
> > 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.


 
Reply With Quote
 
msalters
Guest
Posts: n/a
 
      01-14-2005

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

 
Reply With Quote
 
c++newbie
Guest
Posts: n/a
 
      01-14-2005
"msalters" <> wrote in message
news: 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.


 
Reply With Quote
 
trav580 trav580 is offline
Junior Member
Join Date: Feb 2008
Posts: 1
 
      02-12-2008
I'm having a similar problem..Here is my coding as is.

/*
Program: King Hardware Store Inventory Program
Programmer: Travis Skillin
Date: 2/9/2008
Program: MidTermTS
Version: 1

This program will allow the user to enter an item number, on hand amount and on order amount, as well as price.
*/
/* ------preprocessing directives -----------------------------------------*/

#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>

using namespace std;


/* ------function prototypes ----------------------------------------------*/

int KeyIt(char[25]);
void loop();

/* ----- Global Variables -------------------------------------------------*/
/* input variables */
int ItemNum = 0; /*Denotes the item number*/
int UnitCost = 0; /*The units cost*/
int OnHand; /*shows the amount on hand*/
float OrderCost = 0;
char answer ;
int needed; /*Amount that needs to be reordered.*/

string descrip[6] = {
"Hammer",
"Saw",
"Drill",
"Screwdriver",
"Pliers",
"INVALID",
};

float Data[6][7] =

{
{1000,1999,12,24,0,0,0},
{2000,2999,3,4,0,0,0},
{3000,3999,4,5,0,0,0},
{4000,4999,50,75,0,0,0},
{5000,5999,12,36,0,0,0},
{6000,9999,0,0,0,0,0}
};


/* -------------------------------------------------------------------------
Main Module
---------------------------------------------------------------------------*/

int main()
{int fIndex(int num);
int n;
int x;
do

{
cout <<
"************************************************* ***************************" << endl;
cout << " King Hardware Store Inventory Program " << endl;
cout <<
"************************************************* ***************************" << endl;
ItemNum = KeyIt("Item Number: ");
n = fIndex(ItemNum);
cout<< " "<<endl;
OnHand = KeyIt("Qty on Hand: ");
cout<< " "<<endl;
UnitCost = KeyIt("Unit Cost: $");
cout<< " "<<endl;
system("pause");
system("cls");
cout <<

Data[n][4] << OnHand;

Data[n][6] = 0;
if ( Data[n][4] <= Data[n][2]) {
Data[n][6] = Data[n][2] - Data[n][4];
if ( Data[n][6] == 0 ) Data[n][6]++;
}


cout<< "************************************************* ***************************" << endl;
cout << " King Hardware Store Individual Item Reorder information " << endl;
cout <<
"************************************************* ***************************" << endl;
cout << "Item"" ""Qty"" ""On"" ""Item"" "<<endl;
cout << "Number"" ""Description"" ""on Hand"" ""Order"" ""Cost"<<endl;
cout << descrip[x];
cout << ": Item #: ";
cout <<Data[n][1];
cout << "-";
cout << Data[n][2];
cout << " On Hand: ";
cout << Data[n][5];
cout << " Price: $";
cout << Data[n][6];
loop();
system("cls");
}while (answer == 'N' || answer== 'n');

}
/* -------------------------------------------------------------------------
Name: KeyIt
Desc: Get keyboard input
Return: float -- value entered
Parameters: char -- prompt
---------------------------------------------------------------------------*/
int KeyIt(char desc[25])
{
int fInputNum;
cout << desc;
cin >> fInputNum;
//get rid of the enter key
cin.ignore();

return fInputNum;
}
void loop()
{
cout << "Are we done for the day? Yes/No? ";
cin>> answer;
cin.ignore();
return;
}
int fIndex(int num){
int x;
for(x=0;x<5;x++){
if( Data[x][0] < num && num < Data[x][1] ) return x;
}
return x;
}


Ok, I edited my post because I fixed my problem. Now for the fun part. I need to pull information from my array and have it display on my screen.
 

Last edited by trav580; 02-12-2008 at 09:44 PM..
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const string error compilation - error C2679 Tassador C++ 0 07-19-2009 05:26 AM
error: no match for ‘operator<<’ in ‘std::cout.std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](k) << dispMyarr(k)’ curiousEngine C++ 1 05-09-2008 09:34 AM
error C2679 hgbso C++ 0 03-09-2008 08:43 PM
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) aarthi28@gmail.com C++ 29 06-21-2007 08:42 PM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57