Call function from another class.
Dec 12, 2011 at 11:10pm
Hello forum! A short question regarding functions.
My example is the following:
1 2 3 4 5 6 7 8 9 10 11 12
|
// somewhere in class A
//in the .h file : public: int a;
//In the .cpp
int A::doSomething()
{
a++
return a;
}
|
1 2 3 4 5 6 7 8 9 10 11
|
// Constructor of class B
B::B()
{
random_private_value = A::doSomething();
}
|
Class A is connected with class B with B * lnkB;
The error that I have is:
B.cpp(13) : error C2653: 'A' : is not a class or namespace name
B.cpp(13) : error C3861: 'doSomething': identifier not found
Ok obviously I do something wrong in the connection between the classes and the call of the function from class A...
Is that possible? If yes what wrong I have in my approach?
Last edited on Dec 12, 2011 at 11:12pm
Dec 12, 2011 at 11:34pm
This function call
A::doSomething()
means you're calling a static function doSomething() of class A.
Either a) you haven't declared A::doSomething() to be static in the definition of class A, or b) you didn't mean to use static function(s).
If b), then you'll need to have an instance of class A and then use that, for example:
1 2
|
A myInstanceOfA;
myInstanceOfA.doSomething();
|
Edit: Actually, your error message looks like you need to #include class A's header from B's.
Cheers,
Jim
Last edited on Dec 12, 2011 at 11:35pm
Topic archived. No new replies allowed.