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

C

c++newbie

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_algortimen\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 said:
' (or there is no acceptable conversion)
d:\develop\acpp\generiekealgortihmen\generieke_algortimen\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_algortimen\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_algortimen\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_algortimen\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_algortimen\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>::eek:perator
()(class Student &) const'

Can anyone help please?

Regards,

Matthijs.
 
S

Sharad Kala

c++newbie said:
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
 
C

c++newbie

Sharad Kala said:
c++newbie said:
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>::eek:perator
()(class Student &) const'

Matthijs.
 
C

c++newbie

Sharad Kala said:
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.
 
M

msalters

c++newbie said:
Hi all,

I try to compile the following classes:

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

d:\develop\acpp\generiekealgortihmen\generieke_algortimen\student.h(31)
:
error C2679: binary '<<' : no operator defined which takes a right-hand
operand of type 'class std::basic_string<char,struct

d:\develop\acpp\generiekealgortihmen\generieke_algortimen\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
 
C

c++newbie

msalters said:
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.
 
Joined
Feb 12, 2008
Messages
1
Reaction score
0
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:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top