Passing data between variables inside a class in C++ [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I don't fully understand nor did i find anything online about this topic but this code(cut down so it wont be 500 lines of code):
class Character{
public:
int MaxHP;
int currentHP;
currentHP = MaxHP;
int getHP()
{
return CurrentHP;
}
Character(int h){
maxHP=h
};
~Character(){};
};
int main()
{
Character warrior(300)
cout<<getHP();
return 0;
};
maxHP = 300
CurrentHP = -875000
now trying to acces the value of currentHP i get -87878....
My question is what is the problem?

If you wish to initialize a private variable it's better done in a constructor:
class Character{
private:
int MaxHP;
int currentHP;
public:
Character() : MaxHP(0), currentHP(MaxHP){}
}

Related

How to do I implement oop with a number of questions [closed]

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 2 years ago.
Improve this question
I have about 40 questions. (all the answers to the questions are of either int or float data type)
I will display the question and ask the user to input his/her answer.
Then I will check if the answer is correct or not.
I need to implement this program in object oriented programming.
I thought about doing it this way,questions by questions..but is there another way of doing this? Please help me..
The code below is sort of a pseudocode . I use c++ .
class workout{
private:
float variables...;
public:
workout();
workout(float variables...);
void answers();
int Display();
~workout();
};
...
void workout::answers(){
declare variables;
display question1;
input answer1
check if correct
display question2;
input answer2
check if correct
display question3;
..and so on..
}
workout::~workout(){
}
int main(){
...
return 0;
}
Well you could have a Question class, and then go from there. Here's an example.
class Question
{
public:
std::string question
float answer;
Question(std::string _question, float _answer)
: question(_question), answer(_answer) {}
void Display() {}
bool Check(float input) {}
};
And then maybe create an array of questions?

how can I bind a class member function with param as rvalue to boost::function? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I try to bind a class member function with param as rval to boost::function.
But it doesn't work.
my sample false code :
class Class1
{
int Foo1(int&& b)
{
return b;
}
void foo2()
{
boost::function<int(int&&)> fc(boost::bind(&Class1::Foo1, this, _1)
}
};
Use a lambda expression:
boost::function<int(int&&)> fc = [this](int&& x)
{
return Foo1(x);
};

Why can't I overload this constructor? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am coding a class to help with models.
There is a snippet of code, this one (from the .cpp file):
Model GameModels::getModel(std::string& gameModelName){
return GameModelList.at(gameModelName);
}
Which throws this error:
Models.h:44:9: error: ‘Models::Model
Models::GameModels::getModel(std::string&)’ cannot be overloaded
Model getModel(std::string& gameModelName);
^ Models.h:40:9: error: with ‘Models::Model Models::GameModels::getModel(std::string&)’ Model
getModel(std::string& gameModelName);
on g++. Here is my Model definition:
struct Triangle{
VertexFormat x;
VertexFormat y;
VertexFormat z;
};
struct Model{
unsigned int vao;
std::vector<unsigned int> vbos;
std::vector<Triangle> geometry;
std::string shaderFilepath;
Model(){}
};
According to the error message (which has nothing to do with the posted code), you declare the same function twice, at lines 40 and 44 of Model.h.

expected unqualified-id before 'public' [closed]

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.

Making a private function public in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am learning my first programming language C++ and I have issues with making a private function public. Can you guys help me to find the problem?
#include <iostream>
#include <string>
using namespace std;
class JadClass
{
public:
void setName(string x)
{
name = x;
}
string getName()
{
return name;
}
private:
string name;
};
int main()
{
JadClass jc;
jc.setName = "Jad Charara w\n";
cout << jc.getName();
system("pause");
return 0;
}
instead of
jc.setName = "Jad Charara w\n";
write
jc.setName("Jad Charara w\n");
First of all you have defined 2 functions in class JadClass with public access specifier,so please confirm access specifier of which function you want to change from private to public.
Second thing in main you are trying to call setName function.
jc.setName = "Jad Charara w\n";
The above function call should be in jc.setName("Jad Charara w\n"); format.