No match for operator[]

Discussion in 'C++' started by truste86, Jun 20, 2014.

  1. truste86

    truste86 New Member

    Joined:
    Jun 20, 2014
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi everybody, I have an assignment to create a grading system using multimap. It's not completed yet at the moment but whenever i try to compile it tells me "no match for 'operator[]' in lines 56,57,etc. I've scoured several help sites, and my textbook but i cant find any solutions. Can anyone help me? Thanks so much in advance!

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    
    class Student
    {
    public:
      //--- Constructor
      Student (int id = 0, double gpa = 0);
      //--- Output operator
      friend ostream & operator<<(ostream & out, const Student & s);
      //--- Input operator
      friend istream & operator>>(istream & in, Student & s);
      //--- Less-than operator
      friend bool operator<(const Student & a, const Student & b);
    private:
      //--- Data members
      int myId;
      double myGPA;
    };  // end of class Student
    
    //--- Definition of constructor
    inline Student::Student(int id, double gpa)
      : myId(id), myGPA(gpa)
    { }
    
    //--- Definition of input operator
    inline istream & operator>>(istream & in, Student & s)
    {
      in >> s.myId >> s.myGPA;
      
    }
    
    //--- Definition of output operator
    inline ostream & operator<<(ostream & out, const Student & s)
    { out << "id = " << s.myId << "  GPA = " << s.myGPA;
     return out;
    }
    
    //--- Definition of less-than operator
    inline bool operator<(const Student & a, const Student & b)
    { return a.myId < b.myId; }
    //================================================================
    
    int main()
    {
    	//typedef multimap<string, Student>grades
      typedef multimap< string, Student, less<string> > grades1;
      typedef multimap< Student, string, less<Student> > grades2;
    
      grades1 a1;   // associative array of Student, index type is string
      grades2 a2;   // associative array of string, index type is Student
    
      Student s;
      s = Student(12345, 3.3);  a1["Fred"] = s;   a2[s] = "Fred";
      s = Student(32322, 3.9);  a1["Al"] = s;     a2[s] = "Al";
      s = Student(13131, 2.5);  a1["Joan"] = s;   a2[s] = "Joan";
      s = Student(22121, 4.0);  a1["Barb"] = s;   a2[s] = "Barb";
      s = Student(28888, 2.9);  a1["George"] = s; a2[s] = "George";
      s = Student(19981, 3.0);  a1["Dot"] = s;    a2[s] = "Dot";
      s = Student(20012, 2.9);  a1["Sue"] = s;    a2[s] = "Sue";
      string name;
      cout << "Enter a name: ";
      cin >> name;
      grades1::iterator it1 = a1.find(name);
      cout << name << " has ";
      if (it1 == a1.end())
        cout << "no info";
      else
        cout << a1[name];
      cout << endl;
    
      Student aStudent;
      cout << "Enter a Student's id and GPA: ";
      cin >> aStudent;
      grades2::iterator it2 = a2.find(aStudent);
      cout << "Student " << aStudent << " is ";
    
      if (it2 == a2.end())
        cout << "no info";
      else
        cout << a2[aStudent];
      cout << endl;
    
    }
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    This error means that class multimap does not define operator[]; i.e. you cannot use the syntax you're trying to use on lines 56-62:
    Code:
    a1["Fred"] = s;
    
    If you want to do this then you'll have to define a new class derived from multimap which implements operator[].

    Also you can see here that there is no definition for operator[]:
    http://en.cppreference.com/w/cpp/container/multimap
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice