[DEV]Problem with function

Discussion in '3DS - Homebrew Development and Emulators' started by Lilligant, Sep 24, 2016.

  1. Lilligant
    OP

    Lilligant GBAtemp Regular

    Member
    148
    77
    Aug 29, 2016
    Brazil
    Well i'm trying to practice some coding but i stumbled upon this error

    The code is this one

    secondT is a string variable inside main that is passed as a pointer to be modified inside that example function
     
  2. elhobbs

    elhobbs GBAtemp Advanced Fan

    Member
    762
    284
    Jul 28, 2008
    United States
    There are multiple problems. The scope on tChange and secondOption is a pointer. If you are using c++ you might as well use references.
     
  3. Logan Pockrus

    Logan Pockrus Knawledge is key.

    Member
    1,338
    1,020
    Jan 1, 2016
    United States
    Well, if what you're trying to do is modify the variable "secondT" points to, you'll need to dereference it (by adding an asterisk (*) in front of your pointer). Here's what your function should look like:
    Code:
    void example(std::string* secondT, int* secondOption)
    {
        std::string tChange;
        if(*secondOption == 0)
        {
            tChange="ON";
            *secondOption=1;
        }
        else
        {
            tChange="OFF";
            *secondOption=0;
        }
    
        *secondT=tChange;
    |
    Oh, and don't forget to call the function like this:
    Code:
    int main()
    {
        // ...
        example(&someString, &someInt);
        // ...
    }
    The whole point of dereferencing is to access/modify data being pointed to by a pointer; in your code's current state, you're trying to modify what/where the pointer (std::string secondT) is pointing to, rather than the value of what it's pointing to. I know it's hard to wrap your head around, so here's how you can rewrite the declaration of your function in order to keep you from having to modify the code in the function:
    Code:
    void example(std::string& secondT, int& secondOption)
    {
        // Code goes here (unmodified)
        // ...
    }
    If you don't already know, you'd call this function like so:
    Code:
    int main()
    {
        // ...
        example(someString, someInt);
        // ...
    }
    (The big takeaway here is that you don't use the '&' symbol in this instance.)
     
    Last edited by Logan Pockrus, Sep 24, 2016
    Lilligant likes this.
  4. Lilligant
    OP

    Lilligant GBAtemp Regular

    Member
    148
    77
    Aug 29, 2016
    Brazil
    I was thinking in class dynamic array pointers, because i did that in college couple weeks ago, and they go a little bit different,
    but that worked, thanks!
     
    Logan Pockrus likes this.
  5. Lilligant
    OP

    Lilligant GBAtemp Regular

    Member
    148
    77
    Aug 29, 2016
    Brazil
    So, i got another issue @xerpi
    i'm using sftdlib to render text on screen, i'm using this function

    sftd_draw_text_wrap(font, 43, 140, RGBA8(255, 255, 255, 255), 10, 100, CNumber);

    It starts rendering the string at X = 43 & Y = 140 and goes untill X = 143 where it wraps
    but it's not happening, it goes beyond the screen boundaries, and every 8th character (8th, 18th, 28th...) the text goes a bit to the left, when i put the 9th character the text goes back to place
     
    Last edited by Lilligant, Sep 25, 2016
  6. Logan Pockrus

    Logan Pockrus Knawledge is key.

    Member
    1,338
    1,020
    Jan 1, 2016
    United States
    I honestly don't know what's going wrong, as I don't use sftdlib...sorry!

    EDIT: I know you weren't asking me, but I was part of this thread, so I thought I'd make it clear I don't know the answer.
     
  7. Lilligant
    OP

    Lilligant GBAtemp Regular

    Member
    148
    77
    Aug 29, 2016
    Brazil
    It's all good, that's why i tagged Xerpi here hehe
     
    Logan Pockrus likes this.
  8. Lilligant
    OP

    Lilligant GBAtemp Regular

    Member
    148
    77
    Aug 29, 2016
    Brazil
    So, i got another issue here tehe :P

    i was trying to solve some issues with the 3DSSCalc homebew
    and i'm trying to do that using this code

    http://pastebin.com/3gQBn4a2

    It's a recursive function to solve basic numeric expressions (in string format)

    BUT it gives me errors when i try to compile with it

    the issue is inside the switch part, what is going on D:
     
    Last edited by Lilligant, Sep 25, 2016
  9. TheCruel

    TheCruel Developer

    Banned
    1,351
    2,882
    Dec 6, 2013
    United States
    Your function requires a string reference to be passed. You can fix this my either not making it a reference or perhaps doing something like:
    Code:
    calcBasicOperations(string(calcNumber.substr(0,i))+ calcBasicOperations(calcNumber.substr(i+1,lastNumber-i-1)));
    Or even better, declare a string variable in the beginning, define it in the switch statement, then use calcBasicOperations(yourStringVar) after the switch.