game.cpp
game.h
game.h:8 error: ‘string’ does not name a type
game.h:9 error: ‘string’ does not name a type
Ваше объявление using находится в game.cpp , а не game.h , где вы фактически объявляете строковые переменные. Вы намеревались помещать using namespace std; в заголовок, над строками, использующими string , которые позволят этим линиям найти тип string , определенный в пространстве имен std .
В качестве других указали, это не хорошая практика в заголовках — все, кто включает в себя заголовок также невольно попадет в строку using и импортирует std в свое пространство имен; правильным решением является изменение этих строк для использования std::string вместо
string не указывает тип. Класс в заголовке string называется std::string .
Пожалуйста, не поместите using namespace std в заголовочный файл, он загрязняет глобальное пространство имен для всех пользователей этого заголовка. См. Также «Почему» using namespace std;» считается плохой практикой в С++?
Ваш класс должен выглядеть следующим образом:
Доброго времени суток, тостеровчане.
У меня есть два заголовочных файла с исходным кодом:
Собственно после попытки компиляции компилятор ругается следующим образом:
inchid
eport.h|97|error: ‘Mode’ in ‘class HidDevice::Device’ does not name a type|
Хотя, вроде как, для предотвращения именно этой ошибки я использовал forward declaration в файле report.h:
Что я делаю не так?
- Вопрос задан более трёх лет назад
- 581 просмотр
Потому что это так не работает. Forward declaration говорит компилятору только о том, что где-то у вас есть некоторый объявленный класс Х, оно не говорит, что внутри этого класса и какого он размера (т.е. вы можете хранить указатель на этот класс, но не его объект).
В вашем случае можно просто поменять порядок включения.
game.cpp
game.h
game.h:8 error: ‘string’ does not name a type
game.h:9 error: ‘string’ does not name a type

4 Answers 4
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
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:
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 at the top of your header file to make it more self contained.
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.

Not the answer you’re looking for? Browse other questions tagged c++ string std or ask your own question.
Related
Hot Network Questions
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.11.15.35459