Basic question about function scope

HI~

I am a cpp beginner and have a question about class function scope~
My program (modified) is as below:

test.h:

1
2
3
4
5
6
7
8

class test
{
public:
int A();

};


test.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Test.h"

int main ()
{
  return 0;
}



int test::A()
{
  bool x=B();
  return 0;
}

bool B()
{
  return -1;
}



The compiler shows me that 'B' was not declared in this scope.
First, I guessed the error is because that the member function of a class can only use other member functions or data member within the same class.

But it turns out that if I move the bool B() in front of int test::A(). There will be no error at all. Can anyone explain the reason further more for me

Thank you~
Last edited on
Don't post code which is different from your own but does not reproduce the problem ( or has other problems )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class test
{
public:
   int A();
}; //semicolon

bool B() //parenthesis
{

}

int test::A()
{
bool x=B();
}
The reason might be that B() is not declared, while B is. Does the compiler not complain about opening curly just after bool B? Also, A must return value and test class closing curly must be followed by ;

error C2628: 'test' followed by 'bool' is illegal (did you forget a ';'?)
error C2470: 'B' : looks like a function definition, but there is no parameter list; skipping apparent body
error C3861: 'B': identifier not found
Last edited on
¡já!, the code was real. I apology.
Topic archived. No new replies allowed.