Why am I getting string does not name a type Error?

289,120

Solution 1

Your using declaration is in game.cpp, not game.h where you actually declare string variables. You intended to put using namespace std; into the header, above the lines that use string, which would let those lines find the string type defined in the std namespace.

As others have pointed out, this is not good practice in headers -- everyone who includes that header will also involuntarily hit the using line and import std into their namespace; the right solution is to change those lines to use std::string instead

Solution 2

string does not name a type. The class in the string header is called std::string.

Please do not put using namespace std in a header file, it pollutes the global namespace for all users of that header. See also "Why is 'using namespace std;' considered a bad practice in C++?"

Your class should look like this:

#include <string>

class Game
{
    private:
        std::string white;
        std::string black;
        std::string title;
    public:
        Game(std::istream&, std::ostream&);
        void display(colour, short);
};

Solution 3

Just use the std:: qualifier in front of string in your header files.

In fact, you should use it for istream and ostream also - and then you will need #include <iostream> at the top of your header file to make it more self contained.

Solution 4

Try a using namespace std; at the top of game.h or use the fully-qualified std::string instead of string.

The namespace in game.cpp is after the header is included.

Solution 5

You can overcome this error in two simple ways

First way

using namespace std;
include <string>
// then you can use string class the normal way

Second way

// after including the class string in your cpp file as follows
include <string>
/*Now when you are using a string class you have to put **std::** before you write 
string as follows*/
std::string name; // a string declaration
Share:
289,120

Related videos on Youtube

Steven
Author by

Steven

Updated on July 05, 2022

Comments

  • Steven
    Steven almost 2 years

    game.cpp

    #include <iostream>
    #include <string>
    #include <sstream>
    #include "game.h"
    #include "board.h"
    #include "piece.h"
    
    using namespace std;
    

    game.h

    #ifndef GAME_H
    #define GAME_H
    #include <string>
    
    class Game
    {
        private:
            string white;
            string black;
            string title;
        public:
            Game(istream&, ostream&);
            void display(colour, short);
    };
    
    #endif
    

    The error is:

    game.h:8 error: 'string' does not name a type
    game.h:9 error: 'string' does not name a type

  • Alok Save
    Alok Save about 13 years
    @Jonhsyweb: +1 for pointing out the perils of using namespace
  • johnsyweb
    johnsyweb about 13 years
    @Michael Mrozek, @Steven: Moving using namespace std; into the header is a despicable act. Advising it doubly-so!
  • Michael Mrozek
    Michael Mrozek about 13 years
    @Johnsyweb Personally I hate using namespace altogether, but it's clearly what he intended to do
  • johnsyweb
    johnsyweb about 13 years
    @Michael: All the more reason to discourage him!
  • Eddy Pronk
    Eddy Pronk about 13 years
    From the book C++ Coding Standards (Sutter) - "Don't write namespace usings in a header file or before an #include" It is not about style but about danger.
  • Michael Mrozek
    Michael Mrozek about 13 years
    @Johnsyweb I hate when I search the internet for a problem, see someone who's asked the same question, and all the answers are "no, don't do that" -- I answer the question that was asked. I should've mentioned that it's a bad idea, yes, but I refuse to just say "no, it's impossible"
  • Admin
    Admin about 13 years
    @MichaelMrozek: Why don't you edit your answer to make it clear it is bad practice?
  • Michael Mrozek
    Michael Mrozek about 13 years
    @Will This answer already does, and I think my answer is beyond saving at this point, it's been downvoted too many times
  • Admin
    Admin about 13 years
    @MichaelMrozek: Since it is the accepted answer, I hesitate to delete. Editing would appease the good-practices gods without destroying content. Anyhow, you've gotten a total of 32 rep out of this, so it isn't all bad. If you're absolutely sure you want to delete, I'll do it.
  • johnsyweb
    johnsyweb about 13 years
    @Michael: Thank you for the edit. I hate searching the web for the solution to a problem only to find that the top hit is a hack. +1 :-)
  • LazerSharks
    LazerSharks almost 11 years
    should #include <string> also be included in the header file in addition to it being included in the main .cpp file?
  • johnsyweb
    johnsyweb almost 11 years
    @Gnuey: Since #include <string> is required for any compilation unit including this header, I would include that line, yes. There is no need to repeat this directive in any subsequent source file.