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

C++StringStd

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

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 - C++

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 - C++

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 - C++

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 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStevenView Question on Stackoverflow
Solution 1 - C++Michael MrozekView Answer on Stackoverflow
Solution 2 - C++johnsywebView Answer on Stackoverflow
Solution 3 - C++quamranaView Answer on Stackoverflow
Solution 4 - C++BorealidView Answer on Stackoverflow
Solution 5 - C++crispengariView Answer on Stackoverflow