help with an error

Pages: 12
Hello everyone

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)

please help.
Thanks in advance
Last edited on

Posting our code would help better.
the first class:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#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
	const int getSemester() const; //return students semester number
	Student& setSemester(const int semester); //set value of students semester
	const int getId() const; //return students ID
	Student& setId(const int id);
	int getCoursesCount() const; //return number of courses student registered to

	/*regestering to courses functions*/
	Result registerToCourse(const Course& course);
	Result unregisterFromCourse(const int number);
	 bool isRegistered(const Course& course);

	/*print functions*/
	Student& print(std::ostream& os);
	Student& printSchedule(std::ostream& os);
	
	/*equality operator*/
	bool operator==(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



cpp file:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#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
const int Student::getSemester() const {
	cout << "getting semester" << endl;
	return sem;
}//return students semester number
Student& Student::setSemester(const int semester) {
	sem = semester;
	cout << "setting semester" << endl;
	return *this;
} //set value of students semester
const int Student::getId() const {
	cout << "getting id" << endl;
	return id;
} //return students ID
Student& Student::setId(const int 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)
	return true;

	else
		return false;

}


/*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(const int 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;
			return true;
		}
		it++;
	}
	cout << "you are not registered to this course" << endl;
	return false;
}

//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;
}



second class:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef _COURSE_H_
#define _COURSE_H_

#include <string>
#include <iostream>
using namespace std; 

/*
* Class which represents course at university. Course class is able to hold
* following procedural information such as:
*
* 1. Course Name
* 2. Course Number
* 3. Faculty course belongs to.
*/
class Course {
private:
	std::string _name;
	int _number;
	std::string _faculty;
public:
	/*
	* Course default constractor which doesn't need any parameters
	*/
	Course() { _name = "default name", _number = 0; _faculty = "none"; }

	/*
	* Course constractor with parameters:
	* Name - Course name
	* Number - Course number
	* Faculty - To which faculty course belongs to
	*/
	Course(const std::string& name, const int number, const std::string& faculty) {
		_name = name; _number = number; _faculty = faculty; 
	}


	/*Return course name value */
	const std::string getName() const;

	/*Set course name*/
	Course& setName(const std::string& name);

	/*Return course faculty name*/
	const std::string getFaculty() const;

	/*Set course faculty name*/
	Course& setFaculty(const std::string& faculty);

	/*Return course number*/
	const int getNumber() const;

	/*Set course numbe*/
	Course& setNumber(const int number);

	/*Print course information on output stream*/
	std::ostream& print(std::ostream& os) const;

	/*
	* Course equality operator
	* Return iff name faculty and course number
	* are the same for two instances.
	*/
	bool operator==(const Course& course) const;

	/*
	* Conversion to string operator, returns
	* string presentation of course internals,
	* such as name, number and faculty.
	*/
	operator std::string() { return _name + _faculty; }



};

#endif 


cpp file:
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
#include "Course.h"

/*set and get functions*/
const string Course::getName()const{
	return _name;
}
Course& Course::setName(const std::string& name){
	this->_name = name;
	return *this;
}
const string Course::getFaculty()const{
	return _faculty;
}
Course& Course::setFaculty(const std::string& faculty){
	this->_faculty = faculty;
	return *this;
}
const int Course::getNumber()const{
	return _number;
}
Course& Course::setNumber(const int number){
	this->_number = number;
	return *this;
}

/*print function*/
ostream& Course::print(std::ostream& os) const{
	cout << "Printing Course Information:" << endl;
	os << _name;
	os << _number;
	os << _faculty;
	return os;
}

/*operators*/
bool Course::operator==(const Course& course) const{
	if ((_name == course._name) && (_number == course._number) && (_faculty == course._faculty)){
		return true;
	}
	return false;
}
last class:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#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(const int 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(const int 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 



cpp:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#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(const int 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(const int 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;	
}
Last edited on
The problem is with your call to std::copy (lines 97 and 107 of Schedule.cpp)

Internally it uses operator= to assign values. However courses list contains ints and array contains Course objects.

Essentually you are doing something like Course foo = 5;
how can i create an array of type int and size=course.size()?
int array[courses.size()]; is not the right syntax.
 Course* array = new Course[courses.size()];   
  ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    int* array = new    int[courses.size()]; 
Why do you need to create new array? Can't you just iterate over courses member?
Last edited on
The problem now is that I have to return type Course*..
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?
Last edited on
course number 3
and what course it will be? Name, faculty?
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)
I dont have the information
Exactly. So you cannot return a Course (or a pointer to Course) without changinng your class definition.

Why it hold ints instead of Courses in the first place?
because the course is defined according to its number (which is an int)
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?)
Last edited on
can i declare t a reference and return course* at the same time? how is that possible?

I have never used smartpointers before and the TA told us not to because its going to complicate things and there's no need to.
The whole point of references is to return them instead of pointers.

Well, you can have pointer, but you should be able to convert int to Course. Preferably by storing whole courses instead of only int in your list.
Pages: 12