Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic pointer to array of class for c++

Tags:

c++

class

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?

like image 424
user1203499 Avatar asked Mar 06 '26 07:03

user1203499


1 Answers

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;
}
like image 86
Greg Hewgill Avatar answered Mar 07 '26 19:03

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!