Jeremy Beare
Jeremy Beare

Reputation: 487

C++ 'string' does not name a type in header

Eugh, 2 problems in one day. I am having one of those bad days you hear so much about. I have been organizing my small project to make it less cluttered. It's at the start of the development so there isn't much going on. I have this header below

#pragma once
#include <string>

class Game_Map
{
    private:
        int map_width;
        int map_height;
        string map_data [50][50]
    public:
        Game_Map(int default_width = 20,int default_height = 20)
        ~Game_Map()
};

Now as far as I can see, there shouldn't be any problems. I avoided using the "using" and I kept the programming up til now at the basic to prevent external interference. But am I 100% of the time getting "map.h:9:9: error: 'string' does not name a type"

I am certain I have missed something. Can anyone see where I have gone wrong?

Upvotes: 1

Views: 2735

Answers (3)

Nemanja Trifunovic
Nemanja Trifunovic

Reputation: 24561

Change

string map_data [50][50]

to

std::string map_data [50][50];

That's necessary because string belongs to the std namespace.

Don't use "using" declaration or directive in a header file.

Upvotes: 9

Mr.C64
Mr.C64

Reputation: 43014

You seem to miss some semicolons at the end of some lines here:

class Game_Map
{
...
    string map_data [50][50]  // Missing ;
public:
    Game_Map(int default_width = 20,int default_height = 20)  // Missing ;
    ~Game_Map() // Missing ;
};

Moreover, when you #include <string>, the "complete name" to identify the STL string class is std::string (since the STL string class is located under the std:: namespace).

So, just use std::string instead of string in this line:

std::string map_data[50][50];

Note that in header files you shouldn't use "using directives" (e.g. using namespace std;), to avoid to "pollute" the global namespace of clients that #include your header file. Just specify the STL class names with their std:: prefix in header files.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

Use either a fully qualified name (preferable) as

std::string map_data [50][50];

or use using declaration

using std::string;
string map_data [50][50];

or using directive

using namespace std;
string map_data [50][50]'

Upvotes: 1

Related Questions