Hello please help me
I want to declare object of base class above function main or global variable( object)
and use this object in derived class like this:

class X
{
    ...
}

class y : public X
{
   void func();
}

void y::func()
{
  a[1][0] = &c;   //compiler error: a[1][0] undeclared identifier
}
////////////////////////

X* arr[8][8];
void main()
{
 ...
}

----------------------------
how can i do this?
because when i define like this compiler error that a[1][0] undeclared identifier

Recommended Answers

All 6 Replies

I'm guessing you actually have ... in your source instead of actual code. a and c are not anywhere to be found. Provide the full source code and we can help you. Also, use code tags and void main is now int main.

I'm guessing you actually have ... in your source instead of actual code. a and c are not anywhere to be found. Provide the full source code and we can help you. Also, use code tags and void main is now int main.

The problem is quite clear.And there is no need for the source code for solving this problem.Even though the source code may be needed in the near future for other problems which may rise. ;)

@thr:
A function in class y can work on variables declared in class y or the public variables in class x or the global variables(Usage of global variables is not recommended.).But I think you want to use the pointer to objects of class x which you have declared as X *arr[10][10] But you are using a[][] instead of arr[][] inside the function

void y::func()
{
a[1][0] = &c; //compiler error: a[1][0] undeclared identifier
}

>>//compiler error: a[1][0] undeclared identifier
Another reason for that error that no one has mentioned is that the class is attempting to use that array before it has been declared. If the class is in a header file then you will also have to put the array declaration there. The easiest way to do that is to make the array a static member of class Y. If you don't want to do that then you will have to do something like this:

class X
{
  // blabla
};
extern X* a[10][10];
class Y : public X
{
public:
    func()
   {
      a[1][0] = reinterpret_cast<X*>(this); 
   }
};

X* a[10][10];

int main()
{

}

@thr:
A function in class y can work on variables declared in class y or the public variables in class x or the global variables(Usage of global variables is not recommended.).But I think you want to use the pointer to objects of class x which you have declared as X *arr[10][10] But you are using a[][] instead of arr[][] inside the function

void y::func()
{
a[1][0] = &c; //compiler error: a[1][0] undeclared identifier
}

excuse me i have a problem in writting above code i use arr[][] in func():
void func()
{
arr[0][1] = &c; // compiler error to this line arr no declarade
}

excuse me i have a problem in writting above code i use arr[][] in func():
void func()
{
arr[0][1] = &c; // compiler error to this line arr no declarade
}

Ya thats what I mentioned.You use arr[][] in function and not a[][].For making it work write the code as follows:

//Definition of class X
X *arr[10][10];
//Definition of class Y with public X inheritence
//Function definition of Y::fun()

To declare the variable before it is being used or you can even do what Ancient Dragon has stated in post #5.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.