开发者

C++ g++ can't find 'string' type in class header file

开发者 https://www.devze.com 2023-02-08 05:47 出处:网络
I\'m new to C++ but I can\'t figure out why this won\'t compile for me. I\'m running on a Mac, coding with Xcode, but I\'m building with my own makefile from bash.

I'm new to C++ but I can't figure out why this won't compile for me. I'm running on a Mac, coding with Xcode, but I'm building with my own makefile from bash.

Anyway, I'm getting two compiler errors that "string" type can't be found, even though I've included . Any help would be welcomed. Code:

//#include <string> // I've tried it here, too. I'm foggy on include semantics, but I think it should be safe inside the current preprocessor "branch"
#ifndef APPCONTROLLER_H
#define APPCONTROLLER_H

#include <string>
class AppController {
// etc.
public:
    int processInputEvents(string input); //error: ‘string’ has not been declared
    string prompt(); //error: ‘string’ does not name a type
};
#endif

I include this file in my main.cpp, and elsewhere in main I use the string type and it works just fine. Though in main I have included iostream instead of string (for other purposes). Yes, I've also tried including iostream in my AppController class but it didn't solve anything (I didn't really expect it to, either).

开发者_JS百科

So I'm not really sure what the problem is. Any ideas?


string is in the std namespace.

#include <string>
...
std::string myString;

Alternatively you can use

using namespace std;

However, this is a very bad thing to do in headers as it will pollute the global namespace for anyone that includes said header. It's ok for source files though. There is an additional syntax you can use (that has some of the same problems as using namespace does):

using std::string;

This will also bring the string type name into the global namespace (or the current namespace), and as such should generally be avoided in headers.

0

精彩评论

暂无评论...
验证码 换一张
取 消