int BattleSituation::getHPData(int player, int slot)
{
    PokeBattle &p = this->poke(player,slot);
    return p.lifePoints();
}

int callGetDataHP(const BattleSituation&situation, int id, int pokeslot)
{
    return situation.getHPData(id, pokeslot);
}

QScriptValue ScriptEngine::getBattleData(int id, int pokeslot)
{
    if(!loggedIn(id) || pokeslot < 1 || pokeslot > 6)
    {
        return myengine.undefinedValue();
    }else{
        return callGetDataHP(getHPData, id, pokeslot);
    }
}

I'm getting passing 'const BattleSituation' as 'this' argument of 'int BattleSituation::getHPData(int, int)' discards qualifiers.

Dani AI

Generated

Short diagnosis: the cascade of errors comes from const-correctness and from passing a type name where an object is required. Making the member that reads HP const was the right first step (as pointed out). Once that member is const, every member it calls on this must also be callable on a const object; that is why binding a non-const reference to the result of poke(...) failed.

Practical checklist to resolve the chain:

  • Keep the HP accessor const.
  • Provide a const overload of the poke accessor so it can be called from const methods; for example:
PokeBattle& poke(int player, int slot);                // non-const use
const PokeBattle& poke(int player, int slot) const;    // const overload
  • Inside const accessors, bind to a const reference (so the return from the const overload can bind cleanly).
  • At the call site, pass an actual BattleSituation object or reference — not the type name. For example:
myserver->callGetDataHP(battleInstance, id, slot);  // pass an object or *pointer

The error "expected primary-expression before ',' token" in the thread was caused by attempting to use the class name where an expression (a variable or dereferenced pointer) belongs. If the intent was to pass the member function itself as a callback, pointer-to-member syntax is required (and has a different type). As noted, const qualifiers control which overloads are chosen; avoid const_cast to force things — prefer providing proper const overloads or changing the function signatures so the const-correct design is consistent.

Quick troubleshooting tips: compile after each change and read the line number reported, make sure declarations are visible in headers before use, and keep both const and non-const overloads where both read-only and modifying access are needed.

Recommended Answers

All 25 Replies

Well, yeah.

Line 7 defines situation as a reference to an object of type BattleSituation that you have promised not to change (because you defined it as a reference to const).

Line 9 calls the getHPData member of situation. That member is permitted to change the value of its object because you did not promise not to do so. To make that promise, you would have to make line 1 look like this:

int BattleSituation::getHPData(int player, int slot) const

and change the definition of class BattleSituation analogously.

Thank You ^-^

Now that i've fixed that got other error:

invalid initialization of reference of ttype 'Pokebattle&' from expression of type 'const PokeBattle'

Without seeing the relevant code in its current form, it's hard to guess what the problem might be.

QScriptValue ScriptEngine::getBattleData(int id, int pokeslot)
{
    if(!loggedIn(id) || pokeslot < 1 || pokeslot > 6)
    {
        return myengine.undefinedValue();
    }else{
        return callGetDataHP(getHPData, id, pokeslot);
    }
}

int BattleSituation::getHPData(int player, int slot) const
{
    PokeBattle &p = this->poke(player,slot);
    return p.lifePoints();
}

int callGetDataHP(const BattleSituation & situation, int id, int pokeslot)
{
    return situation.getHPData(id, pokeslot);
}

I don't know what line the compiler doesn't like, but I'm guessing it's line 13. I'm also guessing that poke returns a reference to const (i.e. a reference to an object that you have promised not to change), but you're trying to bind a plain reference to it in the definition of p.

If my guess is correct, you need to change line 13 to say:

const PokeBattle &p = this->poke(player,slot);

If not, then I would need more context in order to advise you.

Wonder why, everytime i get how to do one thing, OTher error comes ._.

return callGetDataHP(getHPData, id, pokeslot);

'getHPData' was not declared in this scope
'callGetDataHP' was not declared in this scope

Wonder if the compiler hates me or what.

You can't call non const functions on const objects. You can declare const function by adding keyword const at the end of declaration, for example bool cmp(const Obj& snd) const.
//nevermind, didn't see later code

