82

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

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Steven
  • 1,061
  • 2
  • 10
  • 14

5 Answers5

113

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

Community
  • 1
  • 1
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • 10
    @Michael Mrozek, @Steven: Moving `using namespace std;` into the header is a despicable act. Advising it doubly-so! – johnsyweb Apr 03 '11 at 07:22
  • 2
    @Johnsyweb Personally I hate `using namespace` altogether, but it's clearly what he intended to do – Michael Mrozek Apr 03 '11 at 18:10
  • 4
    @Michael: All the more reason to discourage him! – johnsyweb Apr 03 '11 at 22:54
  • 1
    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. – Eddy Pronk Apr 04 '11 at 13:45
  • 13
    @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" – Michael Mrozek Apr 04 '11 at 14:25
  • @MichaelMrozek: Why don't you edit your answer to make it clear it is bad practice? –  Apr 04 '11 at 14:45
  • @Will [This answer](http://stackoverflow.com/questions/5527665/weird-string-does-not-name-a-type-error-c/5527754#5527754) already does, and I think my answer is beyond saving at this point, it's been downvoted too many times – Michael Mrozek Apr 04 '11 at 14:46
  • @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. –  Apr 04 '11 at 14:48
  • 2
    @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 :-) – johnsyweb Apr 04 '11 at 20:02
48

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);
};
Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 2
    @Jonhsyweb: +1 for pointing out the perils of `using namespace` – Alok Save Apr 03 '11 at 05:34
  • should #include also be included in the header file in addition to it being included in the main .cpp file? – LazerSharks May 26 '13 at 07:20
  • @Gnuey: Since `#include ` 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. – johnsyweb May 26 '13 at 08:53
10

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.

quamrana
  • 37,849
  • 12
  • 53
  • 71
5

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.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
Borealid
  • 95,191
  • 9
  • 106
  • 122
4

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
crispengari
  • 7,901
  • 7
  • 45
  • 53