expected unqualified-id before 'public' [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've searched everywhere and cant find an answer for this error
"expected unqualified-id before 'public'"
this is my code:
#include <iostream>
using namespace std;
static int playerHP;
static int playerPWR;
public:
{
static int playerHP = 100;
static int playerPWR = 4;
}
int main(){
}
p.s. This is in my main.

I don't know why you wrote this ridiculous code, but I assume you may want this:
class Player
{
public:
static int playerHP;
static int playerPWR;
};
int Player::playerHP = 100;
int Player::playerPWR = 4;

It looks like you're trying to build a class or struct. This public statement would work inside either of those, but not alone.
Try reading this class introduction and see if that makes it clearer.
If you don't want a class, because you're not trying to build a group of similar objects, then you can place these variables inside your main() - in that case, you might want to read about variable scope.

Related

static object inside template class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have been asked several times about the uses of static object and I guess I understand the crux behind using them, but one particular case confusing me, I know that in the code they are trying to make a factorial like mechanism using recursive call but did not able to understand different stages the code will go through.
#include<iostream>
using namespace std;
template<short N> class C
{
private:
static C<N-1>c;
public:
static int const n = N*c.n;
};
template<> class C<0>
{
public:
static int const n =1;
};
int main()
{
cout<<C<5>::n<<endl;
return 0;
}
See the live demo here please.
This code is generating an output 120.
What exactly is happening here?
It's unfortunate that the line
static C<N-1>c;
exists at all. It obfuscates the logic a little bit.
The functionality could have been as easily implemented as:
template<short N> class C
{
public:
static int const n = N*C<N-1>::n;
};
template<> class C<0>
{
public:
static int const n = 1;
};
which is a little bit easier to follow.
The net effect is same:
C<5>::n = 5*C<4>::n
C<4>::n = 4*C<3>::n
C<3>::n = 3*C<2>::n
C<2>::n = 2*C<1>::n
C<1>::n = 1*C<0>::n
C<0>::n = 1
Now you can follow how the program produces the output.

confused about class structure in code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am reading a sample code that uses C++ and classes, I am new on C++ classes I can work with basics similar to this http://www.cplusplus.com/doc/tutorial/classes/, but I cant understand what the code below does mean or the color it is using visual studio c++
thanks
I am sorry if it is a fool question
It creates an object named some by instantiating the class some.
Then it calls the member function ToVector() on the object some and pass the result of the call to the function named function.
class is blue because it is a keyword of the C++ language.
The first some is green because it is the name of a class.
The second some is black because it is a variable.
And function and ToVector are red because the are functions.
Now this is ugly code because you "hide" the class some by reusing the same name for your variable. Also you do not need to put the word class here.
Here is a more complete and nicer version:
#include <vector>
class Some
{
public:
std::vector<int> ToVector()
{
return std::vector<int>(); //return an empty vector
}
};
int f(std::vector<int> v)
{
return 0;
}
int main(int, char**)
{
Some some; // Was "class some some"
return f(some.ToVector());
}

PAWN to C++ global variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do i write it in c++ ??
// in PAWN ( Procedural Language )
enum e_player_data
{
id, // Integer
username[24] // string
};
new PlayerData[50][e_player_data];
/*
so i can access it like PlayerData[1][id] = 1; or PlayerData[1][username] = "Firstname_Lastname";
*/
Can someone code it to c++ for me?
You can use a std::map:
typedef std::map<int,std::string> Player;
Player p;
// add a player:
int id = 0;
p[id] = "Max Power";
As tobi303 wrote in the comment, you can use a std::map to accomplish this.
struct e_player_data
{
int id,
std::string username
};
std::map<int,e_player_data> PlayerData;
PlayerData[1].id = 1;
PlayerData[2].username = "Firstname_Lastname";

How to choose among two classes given a condition in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Suppose there are two classes Class1 and Class2. given a condition I have to choose among them in shortest way possible without using if-else.
Means least lines of code.
At compile time only!!!
class class1{};
class class2{};
auto data = (((condition) ? class1 : class2) *)(variable)
Assuming you need to create object at compile time depending on a variable, you can try something like following
class class1{};
class class2{};
int main( int argc, char *argv[] )
{
constexpr bool variable =true;
/* x is object of type class1 or class2 depending on
compile time constant 'variable'
*/
typedef std::conditional<variable, class1, class2>::type x;
//std::cout << typeid(x).name() << '\n';
return 0;
}
See Here

How to use variable type Struct inside class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to build a videogame with SDL for my end year proyect. However I'm somewhat lost about how and when to use classes.
T tried to include a variable type struct inside a class but I can't manage to do it, maybe Position should be a class instead of a struct? Here's my code:
struct Position{
int x,y;
};
class Object{
private:
Position pos;
Position speed;
int tipe;
public:
Objeto(int,int);
Objeto();
~Objeto(); // DESTROY
};
When I try to do this I get error: 'class Object' has no member named 'x' How can I include the struct in the object?
From your error, I think you are trying to use
Objeto.x
instead of
Objeto.pos.x