c++;指向对象数组的指针在gnu c++;但不是VisualStudio 我试图用C++中的一组自定义对象指针。以下代码在eclipse上编译并运行良好,eclipse使用cygwin的gnu编译器。但是代码在VisualStudio中给出了编译错误
错误c++;指向对象数组的指针在gnu c++;但不是VisualStudio 我试图用C++中的一组自定义对象指针。以下代码在eclipse上编译并运行良好,eclipse使用cygwin的gnu编译器。但是代码在VisualStudio中给出了编译错误,c++,visual-c++,pointers,g++,C++,Visual C++,Pointers,G++,错误 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();
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);
}
}
但在重新初始化方法中,这些行出现了错误
mStructres[size];
for (int i = 0; i < jStructeresArr.size(); i++) {
mStructres[i] = new Structure(obj1, obj2,
obj3);
}
mstrutres[size];
对于(int i=0;i
我做错了什么?这是跨平台开发的正确方法吗
更新
我不希望在这个阶段使用向量或std模板。
Structure*mStructres[]其中一个编译器可能将代码>解释为结构**mStructres
。(编辑:这可能不正确,请参阅注释中的更正)它只是指向结构的指针。请注意,实际上没有为它指定任何要指向的存储(除了单个指针),因此,当您为它指定任何内容时,您只是将其写入随机内存
mStructres[size]; // This does nothing, it _could_ cause a crash...
// but is otherwise the same as the next statement
42;
for (int i = 0; i < jStructeresArr.size(); i++)
mStructres[i] = new Structure(obj1, obj2, obj3);
如果jstructeresar.size()发生变化,那么您还有很多工作要做。所以,如果这是一种可能性,我强烈建议你放弃这个,只需与STD::vector或STD::清单。< /P>这是一个数组(没有已知大小)指针。是的,我想要一个动态数组。使用<代码> STD::向量< /代码>或者如果你想做C方式,<代码>结构** /代码>它不是合法C++。它与GCC一起工作,因为GCC开发人员已经决定允许它作为非标准扩展。它在Visual Studio中不起作用,因为VS不支持特定的非标准扩展。“Structure*mstrutres[];可能被解释为Structure**mstrutres”--不,我不认为是这样。我相信这是C99的一个特性,叫做“灵活数组”,即使编译为C++,GCC也允许使用。@ BejayLundLy:什么是恶意的黑客允许在语言中具体允许…我以前从未听说过这种胡说八道。我做了你指定的改变。编译错误消失了,但现在它在完全不同位置的其他位置“LNK2019未解析符号”抛出链接器错误。我认为这个错误是因为我所做的改变。有什么想法吗?谢谢。@user603125:考虑到这里涉及的更改,链接错误几乎肯定是无关的。需要更多的具体细节。(可能是一个单独的问题……但是链接错误通常非常局限于您的特定代码,因此它们经常会偏离主题空间)将此标记为解决编译错误的正确方法。谢谢:)
mStructres[size]; // This does nothing, it _could_ cause a crash...
// but is otherwise the same as the next statement
42;
for (int i = 0; i < jStructeresArr.size(); i++)
mStructres[i] = new Structure(obj1, obj2, obj3);
void Levels::reInitialize() {
for (int i=0; i< jStructeresArr.size(); i++) {
delete mStructres[i]; // Don't leak memory
mStructres[i] = new Structure(obj1, obj2, obj3);
}
}
mStructres = new Structure*[jStructeresArr.size()];