小编Dav*_*ave的帖子

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

大家好,我需要在C++的类头文件中声明一个字符串数组.

它看起来像这样:

//Maze.h
#include <string>

class Maze

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

public:
    Maze ( );
    void render();
};
Run Code Online (Sandbox Code Playgroud)

并且构造函数看起来像这样:

//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;
}
Run Code Online (Sandbox Code Playgroud)

我收到一个编译错误,说:

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
Run Code Online (Sandbox Code Playgroud)

并且唯一让我意识到的是,由于某种原因,它认为我希望字符串作为变量名而不是类型声明.

如果有人可以帮助我,这将是太棒了,看了一会儿,它给了我屎哈哈.

干杯伙计们

c++ arrays string header declaration

3
推荐指数
1
解决办法
3161
查看次数

标签 统计

arrays ×1

c++ ×1

declaration ×1

header ×1

string ×1