Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

string

std

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

like image 406
Steven Avatar asked Apr 03 '11 04:04

Steven


People also ask

What does cout does not name a type mean?

You're missing your main. The code is outside of a function and is considered by the compiler to be either a declaration of variables, class, structs or other such commands.

How do you include a string?

In C++, you should use the string header. Write #include <string> at the top of your file. When you declare a variable, the type is string , and it's in the std namespace, so its full name is std::string .


1 Answers

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

like image 50
Michael Mrozek Avatar answered Sep 28 '22 03:09

Michael Mrozek