so i've got this problem.
i want to get some variables from a diferant class then main and i've lerned that it's good to
hide your data so it wont get so easely changed and use a getXXX function to acces it.
I tried to use the private: and public: thing but when i do i get an error saying
error: expected unqualified-id befor 'private'
i got class nr1# called dialog and the class with the variables is called race (not as in black and white)
anyway i call the function like this:(class dialog)
above this is all the #include stuff
dialog::dialog(int y)
{
race raceO;
switch(y)
{
case 1: cout << "choose a class \n1 ELF = " << raceO.getStats(1.1) << endl;
break:
}
and this is the race class
//were am i supposed to put
private: and public:
include "race.h"
include <iostream>
include <string>
using namespace std;
race::race(){
}
int race::raceElf(){
return 0;
}
int attack = 5;
int defence = 3;
int stamina = 6;
int race::getStats(int x){
if(x == 11){
return attack;
}
return 0;
}
in the Class declaration it should look like
class myClass{
public:
myClass();
private:
double x,y,z;
}
that is how you should use public and private, but otherwise I can't see what is wrong, plase provide the header file or the class declaration.
It depends how you want to use your class. I would keep all your data members private. Methods raceElf() and getStats() should be public as you will call them on your object. If a method were used only within other class methods but not outside it should be private. Constructor must be public if you want to create any objects of your class.
class race{
public:
race();
int raceElf();
int getStats(int);
private:
int attack;
int defence;
int stamina;
}
race::race(){
attack = 5;
defence = 3;
stamina = 6;
}
int race::raceElf(){ return 0; }
int race::getStats(int x){
if(x == 11){
return attack;
}
return 0;
}
Related
I am just learning about classes in C++, and I am trying to create this Coin class that simulates a coin flip with a method called toss() that will return either 0 or 1 which represent heads or tails respectively.
#include <iostream>
#include <cstdlib>
using namespace std;
class Coin {
private:
public:
Coin();
int toss();
};
Coin::Coin();
int Coin::toss() {
int num;
num = rand() % 2;
return num;
}
int main() {
Coin C;
cout << C.toss() << endl;
}
I keep getting an error that says: error: declaration of 'Coin::Coin()' outside of class is not definition. I am assuming that I did not declare my constructor correctly, but I am not sure.
The problem is that you're trying to declare the constructor outside the class when you wrote Coin::Coin();.
To solve this replace Coin::Coin(); with Coin::Coin(){} as shown below:
class Coin {
private:
public:
Coin();
int toss();
};
//----------v--->semicolon removed from here
Coin::Coin() //this is a definition now
{
}
I am very new to pointers, so I have no idea what is going on with them.
I am trying to get a master class pass a pointer of itself to its worker/s, and I have no idea why it doesn't work.
#include <iostream>
#include <Windows.h>
using namespace std;
class BigOne {
public:
LittleOne workers[1] = {LittleOne(this)};
int increase = 0;
};
class LittleOne {
BigOne* master;
public:
LittleOne(BigOne*);
void Increment();
};
LittleOne::LittleOne(BigOne* upOne) {
master = upOne;
}
void LittleOne::Increment() {
master->increase++;
}
BigOne Outer;
int main() {
cout << Outer.increase << endl;
Outer.worker.Increment();
cout << Outer.increase << endl;
system("PAUSE");
}
This is my problem boiled down to its core components.
The problem isn't with pointers. It's mostly here:
class BigOne {
public:
LittleOne workers[1] = {LittleOne(this)};
int increase = 0;
};
When you define workers, what is LittleOne? How is it laid out in memory? How is it initialized? The compiler can't know, it hasn't seen the class definition yet. So you must flip the definitions around:
class BigOne;
class LittleOne {
BigOne* master;
public:
LittleOne(BigOne*);
void Increment();
};
class BigOne {
public:
LittleOne workers[1] = {LittleOne(this)};
int increase = 0;
};
The forward declaration allows us to define members that accept and return pointers. So the class LittleOne can have its definition written before BigOne. And now BigOne can define members of type LittleOne by value.
The issue is with forward declaration.
You can refer the following link for more details and explanation.
What are forward declarations in C++?
I am struggling knowing how to create a class. I want to create a "Player" class and all I want to do is pass in the name while I'll have the other variables start at 0 until they are updated when a game is run (later in the program)
Player::Player(string name_in)
{
name = name_in;
int numOfWins = 0;
int numOfLoses = 0;
int numOfDraws = 0;
int totalMatches = 0;
}
Right now there are lots of errors around numOfWins, numOfLoses, numOfDraws and totalMatches. What can I do to fix this?
Perhaps the error is in your int ... part of assignments, which essentially creates a new local variable in a constructor.
Try this version:
#include <string>
using namespace std;
class Player
{
string name;
int numOfWins;
int numOfLoses;
int numOfDraws;
int totalMatches;
public:
Player(string name_in)
{
name = name_in;
numOfWins = 0;
numOfLoses = 0;
numOfDraws = 0;
totalMatches = 0;
}
};
You should declare other instance variables in the class declaration, rather than declaring them as locals (which is completely useless).
// This part goes in the header
class Player {
string name;
int numOfWins;
int numOfLoses;
int numOfDraws;
int totalMatches;
public:
Player(string name_in);
};
Now in the constructor you could use initialization lists:
// This part goes into the CPP file
Player::Player(string name_in)
// Initialization list precedes the body of the constructor
: name(name_in), numOfWins(0), numOfLoses(0), numOfDraws(0), totalMatches(0) {
// In this case, the body of the constructor is empty;
// there are no local variable declarations here.
}
Kinda vague, but I'll take a crack at it. You Probably want:
class Player{
string name;
int numOfWins;
int numOfLosses;
int numOfDraws;
int totalMatches;
Player(string name_in)
};
Player::Player(string name_in){
name = name_in;
numOfWins = 0;
numOfLosses = 0;
numOfDraws = 0;
totalMatches = 0;
}
Haven't used C++ in a while, so this may be faulty.
The errors you get, at least from the snippet you posted are caused for you can't declare variables in constructor - you declare them in class body and initialize in constructor or using another function.
#include <string>
class Player {
public:
Player( std::string const& name_in) : name( name_in),
numOfWins(), numOfLoses(),
numOfDraws(), totalMatches()
{} // constructor
// will initialize variables
// numOfWins() means default
// initialization of an integer
private:
std::string name;
int numOfWins;
int numOfLoses;
int numOfDraws;
int totalMatches;
};
usage:
int main() {
Player( "player_one");
return 0;
}
I wrote a small piece of code to test friend functions. It worked fine for methods that didn't belong to a specific class but when I tried to put it into a class all it can access is the public variables (just as any object would).
#include <iostream>
#include <conio.h>
using namespace std;
class something{
int ip = 100;
public:
int x = 100;
void getIP();
friend void cIP::changeIP(something);
};
void something::getIP(){
cout << ip << endl;
}
class cIP{
public:
int i;
cIP();
cIP(int nIP);
something some;
void changeIP(something s);
};
cIP::cIP(){
i = 100;
}
cIP::cIP(int nIP){
i = nIP;
}
void cIP::changeIP(something s){
s.ip = i;
}
s.ip brings up the error member is inaccessible.
I have a multilevel inheritance (from Ship class -> MedicShip class -> Medic class) with virtual function code as below. I suppose the result should be :
Medic 10
Medic 10
But it generated strange result. On the other hand, if I only use one level inheritance (from Ship class -> Medic class without MedicShip class in between) the result will be OK. Could you find my mistake please? Many thank....
#ifndef FLEET_H
#define FLEET_H
#include <string>
#include <vector>
using namespace std;
class Ship
{
public:
Ship(){};
~Ship(){};
int weight;
string typeName;
int getWeight() const;
virtual string getTypeName() const = 0;
};
class MedicShip: public Ship
{
public:
MedicShip(){};
~MedicShip(){};
string getTypeName() const;
};
class Medic: public MedicShip
{
public:
Medic();
};
class Fleet
{
public:
Fleet(){};
vector<Ship*> ships;
vector<Ship*> shipList() const;
};
#endif // FLEET_H
#include "Fleet.h"
#include <iostream>
using namespace std;
vector<Ship*> Fleet::shipList() const
{
return ships;
}
int Ship::getWeight() const
{
return weight;
}
string Ship::getTypeName() const
{
return typeName;
}
string MedicShip::getTypeName() const
{
return typeName;
}
Medic::Medic()
{
weight = 10;
typeName = "Medic";
}
int main()
{
Fleet fleet;
MedicShip newMedic;
fleet.ships.push_back(&newMedic);
fleet.ships.push_back(&newMedic);
for (int j=0; j< fleet.shipList().size(); ++j)
{
Ship* s = fleet.shipList().at(j);
cout << s->getTypeName() << "\t" << s->getWeight() << endl;
}
cin.get();
return 0;
}
You haven't created any instances of class Medic. Did you mean to say
Medic newMedic;
instead of
MedicShip newMedic;
perhaps? So, the Medic constructor isn't being called and weight and typeName aren't being initialized.
~Ship(){};
The first mistake is right here. This destructor should be virtual if you want to delete derived class objects through base class pointer.