Homebrew [DEV]Problem with function

  • Thread starter Thread starter MistWisp
  • Start date Start date
  • Views Views 1,291
  • Replies Replies 8

MistWisp

Well-Known Member
Member
Joined
Aug 29, 2016
Messages
165
Reaction score
83
Trophies
0
Age
33
XP
238
Country
Brazil
Well i'm trying to practice some coding but i stumbled upon this error

error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' in assignment
secondT = TChange;

The code is this one

void example(std::string* secondT, int* secondOption){
if(secondOption == 0){
std::string tChange = "ON";
secondOption = 1;
}
else{
std::string tChange = "OFF";
secondOption = 0;
}
secondT = tChange;
}

secondT is a string variable inside main that is passed as a pointer to be modified inside that example function
 
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
There are multiple problems. The scope on tChange and secondOption is a pointer. If you are using c++ you might as well use references.
 
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,
  • Like
Reactions: MistWisp
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.)

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!
 
  • Like
Reactions: Logan Pockrus
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 MistWisp,
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
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.
 
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

c:/devkitPro/msys/home/Lilligant/3DSSCalc/source/Calc.cpp:97:83: error: invalid initialization of non-const reference of type 'std::__cxx11::string& {aka std::__cxx11::basic_string<char>&}' from an rvalue of type 'std::__cxx11::basic_string<char>'
calcBasicOperations(expr.substr(0,i)) + calcBasicOperations(expr.substr(i+1,lastNumber-i-1));
^
c:/devkitPro/msys/home/Lilligant/3DSSCalc/source/Calc.cpp:90:6: note: initializing argument 1 of 'void calcBasicOperations(std::__cxx11::string&)'
void calcBasicOperations(string& calcNumber){

the issue is inside the switch part, what is going on D:
 
Last edited by MistWisp,
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:
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.
 

Site & Scene News

Popular threads in this forum