无法使用STL的字符串类
以前也遇到过这个问题但是忘了怎么解决的了。
我想使用 STL 字符串类,但编译器抱怨找不到它。 这是完整的 .h 文件。
#ifndef MODEL_H
#define MODEL_H
#include "../shared/gltools.h" // OpenGL toolkit
#include <math.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "Types.h"
class Model
{
public:
obj_type_ptr p_object;
char Load3DS (char *p_filename);
int LoadBitmap(char *filename);
int num_texture;
string fun("alex");
Model(char* modelName, char* textureFileName);
};
#endif
Encountered this problem before but forgot how I solved it.
I want to use the STL string class but the complier is complaining about not finding it.
Here is the complete .h file.
#ifndef MODEL_H
#define MODEL_H
#include "../shared/gltools.h" // OpenGL toolkit
#include <math.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "Types.h"
class Model
{
public:
obj_type_ptr p_object;
char Load3DS (char *p_filename);
int LoadBitmap(char *filename);
int num_texture;
string fun("alex");
Model(char* modelName, char* textureFileName);
};
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想使用
std::string
,是吗?您只是使用
字符串
。如果您有using namespace ...
声明,则该方法有效,但在头文件中并不是一个好主意。You want to be using
std::string
, yes?You're just using
string
. Which works if you have ausing namespace ...
declaration, but isn't really a good idea in a header file.STL 中的每个标识符都位于
std
命名空间中。在执行using namespace std;
、using std::string;
或typedef std::string xxx;
之前,必须调用它<代码>std::string。正如其他人提到的,标头中的任何类型的
using
声明,尤其是在您自己的命名空间之外,都是一个坏主意。因此,将
std::string
导入到您的类中:Every identifier in the STL is in the
std
namespace. Until you dousing namespace std;
,using std::string;
, ortypedef std::string xxx;
, it must be calledstd::string
.Any kind of
using
declaration in a header, especially outside your own namespace, is a bad idea, as others have mentioned.So, import
std::string
into your class:哦,std::string。顺便说一句,切勿在头文件中使用 using 命名空间。
ohhh, std::string. Never use the using namespace in a header file btw.
除了其他答案提到的命名空间问题之外,您无法在其声明中将变量构造为类成员。假设你将其更改为
This 在类中仍然是非法的,你不能在声明中赋值,你必须保留它:
如果你想在创建它时给它“alex”,请在构造函数中初始化它:
As well as the namespace issue mentioned by other answers, you can't construct a variable at its declaration as a class member. Assuming you changed it to
This is still illegal inside a class, you cannot assign a value in the declaration, you have to leave it:
And if you want to give it "alex" when it is created, initialise it in the constructor: