I get this error:
Error 2 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 2144
and when i click on it i get this code:
1 2 3 4 5 6 7 8 9 10 11
// TEMPLATE FUNCTION copy
template<class _InIt,
class _OutIt> inline
_OutIt _Copy_impl(_InIt _First, _InIt _Last,
_OutIt _Dest, _Nonscalar_ptr_iterator_tag)
{ // copy [_First, _Last) to [_Dest, ...), arbitrary iterators
for (; _First != _Last; ++_Dest, ++_First)
*_Dest = *_First; //the problem seems to be connected to this line
return (_Dest);
}
it is in xutility internal header
I searched for every == operator that i have used in the classes I implemented and i searched for a problem but none seems to be the problem!
What should i serach for in my code in order to find the problem? I dont have any other errors (in case this information helps)
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <iostream>
#include<string>
#include<list>
#include"Course.h"
class Student
{
private:
std::string firstn;
std::string lastn;
int sem;
int id;
int num; //number of courses that student is registered to
std::list<int> listofcourses;
// TODO: Here should be d e f i n i t i o n o f s chedul e ,
// use t emplat e s s p e c i a l i z a t i o n to d i f f e r e n t i a t e
// between i s L i s t == t rue or i s L i s t == f a l s e .
// I f i s L i s t == t rue Schedule should use s td : : l i s t c ont a ine r
// o the rwi s e Schedule should use s td : : ve c t o r c ont a ine r
public:
enum Result { Ok, Failure, Already_Registered, Not_Found };
/*ctor*/
Student(const std::string& fname, const std::string& lname, int idd=0, int semester=1) { firstn = fname; lastn = lname; id = idd; sem = semester; }
/* get and set functions*/
const std::string getFirstName() const; //return student's first name
Student& setFirstName(const std::string& fname); //set student's first name
const std::string getLastName() const; //return students last name
Student& setLastName(const std::string& lname); //set value for students last name
constint getSemester() const; //return students semester number
Student& setSemester(constint semester); //set value of students semester
constint getId() const; //return students ID
Student& setId(constint id);
int getCoursesCount() const; //return number of courses student registered to
/*regestering to courses functions*/
Result registerToCourse(const Course& course);
Result unregisterFromCourse(constint number);
bool isRegistered(const Course& course);
/*print functions*/
Student& print(std::ostream& os);
Student& printSchedule(std::ostream& os);
/*equality operator*/
booloperator==(const Student& student) const;
/*conversion to string operator*/
operator std::string() { return firstn + lastn; }
/*
* Returns number of courses student registered to.
*/
// int getCoursesCount( ) const;
/*
* Return list of distinct faculty names in schedule
*
* You have to compose different STL algorithms to implement
* this function.
*/
// std::list<std::string> getCoursesFaculties( ) const;
/*
* Get number of courses from given faculty.
* To implement this function you have to use
* STL algorithm and not for-while loops.
*/
// int getCountOfFacultyCourses( const std::string& facultyName) const;
};
#endif
#include "Student.h"
#include <iostream>
/* get and set functions*/
const std::string Student::getFirstName() const{
cout << "You are getting the first name" << endl;
return firstn;
}//return student's first name
Student& Student::setFirstName(const std::string& fname){
firstn = fname;
cout << "Setting first name" << endl;
return *this ;
}//set student's first name
const std::string Student::getLastName() const {
cout << "getting last name" << endl;
return lastn;
} //return students last name
Student& Student::setLastName(const std::string& lname) {
lastn = lname;
cout << "setting last name" << endl;
return *this;
} //set value for students last name
constint Student::getSemester() const {
cout << "getting semester" << endl;
return sem;
}//return students semester number
Student& Student::setSemester(constint semester) {
sem = semester;
cout << "setting semester" << endl;
return *this;
} //set value of students semester
constint Student::getId() const {
cout << "getting id" << endl;
return id;
} //return students ID
Student& Student::setId(constint id){
this->id = id;
cout << "setting id" << endl;
return *this;
}
int Student::getCoursesCount() const {
cout << "getting number of courses the student is registered to" << endl;
return num;
} //return number of courses student registered to
/*equality operator*/
bool Student::operator==(const Student& student) const {
cout << "using operator ==" << endl;
//if ((student.firstn == firstn) && (student.firstn == lastn) && (student.id == id) && (student.sem == sem))
if (student.getFirstName() == firstn && student.getLastName() == lastn && student.getId() == id && student.getSemester() == sem)
returntrue;
elsereturnfalse;
}
/*regestering to courses functions*/
Student::Result Student::registerToCourse(const Course& course){
cout << "Registereing to Course" << endl;
int old;
std::list<int>::iterator it = listofcourses.begin();
old = *it;
//add course id to the list of course at the right place (in order)
while (it != listofcourses.end() && *it > old) {
old = *it;
it++;
}
if (it == (it--))
{
cout << "you are already registered to this course! Cannot be registered to again."<<endl;
return Already_Registered;
}
listofcourses.insert(it, course.getNumber());
cout << "Registering to course done successfully" << endl;
return Ok;
}
Student::Result Student::unregisterFromCourse(constint number){
//delete from the list of courses
cout << "Unregistering from courseL" << endl;
//is registered? if not return not registered
std::list<int>::iterator it = listofcourses.begin();
//go over the list and search for the id given
while (it != listofcourses.end()) {
if (*it == number)
{
listofcourses.remove(number);
cout << "You are unregistered from this course!:)" << endl;
return Ok;
}
it++;
}
cout << "course was not found. Try another number" << endl;
return Not_Found;
}
bool Student::isRegistered(const Course& course){
cout << "Checking whether you are registered to this course." << endl;
std::list<int>::iterator it = listofcourses.begin();
//go over the list and search for the id given
while (it != listofcourses.end()) {
if (*it == course.getNumber()){
cout << "you are registered to this course" << endl;
returntrue;
}
it++;
}
cout << "you are not registered to this course" << endl;
returnfalse;
}
//print functions
Student& Student::print(std::ostream& os){
cout << "printing student's information:" << endl;
os << "first name:" << firstn;
os << '\n' << "last name: " << lastn;
os << '\n' << "id " << id;
os << '\n' << "sem" << sem;
os << '\n';
return *this;
}
Student& Student::printSchedule(std::ostream& os){
listofcourses.sort();
std::list<int>::iterator it = listofcourses.begin();
std::cout << "Printing list of courses:";
for (it = listofcourses.begin(); it != listofcourses.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return *this;
}
#ifndef _SCHEDULE_H_
#define _SCHEDULE_H_
#include <string>
#include <list>
#include <iostream>
#include "Course.h"
/*
* Class which holds list of courses and provides API to
* manage that list by adding, removing courses to it, as
* well provides API to retrive courses using course unique
* number.
*/
class Schedule {
private:
int num; //number of courses
int max; //the maximum size of the schedule list
std::list<int> courses;
// Container<Course*, std::allocator<Course*> > courses;
public:
/*
* Enumeration which indicates operation status for
* part of Schedule API.
*
* Ok - operation completed succesefully
* Failure - an error occured during operation
* Already_Exist - status which indicates fact of trying
* to add course to the list which already exist in the
* list
* Not_Found - trying to find course which doesn't
* exists in the list
*/
enum Result { Ok, Failure, Already_Exist, Not_Found };
/*
* Constructor which receives maximum size of the schedule
* list.
* HINT: Use array of Course to represent list.
*/
Schedule(int size);
/*
* Add new course to the list
*
* Return:
* Ok - on success
* Already_Exist - course already exists in the list
* Failure - undefined error situation
*/
Result addCourse(const Course& course);
/*
* Remove course from the list
*
* Return:
* Ok - course successefully removed
* Not_Found - trying to remove course which is not
* exist in the list
* Failure - undefined error situation
*/
Result removeCourse(constint number);
/*
* Clean courses list, e.g. remove all courses from the
* list.
*/
Schedule& removeAll();
/*
* Return array with all courses from schedule
*/
Course* getAllCourses(); //to be changed to the function with the container
/*
* Find course in the list with given course number
*
* Returns:
* 1. Course with given number in cases list holds such
* 2. NULL in case course was not found
*/
//to be changed to:
//{
/*
* Find course in the list with given course number
*
* Returns:
* 1. Course with given number in cases list holds such
* 2. Course::DUMMY_COURSE in case course was not found
*/// }
Course* findCourse(constint number);
/*
* See addCourse.
*/
Schedule& operator+=(const Course& course);
/*
* See removeCourse.
*/
Schedule& operator-=(const Course& course);
/*
* Return number of courses in the list
*/
int getCoursesNumber() const;
/*
* Output on the screen courses schedule list sorted by
* course number.
*/
Schedule& print(std::ostream& os);
};
#endif
#include "Schedule.h"
/*ctor*/
Schedule::Schedule(int size){
max=size;
}
/*operators*/
Schedule& Schedule::operator+=(const Course& course){
this->addCourse(course);
return *this;
}
Schedule& Schedule::operator-=(const Course& course){
this->removeCourse(course.getNumber());
return *this;
}
/*get function*/
int Schedule::getCoursesNumber() const {
return num;
}
/*print function*/
Schedule& Schedule::print(std::ostream& os){
courses.sort();
std::list<int>::iterator it = courses.begin();
std::cout << "list of courses:";
for (it = courses.begin(); it != courses.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return *this;
}
/*remove*/
Schedule::Result Schedule::removeCourse(constint number){
// delete from the list of courses
// is registered? if not return not registered
std::list<int>::iterator it = courses.begin();
// go over the list and search for the id given
while (it != courses.end()) {
if (*it == number)
{
courses.remove(number);
num = num - 1;
cout << "You have removed a course from your list of courses!" << endl;
return Ok;
}
it++;
}
cout << "Course was not found to be removed. Try again a different course number" << endl;
return Not_Found;
}
Schedule& Schedule::removeAll(){
std::list<int>::iterator it1 = courses.begin();
for (it1 = courses.begin(); it1 != courses.end(); ++it1)
courses.erase(it1);
num = 0;
cout << "You have removed all the courses from the list.";
return *this;
}
Schedule::Result Schedule::addCourse(const Course& course){
std::list<int>::iterator it = courses.begin();
if (findCourse(course.getNumber()) != NULL)
{
cout << "Course Already exists. Cannot be added to the list again." << endl;
return Already_Exist;
}
if (max > (num + 1))
{
courses.push_front(course.getNumber());
num += 1;
cout << "Course was added successfully." << endl;
return Ok;
}
else
{
cout << "You have reached the maximum size" <<endl;
return Failure;
}
}
Course* Schedule::getAllCourses(){
Course* array = new Course[courses.size()]; // create a dynamic array
std::copy(courses.begin(), courses.end(), array); // copy the data
return array;
}
Course* Schedule::findCourse(constint number){
Course* array = new Course[courses.size()]; // create a dynamic array
std::list<int>::iterator it = courses.begin();
std::copy(courses.begin(), courses.end(), array); // copy the data
while (it!=courses.end())
{
if (number == *it)
{
cout << "Course was found" << endl;
return array;
}
++it;
}
cout << "Course was not found." << endl;
return NULL;
}
Course* array = new Course[courses.size()]; ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ int* array = newint[courses.size()];
Why do you need to create new array? Can't you just iterate over courses member?
Well, you do not store Course in your class. All you store is ints. If you can create Course pointer from int (I assume int is a index for some global array), do so before returning.
this is why the array's type that i created was Course*
I didn't really undesrtand your suggestion! where should I create a course object? because i don't really need one in any other function!
Ok, forget about your code for a second.
Imagine you have your couses list inside Scedule class: std::list<int> courses = {1, 2, 3, 4, 5};
Your findCourse was called with number equal to 3
What your function should return?
I dont have the information, i just know the number in the list (this is connected to the information the user gives me when he adds a course with number 3)
Perfect. Now think about where would you get missing information? Or pointer to what will you return? (Why pointer. They are evil. Why not reference or smart pointer?)