在类头文件中声明字符串数组 - 编译器认为字符串是变量名?

发布于 2024-08-30 01:11:02 字数 870 浏览 5 评论 0原文

大家好,我需要一些帮助来在 C++ 类头文件中声明字符串数组。

atm 看起来像这样:

//Maze.h
#include <string>

class Maze

{
    GLfloat mazeSize, mazeX, mazeY, mazeZ;
    string* mazeLayout;

public:
    Maze ( );
    void render();
};

构造函数看起来像这样:

//Maze.cpp
#include <GL/gl.h>
#include "Maze.h"
#include <iostream>
#include <fstream>

Maze::Maze( )
{
    cin >> mazeSize;
    mazeLayout = new string[mazeSize];

    mazeX = 2/mazeSize;
    mazeY = 0.25;
    mazeZ = 2/mazeSize;
}

我收到一个编译器错误,上面写着:

In file included from model-view.cpp:11:
Maze.h:14: error: ISO C++ forbids declaration of ‘string’ with no type
Maze.h:14: error: expected ‘;’ before ‘*’ token

对我来说唯一有意义的是,出于某种原因,它认为我希望字符串作为变量名而不是类型声明。

如果有人能帮助我,那就太棒了,我已经查了一段时间了,它让我大吃一惊,哈哈。

干杯,伙计们

Hey everybody, I need a bit of a hand with declaring a string array in my class header file in C++.

atm it looks like this:

//Maze.h
#include <string>

class Maze

{
    GLfloat mazeSize, mazeX, mazeY, mazeZ;
    string* mazeLayout;

public:
    Maze ( );
    void render();
};

and the constructor looks like this:

//Maze.cpp
#include <GL/gl.h>
#include "Maze.h"
#include <iostream>
#include <fstream>

Maze::Maze( )
{
    cin >> mazeSize;
    mazeLayout = new string[mazeSize];

    mazeX = 2/mazeSize;
    mazeY = 0.25;
    mazeZ = 2/mazeSize;
}

I'm getting a compiler error that says:

In file included from model-view.cpp:11:
Maze.h:14: error: ISO C++ forbids declaration of ‘string’ with no type
Maze.h:14: error: expected ‘;’ before ‘*’ token

and the only sense that makes to me is that for some reason it thinks I want string as a variable name not as a type declaration.

If anybody could help me out that would be fantastic, been looking this up for a while and its giving me the shits lol.

Cheers guys

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不喜欢何必死缠烂打 2024-09-06 01:11:03

您需要限定名称:std::string

除此之外,您的类设计没有正确地分离关注点:类不应该直接询问用户输入。这应该是类构造函数的参数——实际的输入处理应该在类之外。

此外,我希望您正确删除在类析构函数中分配的内存。最好使用原始指针。请改用 std::vector 。这更容易、更安全。

You need to qualify the name: std::string.

Apart from that, you class design doesn’t separate concerns properly: the class should not ask user input directly. That should be a parameter to the class constructor – the actual input handling should be outside the class.

Furthermore, I hope you’re properly deleting the memory you allocated in the class destructor. It would be better not to use a raw pointer. Use a std::vector<std::string> instead. This is much easier and safer.

a√萤火虫的光℡ 2024-09-06 01:11:03

使用 C++ STL SGI。

矢量文档:http://www.sgi.com/tech/stl/Vector.html

字符串文档:http://www.sgi.com/tech/stl /basic_string.html

您可以像普通数组一样访问向量,因为他重载了[]运算符
但是,如果您可以将 intems 添加到向量中,则必须通过 insert 函数来完成,例如:

v.insert(v.end(), new_item);

此代码在向量 v 的末尾添加一个 new_item

您使用 std::vector 进行向量声明。

Use C++ STL SGI.

Vector documentation: http://www.sgi.com/tech/stl/Vector.html

String documentation: http://www.sgi.com/tech/stl/basic_string.html

You access vector like as normal array because he have overloaded [] operator.
But if you may add intems to a vector you must do it by the insert function, like that:

v.insert(v.end(), new_item);.

This code add a new_item at ends of the vector v.

Vector declaration you doing with std::vector<std::string>.

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