【发布时间】:2013-04-01 05:40:50
【问题描述】:
我正在尝试使用指向 C++ 中自定义对象数组的指针。以下代码在使用 cygwin 的 gnu 编译器的 eclipse 上编译并运行良好。但是代码在 Visual Studio 中给出了编译错误。
错误
class 'Levels' has an illegal zero-sized array
上线
Structure *mStructres[];
完整代码
/*
* Levels.h
*/
#include "objects/Structure.h"
#ifndef LEVELS_H_
#define LEVELS_H_
class Levels{
public:
//other public members
void reInitialize();
Levels();
~Levels();
private:
//other private members
Structure *mStructres[];
};
#endif /* LEVELS_H_ */
/////////////////////////////////////
/*
* Levels.cpp
*/
#include "Levels.h"
Levels::Levels() {
}
Levels::~Levels() {
}
void Levels::reInitialize() {
mStructres[size];
for (int i = 0; i < jStructeresArr.size(); i++) {
mStructres[i] = new Structure(obj1, obj2,
obj3);
}
}
我尝试将行改为
Structure *mStructres;
但后来我在重新初始化方法中的这些行出现错误
mStructres[size];
for (int i = 0; i < jStructeresArr.size(); i++) {
mStructres[i] = new Structure(obj1, obj2,
obj3);
}
我做错了什么?这是跨平台开发的正确方法吗?
更新 在这个阶段我宁愿不使用向量或标准模板。
【问题讨论】:
-
这是一个指针数组(大小未知)。
-
是的,我想要一个动态数组。
-
std::vector<Structure*>或者如果你想做 C 方式,Structure** -
这不是合法的 C++。它适用于 GCC,因为 GCC 开发人员已决定允许它作为非标准扩展。它在 Visual Studio 中不起作用,因为 VS 不支持该特定的非标准扩展。
标签: c++ visual-c++ pointers g++