[C++] variable visibility problem

Recommended Posts

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).

Share on other sites
There is no DoSomething in B?

Share on other sites
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()?

Share on other sites
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.

Share on other sites
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?

Share on other sites
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).

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

thanks! :)

Create an account

Register a new account

• Forum Statistics

• Total Topics
628359
• Total Posts
2982259

• 10
• 9
• 13
• 24
• 11