Map member of class produces error - why?

This is a discussion on Map member of class produces error - why? within the C++ Programming forums, part of the General Programming Boards category; I have a map member of a class. Inside a function of the class, I call nameOfMap.insert(), and the compiler ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Map member of class produces error - why?

    I have a map member of a class.
    Inside a function of the class, I call nameOfMap.insert(), and the compiler gives an error saying:

    error: expected unqualified-id before '.' token
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    You should understand that we can't tell anything unless you show the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    You should understand that we can't tell anything unless you show the code.
    Code:
    #include <map>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    class CaClass {
    
      public:
        CaClass(); //constructor
        void doStuff();
    
      private:
        typedef map<unsigned int, string> aMap;
        vector<string> aStringVector;
        vector<unsigned int> aUnsignedIntVector;
    
    };
    
    CaClass::CaClass() {
    
      //nothing in here that deals with the map....
    
    }
    
    void CaClass::doStuff() {
    
      unsigned int currentNum = aUnsignedIntVector.at(0);
      unsigned int i = 0;
      do {
             aMap.insert(aMap::value_type(currentNum - 1, aStringVector.at(currentNum - 1))); //note that I'm using currentNum - 1 here, since every element of the unsigned int vector is 1 more than the corresponding element in the string vector
             currentNum = aUnsignedIntVector.at(i+1);
             i++;
      while(i < aUnsignedIntVector.size());
    
    }
    Last edited by Programmer_P; 06-18-2010 at 10:05 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Where are the template arguments for map?
    std::map<Key, Value>
    Also, you could easily write this...
    Code:
    aMap.insert(aMap::value_type(currentNum - 1, aStringVector.at(currentNum - 1)));
    ...as this:
    Code:
    aMap[currentNum - 1] = aStringVector.at(currentNum - 1);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Where are the template arguments for map?
    std::map<Key, Value>
    Yeah, I noticed that before you posted.
    My bad. I already fixed it in my last post.
    I meant to make the map declaration in the class look like this:
    Code:
    typedef map<unsigned int, string> aMap;
    Because, that's how it is in the real code.
    Also, you could easily write this...
    Code:
    aMap.insert(aMap::value_type(currentNum - 1, aStringVector.at(currentNum - 1)));
    ...as this:
    Code:
    aMap[currentNum - 1] = aStringVector.at(currentNum - 1);
    Thanks, I will try that.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,800
    Code:
    aMap.insert(aMap::value_type(currentNum - 1, aStringVector.at(currentNum - 1)));
    O_o

    According to the code, `aMap' is a type; it is not an instance. Is that really your code?

    Soma

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, using your example changed the error message to this instead:

    error: expected unqualified-id before '[' token

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by phantomotap View Post
    O_o

    According to the code, `aMap' is a type; it is not an instance. Is that really your code?

    Soma
    Yes, because I'm copying the style of someone else (actually 'Bubba' on these forums) when writing a map because I don't know much about maps. I originally tried the code with the map declaration looking like this instead:

    Code:
    map <unsigned int, string> aMap;
    But then the compiler printed out a message saying something like the map wasn't a class type, so I changed it to use a typedef. Anyway, I just changed it back to that after reading your post, and now its getting past that error, so it appears that it is solved.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    When you encounter syntax errors, you must post the code or we cannot help you.
    Using a typedef or not doesn't matter. If it works without the typedef and not with it, then you're doing something wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    When you encounter syntax errors, you must post the code or we cannot help you.
    Using a typedef or not doesn't matter. If it works without the typedef and not with it, then you're doing something wrong.
    Well, apparently it did matter using the typedef in this case, since once I changed the insert calls in my code to using the '[]' and '=' operators instead, like in your example, the compiler still gave an error message on the map line, when I was still using typedef. Then, following phantom's post, I removed the 'typedef' before the map declaration inside the class, and now the compiler doesn't give me any error on the same line of code adding stuff to the map.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Like I said, if it works without the typedef but not with it, then you have an error in your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Like I said, if it works without the typedef but not with it, then you have an error in your code.
    Maybe. I've been wondering if I need to call the constructor of the map object inside my class constructor first before I try calling a member function of map.
    For some reason, though, I thought an object of map would automatically be created by the compiler when you use the '.' operator to access a member function of my member map.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Well here's the problem.

    aMap is either a type, in which stuff like this is valid:

    aMap::value_type (foo);

    Or it is an instance, which lets you insert things:

    aMap[bar] = foo;

    It cannot be both.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Quote Originally Posted by Programmer_P View Post
    Maybe. I've been wondering if I need to call the constructor of the map object inside my class constructor first before I try calling a member function of map.
    For some reason, though, I thought an object of map would automatically be created by the compiler when you use the '.' operator to access a member function of my member map.
    The constructor of the map is invoked before the constructor body of your class.
    The map is ready to use since it's an object inside your class (not a pointer nor a reference).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Well here's the problem.

    aMap is either a type, in which stuff like this is valid:

    aMap::value_type (foo);

    Or it is an instance, which lets you insert things:

    aMap[bar] = foo;

    It cannot be both.
    And yet it would not let me use the insert() function to insert things into the map, when it was just an instance and not a typedef. It was only when I changed it to use the '[]' and '=' operators, like in your example, that it got past the compiler error. Why is that?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Replies: 6
    Last Post: 04-27-2006, 10:55 AM
  4. Is it possible to have callback function as a class member?
    By Aidman in forum Windows Programming
    Replies: 11
    Last Post: 08-01-2003, 11:45 AM
  5. SystemTray class
    By jair in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2003, 06:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21