I can't fix the problem. :s I don't get why it's happening.

I can't fix the problem. :s I don't get why it's happening.

Until you post the actual code that's causing the problem, I doubt anyone will try to help you.

int BattleSituation::getHPData(int player, int slot) const
{
    const PokeBattle &p = this->poke(player,slot);
    return p.lifePoints();
}

int callGetDataHP(const BattleSituation & situation, int id, int pokeslot)
{
    return situation.getHPData(id, pokeslot);
}

QScriptValue ScriptEngine::getBattleData(int id, int pokeslot)
{
    if(!loggedIn(id) || pokeslot < 1 || pokeslot > 6)
    {
        return myengine.undefinedValue();
    }else{
        return callGetDataHP(getHPData, id, pokeslot);
    }
}

'getHPData' was not declared in this scope
'callGetDataHP' was not declared in this scope

In line 18, what does getHPData represent?

Oh that, ignore it it was my mistake.

You said that you're getting the error message

'getHPData' was not declared in this scope

and you don't understand why. Now that I've told you why, is your problem solved? If not, please post the code that's causing trouble now, along with the error message and the line number that the error message specifies, so that we don't have to hunt for it.

I posted it. Line 18.

I posted it. Line 18.

And I explained what your problem was, and you said "Ignore that, my mistake."

So is your problem solved now or not? If not, please answer my question about line 18 of your code:

return callGetDataHP(getHPData, id, pokeslot);

In this line, you are using a variable named getHPData. I do not see a declaration for that variable. I see from line 9 that class BattleSituation has a member named getHPData, but that doesn't matter here, because you're not using getHPData as a member of a class.

So as far as I can tell, the compiler is complaining that you used getHPData without declaring it. If you don't understand why that is happening, please show us where getHPData is declared.

return callGetDataHP(getHPData, id, pokeslot);

There. I'll explain better.

I don't know what to put in replace of getHPData. How we can see in line 7 i have this:

int callGetDataHP(const BattleSituation & situation, int id, int pokeslot)

But i don't get what exatly to put in replace of getHPData.

I don't know what to put in replace of getHPData.

It's your program. If you don't know what you want it to do, how can you expect anyone else to know?

I want this:

QScriptValue ScriptEngine::getBattleData(int id, int pokeslot)
{
    if(!loggedIn(id) || pokeslot < 1 || pokeslot > 6)
    {
        return myengine.undefinedValue();
    }else{
        return callGetDataHP(BattleSituation&getHPData, id, pokeslot);
    }
}

to call this in return:

int callGetDataHP(const BattleSituation & situation, int id, int pokeslot)
{
    return situation.getHPData(id, pokeslot);
}

I have no idea what this statement means:

return callGetDataHP(BattleSituation&getHPData, id, pokeslot);

I've figured it out. Thank you ^-^

But... (Dun) another error. This is easy. :-.

bool Server::callGetDataHP(const BattleSituation & situation, int id, int pokeslot)
{
    return situation.getHPData(id, pokeslot);
}

QScriptValue ScriptEngine::getBattleData(int id, int pokeslot)
{
    if(!loggedIn(id) || pokeslot < 1 || pokeslot > 6)
    {
        return myengine.undefinedValue();
    }else{
        return myserver->callGetDataHP(/*Dont know wat goes here :3*/BattleSituation,id, pokeslot);
    }
}

int BattleSituation::getHPData(int player, int slot) const
{
    const PokeBattle &p = this->poke(player,slot);
    return p.lifePoints();
}

expected primary-expression before ',' token. :>

Btw, with my low knoweldge of C++ i get to fix some errors with your help. :)

In line 13, you are passing the name of a class as an argument. You can't do that.

In the future, please do not make me look through your whole program for the line that generates the error message.

Could you show me a small example of how to do it? So i'll save it and record what to do. ^-^ (I'm better with them :(

No, I cannot show you an example. It's your program -- you need to figure out what you want the program to do. Sorry.

Not an example with my program. I meant a random example.

A random example of what?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.