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

5

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

Unsightly answered 3/4, 2011 at 4:54 Comment(0)
I
115

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

Imbricate answered 3/4, 2011 at 4:55 Comment(9)
@Michael Mrozek, @Steven: Moving using namespace std; into the header is a despicable act. Advising it doubly-so!Keewatin
@Johnsyweb Personally I hate using namespace altogether, but it's clearly what he intended to doImbricate
@Michael: All the more reason to discourage him!Keewatin
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.Diplocardiac
@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"Imbricate
@MichaelMrozek: Why don't you edit your answer to make it clear it is bad practice?Intestine
@Will This answer already does, and I think my answer is beyond saving at this point, it's been downvoted too many timesImbricate
@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.Intestine
@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 :-)Keewatin
K
50

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);
};
Keewatin answered 3/4, 2011 at 5:17 Comment(3)
@Jonhsyweb: +1 for pointing out the perils of using namespaceStolzer
should #include <string> also be included in the header file in addition to it being included in the main .cpp file?Graecize
@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.Keewatin
S
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.

Sayer answered 3/4, 2011 at 7:36 Comment(0)
A
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.

Acis answered 3/4, 2011 at 4:55 Comment(0)
G
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
Gasser answered 28/10, 2020 at 8:20 Comment(1)
Second way is the correct way. Don't muck up the namespace.Ouachita

© 2022 - 2024 — McMap. All rights reserved.