无法使用STL的字符串类

发布于 2024-08-21 03:01:15 字数 537 浏览 3 评论 0原文

以前也遇到过这个问题但是忘了怎么解决的了。

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-08-28 03:01:15

您想使用 std::string ,是吗?

您只是使用字符串。如果您有 using namespace ... 声明,则该方法有效,但在头文件中并不是一个好主意。

You want to be using std::string, yes?

You're just using string. Which works if you have a using namespace ... declaration, but isn't really a good idea in a header file.

野生奥特曼 2024-08-28 03:01:15

STL 中的每个标识符都位于 std 命名空间中。在执行 using namespace std;using std::string;typedef std::string xxx; 之前,必须调用它<代码>std::string。

正如其他人提到的,标头中的任何类型的 using 声明,尤其是在您自己的命名空间之外,都是一个坏主意。

因此,将 std::string 导入到您的类中:

class Model
{
    typedef std::string string;

    public:

Every identifier in the STL is in the std namespace. Until you do using namespace std;, using std::string;, or typedef std::string xxx;, it must be called std::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:

class Model
{
    typedef std::string string;

    public:
愁以何悠 2024-08-28 03:01:15

哦,std::string。顺便说一句,切勿在头文件中使用 using 命名空间。

ohhh, std::string. Never use the using namespace in a header file btw.

烟─花易冷 2024-08-28 03:01:15

除了其他答案提到的命名空间问题之外,您无法在其声明中将变量构造为类成员。假设你将其更改为

class Model {
    // ...
    std::string fun("alex");
};

This 在类中仍然是非法的,你不能在声明中赋值,你必须保留它:

class Model {
    // ...
    std::string fun;
};

如果你想在创建它时给它“alex”,请在构造函数中初始化它:

Model::Model(...)
    : fun("alex")  // initialiser
{
    // ...
}

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

class Model {
    // ...
    std::string fun("alex");
};

This is still illegal inside a class, you cannot assign a value in the declaration, you have to leave it:

class Model {
    // ...
    std::string fun;
};

And if you want to give it "alex" when it is created, initialise it in the constructor:

Model::Model(...)
    : fun("alex")  // initialiser
{
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文