error: ISO C++ forbids declaration of 'string' with no type

This is a discussion on error: ISO C++ forbids declaration of 'string' with no type within the C++ Programming forums, part of the General Programming Boards category; Hello all, I am receiving this error when I try to compile. Tags.h Code: #ifndef _TAGS_H #define _TAGS_H using namespace ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    error: ISO C++ forbids declaration of 'string' with no type

    Hello all,
    I am receiving this error when I try to compile.

    Tags.h
    Code:
    #ifndef _TAGS_H
    #define _TAGS_H
    
    using namespace std;
    
    class Tags
    {
      public:
    
      void splitStart(const string divider, Tags *first);
     ...
    }
    Why would I be getting this error?

    Thanks,
    Michael

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Probably because you didn't include "<string>" before the file.

    But DON'T DON'T DON'T use "using namespace std;" or any using statement in a header file or before you included all header files. Just write "std::string".
    Actually, don't do it, in a few projects you'll hit a big wall yourself meaning you'll have to change every header file you made up to that point. That should teach you for the rest of your life; better than me just saying don't do it.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    The way you've got it set up now, your header requires that the <string> header be included prior to this header. This forces an ordering of headers in source files which should be avoided as it may or may not compile simply according to the order you're including such headers.

    If a header has some object/type that requires a header then that header should be included. This ensures that no matter the ordering of your include statements in the source file, you'll always be able to compile.

    You should also avoid any using namespace std declaration in a header and explicitly qualify any needed objects.

    Try:
    Code:
    #ifndef _TAGS_H
    #define _TAGS_H
    
    #include <string>
    
    class Tags
    {
      public:
    
      void splitStart(const std::string divider, Tags *first);
     ...
    }
    "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

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    96
    Also, why are you taking a string by const value? Do you really want a *copy* of it?
    Since you are passing a const value you can't modify the value anyway. Might as well pass by ref and save some overhead...

    Code:
    void splitStart(const std::string& divider, Tags *first);

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Quote Originally Posted by hk_mp5kpdw View Post
    The way you've got it set up now, your header requires that the <string> header be included prior to this header. This forces an ordering of headers in source files which should be avoided as it may or may not compile simply according to the order you're including such headers.

    If a header has some object/type that requires a header then that header should be included. This ensures that no matter the ordering of your include statements in the source file, you'll always be able to compile.

    You should also avoid any using namespace std declaration in a header and explicitly qualify any needed objects.

    Try:
    Code:
    #ifndef _TAGS_H
    #define _TAGS_H
    
    #include <string>
    
    class Tags
    {
      public:
    
      void splitStart(const std::string divider, Tags *first);
     ...
    }

    Hello and thank you all.

    Why not use "using namespace std;"??? I've seen this before, but never known or heard why not to use it.

    Thanks,
    Michael

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by TheBigOnion
    Why not use "using namespace std;"??? I've seen this before, but never known or heard why not to use it.
    Because your header file could be included in source files that do not expect this using directive to be present, thus resulting in a name conflict that might be a compile error, or might even silently alter the meaning of the program.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Do you really want a *copy* of it
    If the class std::string is implemented following the COW philosophy it doesn't create a copy unless you modify the string. But I'm not sure if the string class is always implemented that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forbids Declaration of '___' with no type
    By warfang in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2007, 12:01 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM

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