简体   繁体   中英

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:

error C4430: missing type specifier - int assumed. 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]'

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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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