Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Map Question 1

Status
Not open for further replies.

Azathoth

Technical User
Jul 14, 2003
61
US
I'm trying to create a map of two different classes, but can't seem to set a value to a key. Here's my code:

Code:
#include <map>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

class Student {
public:
   Student(const string& si, const vector <string>& c) : student_id(si), courses(c) {}

   const string& getStudentId() const { return student_id; }
   void setStudentId(string si) { student_id = si; };

private:
   string student_id;
   vector <string> courses;
};


class Course {
public:
   Course(const string& cc, const int& ts) : course_code(cc), time_slot(ts) {}
   
   const string& getCourseCode() const { return course_code; }
   void setCourseCode(string cc) { course_code = cc; };

private:
   string course_code;
   int time_slot; 
};


   bool operator< (const Course& left, const Course& right)
   {   return left.getCourseCode() < right.getCourseCode();   }

   bool operator< (const Student& left, const Student& right)
   {   return left.getStudentId() < right.getStudentId();   }

int main ()
{
   vector <string> v1;  v1.push_back("2"); v1.push_back("3");
   Student s1 = Student("1", v1);
   Course c1 = Course("6", 7);

   map < Student, Course > schedule;
   map < Student, Course > :: iterator map_itr;
   string :: iterator string_itr;
   vector <string> vector_itr;

   map_itr = schedule.begin();
   schedule[s1] = c1;  //This line produces the error.

   return 0;
}

The error is "no matching function call to Course()" - any ideas?
 
The problem is that Course doesn't have a default constructor which is required if you're gonna use map's [] operator.

/Per

www.perfnurt.se
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top