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

Similar threads

B4R Tutorial Inline C / C++
B4R Tutorial HC 05 Classic Bluetooth
B4R Code Snippet GFX and Bitmaps
B4R Tutorial Big/Large Alphanumeric Characters(LCD1602)
B4R Tutorial SevSeg with inline C
B4R Code Snippet 7 segment display (with dots)

Johan Schoeman

Expert
Licensed 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
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?
 

thetahsk

Active Member
Licensed 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
 

Laurent95

Active Member
Licensed User

thetahsk

Active Member
Licensed 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
 

Laurent95

Active Member
Licensed 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
 
Top