B4R Question [SOLVED] A real dummy question when using inline C

Johan Schoeman

Expert
Licensed User
Longtime User
I am stuck here....I am trying to call "multiply" from "add" and passing a value of 5 to "multiply" from "add". But I get this error:
B4X:
b4r_main.cpp:13: error: 'multiply' was not declared in this scope

Please help this dummy (me) to understand what it is that I am missing....:mad:


B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private Result, N1, N2 As Int 'ignore
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    N1 = 10
    N2 = 20
    RunNative("add", Null)
    Log("Result: ", Result)
End Sub

#if C
void add (B4R::Object* o) {
   //lower case variables
   b4r_main::_result = b4r_main::_n1 + b4r_main::_n2;
   multiply(5);                //THE ERROR IS HERE! b4r_main.cpp:13: error: 'multiply' was not declared in this scope
}

void multiply (int d) {
   //lower case variables
   b4r_main::_result = b4r_main::_n1 * d;
}


#End if
 

Johan Schoeman

Expert
Licensed User
Longtime User
This seems to work:
B4X:
#if C


void multiply(int d);

void add (B4R::Object* o) {
   //lower case variables
   b4r_main::_result = b4r_main::_n1 + b4r_main::_n2;
   multiply(5);                //THE ERROR IS HERE! b4r_main.cpp:13: error: 'multiply' was not declared in this scope
}

void multiply (int d) {
   //lower case variables
   b4r_main::_result = b4r_main::_n1 * d;
}


#End if

Is this the correct way of doing it?
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
This seems to work:
B4X:
#if C


void multiply(int d);

void add (B4R::Object* o) {
   //lower case variables
   b4r_main::_result = b4r_main::_n1 + b4r_main::_n2;
   multiply(5);                //THE ERROR IS HERE! b4r_main.cpp:13: error: 'multiply' was not declared in this scope
}

void multiply (int d) {
   //lower case variables
   b4r_main::_result = b4r_main::_n1 * d;
}


#End if

Is this the correct way of doing it?

you got it, it's called forward declaration.

https://en.wikipedia.org/wiki/Forward_declaration
 
Upvote 0

Laurent95

Active Member
Licensed User
Longtime User
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
Hello,
Another way is to put the sub 'Multiply' before the 'Add'.
Doing the compiler to stop to scream too :rolleyes:

...and your anti scream recipe for this sceneario...
B4X:
#if C
    
void foo_1 (void)  {  foo_2(); }
void foo_2 (void)  {  foo_1(); }

#End if
 
Upvote 0

Laurent95

Active Member
Licensed User
Longtime User
...and your anti scream recipe for this sceneario...
B4X:
#if C
   
void foo_1 (void)  {  foo_2(); }
void foo_2 (void)  {  foo_1(); }

#End if
I do a nap and let the compiler screaming :p
Because with recursive calls, i'm not sure that even the "forward declaration" change something.

Tell me, your dog run after his tail ? :D
 
Upvote 0
Top