Thread: wacko map error

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    wacko map error

    I can't seem to declare a map in certain places:
    Code:
    map<string,int> testdata;
    is fine at the top of main(), but I get one of two kinds of error when I use it in an included file:

    error: ISO C++ forbids declaration of ‘map’ with no type

    I tried adding spaces inside the angle brackets, same thing. The other error is:

    error: expected primary-expression before ‘,’ token
    error: expected primary-expression before ‘>’ token


    which is what I would get if I failed to #include<map>, except then I would also get "error: ‘map’ was not declared in this scope". Everything is within scope of using namespace std.

    Anyone seen this before? I'm using gcc 4.3.2. I thot maybe I had a bracket missing somewhere, but it compiles and runs fine...just I can't use a map
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The usual thing: provide a simple compilable example etc etc etc.

    (EfC: I mean, I can make that happen if I forget #include <map> or something, or miss out the namespace. But that (apparently) won't help figure out what you did.)
    Last edited by tabstop; 03-29-2010 at 08:58 AM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    The usual thing: provide a simple compilable example etc etc etc.
    Well, that's why I started moving it around, since there's a few hundred lines in there that are irrelevent AFAICT, everything has been fine up to now. I guess I'll make a copy and start tearing it down...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Has the string header been included in this header/include file? What's the namespace sitch?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hk_mp5kpdw View Post
    Has the string header been included in this header/include file? What's the namespace sitch?
    Yeah, I was adding a map to an existing class which is otherwise fine, tons of vector and string in it already. All the code compiles and runs fine, I just can't use any kind of map.

    What's a "sitch"?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The included file is hopefully a header. It should include <map> and <string>, not use using namespace std; and say std::map<std::string, int>.

    Also perhaps check code before it (missing semicolons and such).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by anon View Post
    The included file is hopefully a header. It should include <map> and <string>, not use using namespace std; and say std::map<std::string, int>.
    No, it's not a header, it's the whole class, it is not a generic library, it will never be compiled by anyone for any purpose other than the exact purpose it's used in this program, and might as well use use namespace std like everything else. As already mentioned, there is plenty of code there already using string and the rest of the STL and it works fine. It's just map.

    The only reason it's not in the main file is just to save clutter, there is no other significance. In fact, I tried putting it all into one file (yes, with #include<map> at the top, etc), exact same issue...now I guess just keep tearing it down. The fact that it's in another file doesn't matter.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, I've got it narrowed down to this, and it seems to hinge on the fact that "MainWindow" is a derived class (nb, that is the recommended method and as mentioned, works fine except for the map!!!):
    Code:
    #include <gtkmm.h>
    #include <map>
    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class MainWindow : public Gtk::Window {
    //class MainWindow {
    	public:
    		vector<string> okay;
    		map<string,int > testdata;
    		MainWindow(vector<string> &exc);
    };
    
    int main(int argc, char *argv[]) {
    	map<string,int> testdata;
    
    	return 0;
    }
    The map in main() is fine. If I switch the //comment around so MainWindow is not derived, it's fine. The vector<string> in MainWindow is fine either way. The error is:

    copy.cpp:13: error: ISO C++ forbids declaration of ‘map’ with no type
    copy.cpp:13: error: expected ‘;’ before ‘<’ token


    Again, I've tried placing spaces inside <>, etc. What's going on? This happens on gcc 4.4.1 as well.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. My guess is that Gtk::Window has a member named map that introduces a name conflict. A likely fix is to qualify std::map in the class definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by MK27 View Post
    Okay, I've got it narrowed down to this, and it seems to hinge on the fact that "MainWindow" is a derived class (nb, that is the recommended method and as mentioned, works fine except for the map!!!):
    Code:
    #include <gtkmm.h>
    #include <map>
    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class MainWindow : public Gtk::Window {
    //class MainWindow {
    	public:
    		vector<string> okay;
    		map<string,int > testdata;
    		MainWindow(vector<string> &exc);
    };
    
    int main(int argc, char *argv[]) {
    	map<string,int> testdata;
    
    	return 0;
    }
    The map in main() is fine. If I switch the //comment around so MainWindow is not derived, it's fine. The vector<string> in MainWindow is fine either way. The error is:

    copy.cpp:13: error: ISO C++ forbids declaration of ‘map’ with no type
    copy.cpp:13: error: expected ‘;’ before ‘<’ token


    Again, I've tried placing spaces inside <>, etc. What's going on? This happens on gcc 4.4.1 as well.
    My bet is that Gtk::Window contains something named "map". Try using "std::map" anyway rather than just "map". Does that make a difference?

    EDIT: lol... laserlight beat me and said it pretty much the same way. I didn't steal it from him though :P.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, thanks gang.

    That seems to be kind of a bad oversight, isn't it -- altho I guess STL containers are not keywords...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by MK27 View Post
    Yeah, thanks gang.

    That seems to be kind of a bad oversight, isn't it -- altho I guess STL containers are not keywords...
    This is exactly why I dislike using the "using" keyword. I never use it, unless I don't care one bit about code quality. I know people disagree with me on this, though.

    I've found the definition as well. In widget.h:
    Code:
      /** This function is only for use in widget implementations. Causes
       * a widget to be mapped if it isn't already.
       */
      void map();

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. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM