3
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?

3
  • 7
    Please don't make us guess. Show the error message (it's important). Commented Feb 11, 2012 at 7:58
  • 1
    There's nothing wrong with this snippet. ideone.com/UEC0k Commented Feb 11, 2012 at 8:01
  • Do you write it in function scope? Commented Feb 11, 2012 at 8:12

1 Answer 1

4

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is correct. However, to be pedantic, it's only the last two lines that need to be inside a function; the first three can appear at file scope. (Not that it's recommended...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.