943,701 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 553
  • C++ RSS
Jul 12th, 2009
0

help me in c++

Expand Post »
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
thr
Reputation Points: 11
Solved Threads: 0
Light Poster
thr is offline Offline
25 posts
since Jul 2009
Jul 12th, 2009
0

Re: help me in c++

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.
Reputation Points: 27
Solved Threads: 3
Light Poster
Grigor is offline Offline
26 posts
since Jan 2007
Jul 12th, 2009
0

Re: help me in c++

Click to Expand / Collapse  Quote originally posted by Grigor ...
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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 12th, 2009
0

Re: help me in c++

@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
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 12th, 2009
0

Re: help me in c++

>>//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:
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jul 12th, 2009
0

Re: help me in c++

Click to Expand / Collapse  Quote originally posted by csurfer ...
@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
c++ Syntax (Toggle Plain Text)
  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
}
thr
Reputation Points: 11
Solved Threads: 0
Light Poster
thr is offline Offline
25 posts
since Jul 2009
Jul 13th, 2009
0

Re: help me in c++

Click to Expand / Collapse  Quote originally posted by thr ...
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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ CLR - How to search hex in a binary file
Next Thread in C++ Forum Timeline: OOP in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC