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
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>:
perator
()(class Student &) const'
Can anyone help please?
Regards,
Matthijs.
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
d:\develop\acpp\generiekealgortihmen\generieke_algortimen\student.h(64) :std::char_traits said:' (or there is no acceptable conversion)
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>:
()(class Student &) const'
Can anyone help please?
Regards,
Matthijs.