help me in c++

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 13
Reputation: thr is an unknown quantity at this point 
Solved Threads: 0
thr thr is offline Offline
Newbie Poster

help me in c++

 
0
  #1
Jul 12th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 25
Reputation: Grigor is an unknown quantity at this point 
Solved Threads: 3
Grigor's Avatar
Grigor Grigor is offline Offline
Light Poster

Re: help me in c++

 
0
  #2
Jul 12th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: help me in c++

 
0
  #3
Jul 12th, 2009
Originally Posted by Grigor View Post
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.
Last edited by csurfer; Jul 12th, 2009 at 9:17 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: help me in c++

 
0
  #4
Jul 12th, 2009
@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
  1. void y::func()
  2. {
  3. a[1][0] = &c; //compiler error: a[1][0] undeclared identifier
  4. }
Last edited by csurfer; Jul 12th, 2009 at 9:18 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help me in c++

 
0
  #5
Jul 12th, 2009
>>//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:
  1.  
  2. class X
  3. {
  4. // blabla
  5. };
  6. extern X* a[10][10];
  7. class Y : public X
  8. {
  9. public:
  10. func()
  11. {
  12. a[1][0] = reinterpret_cast<X*>(this);
  13. }
  14. };
  15.  
  16. X* a[10][10];
  17.  
  18. int main()
  19. {
  20.  
  21. }
Last edited by Ancient Dragon; Jul 12th, 2009 at 10:11 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 13
Reputation: thr is an unknown quantity at this point 
Solved Threads: 0
thr thr is offline Offline
Newbie Poster

Re: help me in c++

 
0
  #6
Jul 12th, 2009
Originally Posted by csurfer View Post
@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
  1. void y::func()
  2. {
  3. a[1][0] = &c; //compiler error: a[1][0] undeclared identifier
  4. }
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
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: help me in c++

 
0
  #7
Jul 13th, 2009
Originally Posted by thr View Post
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:
  1. //Definition of class X
  2. X *arr[10][10];
  3. //Definition of class Y with public X inheritence
  4. //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.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 395 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC