. Advertisement .
..3..
. Advertisement .
..4..
How to solve the problem – error: ‘string’ does not name a type in c++? I have the sample detail:
#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"
using namespace std;
#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
While I was running it, I found the warning message:
game.h:8 error: 'string' does not name a type
game.h:9 error: 'string' does not name a type
That is my question in my midterm exam, and it is urgent. I searched the solutions on some websites, but I didn’t get it. I may miss any line or other changes. I appreciate your assistance!
The cause:
game.cpp
is the place of yourusing
declaration, but is notgame.h
which you actually declare string variable declarations. Looks like you want to putusing namespace std;
in the header abovestring
lines. This will allow those lines to find thestring
type that is defined in thestd
namespace.Others have already pointed out that and this is bad practice in headings because everyone who includes that header involuntarily hits the
using
line, and then importsstd
into their own namespace. Therefore, the error occurs.Solution:
To solve this error, you need to replace those lines with
std::string
.string
doesn’t name any type.std::string
is the class name for the class listed instring
‘s header.Please Do not place
using namespace std
into a header file. It pollutes all users of the header’s global namespace. Also see “Why is ‘using the namespace std’ considered a bad practice for C++?” ”This is how your class should look: