[C++] variable visibility problem

Started by
5 comments, last by _nomad_ 18 years, 5 months ago
alright let's say i have the following code:
class A {
  private:
    B* test;

  public:
    void testPass (B* aParam) {
      test = aParam;
    }

    void testFunc() {
      test->DoSomething();
    }
}


class B {
  public:
    int testInt;

  void DoSomething() {}
}


main() {
  B* myB = new B();
  A* myA = new A();

  myA->testPass(B);
}
how come i get error about illegal access to test, when i use testFunc() in class A? i've run debug and the watcher says: "CXX0030: Error: expression cannot be evaluated" (i'm using vc++.net) what am i doing wrong? (btw, the code above was rewritten from notepad, it's derived from another project code and rewritten for simplicity/discussion/example).
Advertisement
There is no DoSomething in B?
oops, sorry i forgot to put dosomething() in B in code above.

the problem is still the same, why can't i access test in testFunc()?
Besides the missing DoSomething(), semicolins, using B before it's been defined... nothing obviously wrong to me.

Quote:how come i get error about illegal access to test, when i use testFunc() in class A? i've run debug and the watcher says it is: "CXX0017: Error: symbol "" not found"


So you're saying you're not getting compiler errors, just debug errors? If you're using VS6, it's likely just one of it's many bugs, a clear sign you should upgrade :-).

If you're getting compiler/linker errors, please copy and paste the actual code and errors (including their line numbers) which are giving you this problem. Otherwise, it's like like a lost tourist in Paris trying to find the airport with a New York subway map.

Either way, without the actual source, our ability to help deal with any problems is fairly low.
the code i placed here was remade and isn't directly from the original source code (since it's too huge to past here). although i am doing the same thing (hence the missing semicolons, etc.)

so nothing's wrong with it right?
Quote:Original post by _nomad_
alright let's say i have the following code:

class A {  private:    B* test;  public:    void testPass (B* aParam) {      test = aParam;    }    void testFunc() {      test->DoSomething();    }}class B {  public:    int testInt;  void DoSomething() {}}main() {  B* myB = new B();  A* myA = new A();  myA->testPass(myB); // <--- i think you went wrong here. compare to your original code}



how come i get error about illegal access to test, when i use testFunc() in class A? i've run debug and the watcher says: "CXX0030: Error: expression cannot be evaluated" (i'm using vc++.net)

what am i doing wrong?

(btw, the code above was rewritten from notepad, it's derived from another project code and rewritten for simplicity/discussion/example).


Beginner in Game Development?  Read here. And read here.

 

i've solved it...the problem was in other parts of the code, but is somewhat related to what Alpha_ProgDes mentioned too.

thanks! :)

This topic is closed to new replies.

Advertisement