【发布时间】:2010-09-28 03:28:44
【问题描述】:
在我的 Dev C++ 中,我正在尝试创建一个 2D Array 类,它的作用类似于 Grid。 但问题之一是我不确定构造函数的作用。
当我尝试编译时,我收到以下错误: 在构造函数 'Grid::Grid(int,int)' 中: 'squares' 不是一种类型 'yPos' 不能出现在常量表达式中 [构建错误] [grid.o] 错误 1
这是头文件:
#ifndef GRID_H
#define GRID_H
using namespace std;
class Grid
{
public:
Grid(int xPos, int yPos);
// Constructor
// POST: Creates the squares of grid; (x,y) coordinates
private:
int squares;
//2D Array
//the squares; (x,y) coordinates of the grids
};
#endif
这里是grid.h函数的.cpp文件
#include <iostream>
#include "grid.h"
using namespace std;
Grid::Grid(int xPos, int yPos)
{
squares = new squares[xPos][yPos];
//Trying to make squares into a 2D array, and turn the values into the arguments
//into the the x,y coordinates
}
.cpp 文件中的构造函数不起作用,我不确定该怎么做。有人有解决办法吗?
【问题讨论】:
标签: c++ class object multidimensional-array