在类头文件中声明字符串数组 - 编译器认为字符串是变量名?
大家好,我需要一些帮助来在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要限定名称:
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.使用 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 vectorv
.Vector declaration you doing with
std::vector<std::string>
.