简体   繁体   English

指向c ++类的数组的基本指针

[英]Basic pointer to array of class for c++

AIBase* allai[2];
AIBase *z0AI = new AIA;
    AIBase *z1AI = new AIB;
allai[0] = z0AI;//this this gives me an error
allai[1]= z1AI;

AIBase is the superclass and AIA and AIB inherits from the AIBase what is wrong with the syntax ,i need some help in figuring this out error 1: AIBase是超类,AIA和AIB继承了AIBase语法的错误,我需要一些帮助来解决这个错误1:

error C4430: missing type specifier - int assumed. 错误C4430:缺少类型说明符 - 假定为int。 Note: C++ does not support default-int error C2466: cannot allocate an array of constant size 0 error C2040: 'allai' : 'int []' differs in levels of indirection from 'AIBase *[2]' 注意:C ++不支持default-int错误C2466:不能分配一个常量大小为0的数组错误C2040:'allai':'int []'的间接级别与'AIBase * [2]'不同

Why must this code be in function scope? 为什么这段代码必须在函数范围内? Cant this work in global scope? 不能在全球范围内开展这项工

In C++ (and C), executable code that is not a variable initialiser must appear inside a function. 在C ++(和C)中,不是变量初始化程序的可执行代码必须出现在函数内。 Executable code cannot appear at file scope (that is, outside any function). 可执行代码不能出现在文件范围内(即,在任何函数之外)。

So, just put your code inside a function: 所以,只需将代码放在函数中:

int main(int, char *[])
{
    AIBase* allai[2];
    AIBase *z0AI = new AIA;
    AIBase *z1AI = new AIB;
    allai[0] = z0AI;
    allai[1]= z1AI;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM