Author Topic: Cant return code in a static function (just read it, it's rly hard to explain)  (Read 3770 times)

0 Members and 1 Guest are viewing this topic.

Offline CiriousJoker

  • LV2 Member (Next: 40)
  • **
  • Posts: 34
  • Rating: +1/-0
    • View Profile
Sorry to bug you guys again, but i really cant figure out why this doesnt work :(

Here's the important code snipped im talking about:

Code: [Select]
class Level
{
public:
static bool InitLevel();

private:
static string LevelDataString;
};

bool Level::InitLevel()
{
if(( (Level::LevelDataString).size() % 6) != 0)
{
return false; // This line crashes it all
}
return true;
}


This crashes:
Code: [Select]
if(( (Level::LevelDataString).size() % 6) != 0)
{
char popup_result[256];
show_msgbox("Error", "The level seems to be broken.");
}
return true;


This also crashes:
Code: [Select]
if(( (Level::LevelDataString).size() % 6) != 0)
{
return false;
}
return true;


This works (but makes no sense, i just tested if ANY code makes it crash):
Code: [Select]
if(( (Level::LevelDataString).size() % 6) != 0)
{
int test = 1;
}
return true;

I seriously dont get why i cant just return false from a boolean function ...
I mean, does it crash in "normal" for pcs too or is this just an ndless problem?

Offline bb010g

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 428
  • Rating: +22/-1
  • I do stuff
    • View Profile
    • elsewhere on the net
Do you ever initialize LevelDataString? E.g.
Code: [Select]
std::string Level::LevelDataString = "PotatoLand";
Arch Linux user
Haskell newbie | Warming up to Lua | Being dragged into C++
Calculators: HP 50g, HP 35s, Casio Prizm, TI-Nspire CX CAS, HP 28s, HP Prime, Mathematica 9 (if that counts)
π: 3.14...; l: 108; i: 105; e: 101; l+i+e: 314
THE CAKE IS A LIE IS A PIE

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
AFAIK "static std::string LevelDataString" in the class definition isn't enough. It's only in a header file, but it doesn't have a place to stay, in your current code, it does not exist.
You have to put "std::string Level::LevelDataString" somewhere in a .cpp file. It could also be because you're using a too old version of the SDK, without support for C++ as it was first added by tangrs somewhere around r865.

Offline CiriousJoker

  • LV2 Member (Next: 40)
  • **
  • Posts: 34
  • Rating: +1/-0
    • View Profile
i've added this above the definition of the function:

Code: [Select]
string Level::LevelDataString = "002:000:001+000:001:001+001:002:003";

But it seems to be a problem with the if testing, because if i just write:

if(true)
{
    return false;
}

it works

Offline ajorians

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 105
  • Rating: +47/-0
    • View Profile
Hi Virusscript24,

Vogtinator is correct.  Having a static class member variable also needs something else.  Here is a small example:
Code: [Select]
#include <iostream>
class Something
{
public:
    static int s_nValue;
};
 
int Something::s_nValue = 1;//This part here is needed
 
int main()
{
    Something cFirst;
    cFirst.s_nValue = 2;
 
    Something cSecond;
    std::cout << cSecond.s_nValue;
 
    return 0;
}

See this tutorial here: http://www.learncpp.com/cpp-tutorial/811-static-member-variables/

Hope that helps!