modifiable lvalue

Hello I am trying to make 2 items inside of if statment for example
I want to make this with define
BEGIN
POKEMON{
NAME:"PIKACHU"
HP:100
}

POKEMON{
NAME:"CHARMANDER"
HP:100
}
END
Pokemon pikachu;
Pokemon charmander;
if(
pikachu= Pokemon{ false ? "" : "PIKACHU",false ? 0 : 100 } &&
charmander= Pokemon{ false ? "" : "CHARMANDER" false ? 0 : 100}
);
i have overload && in my class pokemon and wrote this
bool operator&&(const Pokemon &right) const
{
return true;
}
and i have an error in first ....Pokemon{ false ? "" : "PIKACHU" and it says expression must be a modifiable lvalue what i can do?
Aside from all the pointless false ? nonsense, you need to realise that = has lower precedence than &&.

So you have some weirdness like

pikachu= ( Pokemon{ false ? "" : "PIKACHU",false ? 0 : 100 } &&
charmander ) = Pokemon{ false ? "" : "CHARMANDER" false ? 0 : 100}


Put some ( ) in the right place to say what you mean.
In my experience, there is often no good reason to overload the && operator. There is also usually no reason to allow implicit conversion to bool, but that is slightly more acceptable (parts of the standard library do it).

What you're doing doesn't make sense (see salem c's post), and even if it did make sense, it seems to be quite a bad practice.

Construction of a Pokemon shouldn't be something that fails.
1
2
Pokemon pikachu("PIKACHU", 100); // name, HP
Pokemon charmander("CHARMANDER", 100);

Keep it simple.
Last edited on
hello i just want to make my pokemon programming language thats because i am doing this weird stuff also i put () and i have now something like this
if ((pikachu= new pokemon{ false ? "" : "PIKACHU", false ? 0 : 100 } )&&
(charmander= new pokemon{ false ? "" : "CHARMANDER", false ? 0 : 100 }
));

but now I am trying to create an array like this
POKEMONS [
POKEMON {
NAME : "PIKACHU" ,
HP:100
},
POKEMON {
NAME : "CHARMANDER " ,
HP : 85
}
]

and I have wrote something like this
Pokemon *somePokemon[2] = { NULL };
if (somePokemon[new Pokemon{ false ? "" : "PIKACHU", false ? 0 : 100 },
new Pokemon{ false ? "" : "CHARMANDER", false ? 0 : 100 }] );
but in the first new it says expression must have integral or unscoped enum type
what I can do inorder to fix this I tried put () but it doent seems to work and also i want to make it with defines so () here couldnt help


> but in the first new it says expression must have integral or unscoped enum type
Yah think!?

somePokemon[new Pokemon{ false ? "" : "PIKACHU", false ? 0 : 100 },
new Pokemon{ false ? "" : "CHARMANDER", false ? 0 : 100 }]

Maybe you should learn some C++ first.

You can't just put your random wibble between [ ] and expect it to be an array subscript.

ooh so i have to overload []?
Last edited on
No, you let go of that if nonsense and do
1
2
3
somePokemon[0] = new Pokemon{"PIKACHU", 100 };
somePokemon[1] = new Pokemon{"CHARMANDER", 85 };

yes but like i said i want to make my pokemon programm language with define and i want to write in a cpp file this:
POKEMONS [
POKEMON {
NAME : "PIKACHU" ,
HP:100
},
POKEMON {
NAME : "CHARMANDER " ,
HP : 85
}
]
and with defines overloads etc, i want the compiler make an array of 2 object and put all the fields
You don't need an EDSL for this. Don't try to make C++ do this for you. Don't try to make the C preprocessor do this for you.

Read it from a file. At runtime.
Last edited on
The syntax looks very similar to JSON, is that what you're trying to emulate?

I agree with mbozzi. Use a file.

There is a header-only JSON implementation for C++ by nlohmann.
https://github.com/nlohmann/json/
https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp

_______________________________________

pokemon.json:
{
	"pokemon": [
		{ "name" : "Pikachu",
		  "hp"   : 120 },
		{ "name" : "Charmander",
		  "hp"   : 90 }
	]
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <vector>

#include "json.hpp"

// for convenience
using json = nlohmann::json;

class Pokemon {
  public:
	std::string name;
	int hp;
};

int main()
{
	// read a JSON file
	std::ifstream fin("pokemon.json");
	json pokejson;
	fin >> pokejson;

	std::vector<Pokemon> pokemon;
	
	// read from JSON object into vector<Pokemon>:
	for (const auto& element : pokejson["pokemon"]) {
		std::string name = element["name"];
		int hp = element["hp"];
		pokemon.push_back(Pokemon{name, hp});
	}
	
	// print
	for (const auto& poke : pokemon)
	{
		std::cout << '\n';
		std::cout << "Name: " << poke.name << '\n';
		std::cout << "HP: " << poke.hp << '\n';
	}
}
Last edited on
Topic archived. No new replies allowed.