Thread: C++ Map - Error in simple code

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    10

    C++ Map - Error in simple code

    Hi

    I can't figure out why this short piece is giving an error at the point where I add an element to the map. Any ideas much appreciated, thanks in advance.


    Code:
    #include <map>
    using namespace std;
    
    class component{
    int failureRate;
    public:
           int getFailrate(){
               return failureRate;
               };
           component(int Ifail):failureRate(Ifail){};
    };
    
    int main(){
        component comp(8);
       map<int,component> compMap;
    
       compMap[8] = comp; // this line is giving a compilation error!
        
        while(1);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Because you have no default constructor. Add this line to the component:
    Code:
    component()
    {
    }
    and remove the ; after method bodies of getFailrate() and component(int Ifail).
    Or, you can add the default value for the existing constructor.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    Thanks for the help Karas, it now compiles.

    Would you be able to explain to me why I need the default constructor, since the component Object had already been initialised when I attempted to add it to the map?

    Also why does this code in main() fail :

    Code:
    int temp = compMap[8].getFailrate;
    thanks again

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Code:
    int temp = compMap[8].getFailrate();  // () added

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    thanks and apologies as I should of spotted that.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    Because you are using the operator= for the class component (you didn't defined any operator= for this class, so compiler did it). operator= requires that class has default constructor.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> operator= requires that class has default constructor.
    This is not true. You can have the operator= without a default constructor.

    The problem is with the use of operator[] for the map. The map's operator[] creates a new entry in the map if one does not exist. This is true even if you are attempting to assign a new entry. So in the original code, compMap[8] = comp attempts to insert a new entry with a key of 8 and the value of a default constructed component, then assign the comp variable as a new value for that key.

    If your class does not need a default constructor, don't create one just for this problem. Use insert instead of operator[] with the map:
    Code:
    compMap.insert(std::make_pair(8, comp));

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Since he is actually assigning a value on creation of the "component" object for the map, wouldn't he need a copy constructor instead ?
    Code:
    class component {
      public:
        component(const component& copy) {
          failureRate = copy.failureRate;
        }
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird error in this simple code! C2248!
    By gross100 in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2005, 01:31 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM