Im trying to use Inheritance through multiple header and cpp files for a text game that I'm writing.
I have my base class of Weapon. Which is in the file Weapon.h
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
I am then trying to inherit form the base Weapon.h class to a Bow and Sword class. I am sure I am including the file correctly but when I try to compile I get the error "error: expected class name class Blade : public Weapon" The same error for the Bow class.
#include "Weapon.h"
#include "Crossbow.h"
using namespace std;
class Bow : public Weapon
{
public:
string Type = "Ranged";
bool loaded;
protected:
Bow();
};
#include "Weapon.h"
class Blade : public Weapon
{
private:
string Type = "Melee";
protected:
void Draw();
};
Does anyone know why this is happening? Google isn't coming up for anything useful either. Thanks
MCVE (I think)
//In Base.h
class Base
{
public:
int function();
private:
};
//In Base.cpp
int Base::function()
{
randomshit
return 0;
}
//In Inherit.h
#include "Base.h"
class Inherit : public Base
{
public:
int function():
private:
};
Getting error: "expected class name class Bow : public Weapon"
EDIT: Turns out I needed to include "#pragma once" and that solved almost everything. Thanks for the help guys.
You don't use any include guards, so your file Weapon.h is probably included multiple times, leading to the compilation error.
To know more about include guards: https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Include_Guard_Macro
Your header Weapon.h would then become:
#ifndef WEAPON_H_INCLUDED
#define WEAPON_H_INCLUDED
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
#endif // WEAPON_H_INCLUDED
Do the same for all other header files.
Once you did that, remove all unnecessary includes and do a clean rebuild.
This is maybe not an answer but it just is impossible to post it as comment
This compiles (but does not link !!) on my Visual Studio 2013.
#include <string>
using namespace std;
class Weapon
{
public:
string Name;
int Damage;
float ChanceToHit;
int ExtraDamage;
int Result;
int Array[3];
int Attack(int, int, string);
};
class Bow : public Weapon
{
public:
string Type = "Ranged";
bool loaded;
protected:
Bow();
};
class Blade : public Weapon
{
private:
string Type = "Melee";
protected:
void Draw();
};
But it is likely to fail on older compilers because of the initialisation in the declaration as string Type = "Melee";.
Note that using namespace std; comes before the declaration of class Weapon.
Related
Hello so i want to create a header file class which name testing and also its cpp but for some reason this is inaccessible i dont know why
testing.h
#ifndef TESTING_H
#define TESTING_H
#include <string>
using namespace std;
class testing
{
string Name;
void printname(string name);
};
#endif
testing.cpp
#include <iostream>
#include "testing.h"
using namespace std;
void testing::printname(string name) // inaccessible in my main i dont know what reason :(
{
Name = name;
cout<<Name<<endl;
}
main
#include <iostream>
#include "testing.h"
using namespace std;
using std::string;
int main()
{
testing tester;
tester.printname("JPR"); //error since testing::printname is inaccessible no idea
return 0;
}
If you don't specify the visibility of the members, they are private.
You can either use a struct (visibility is public):
struct testing
{
string Name;
void printname(string name);
};
or you can specify that printname is public:
class testing
{
public:
void printname(string name);
private:
string Name;
};
Try the following:
testing.h
#ifndef TESTING_H
#define TESTING_H
#include <string>
class testing
{
public:
// Better to pass the parameter as const reference to avoid performing a copy.
void printname(const std::string& name);
private:
std::string Name;
};
#endif
testing.cpp
#include <iostream>
#include "testing.h"
void testing::printname(const std::string& name)
{
Name = name;
std::cout << Name << std::endl;
}
main.cpp
#include "testing.h"
int main()
{
testing tester;
tester.printname("JPR");
return 0;
}
In C++:
A class defined with the keyword class has private access for its members and its base classes by default.
Add public: access modifier to your class defition, to mark method as public:
class testing
{
string Name;
public:
void printname(string name);
};
when you create a class, every member function and member variable is set in default as private, which means that they won't be accessible. To make your function public you need to change this in your code:
class testing
{
private: //is private in default, i add it for better readabilty
string Name;
public:
void printname(string name);
};
Worth to mention that you (almost) ALWAYS want to keep all member variables private!
I have an abstract class called box that only has a string parameter called type and a virtual string that only returns that parameter. This class is the father of another class called Reward that has the constructor that fills the the parameter type and the same function of Box. I'm trying to put them in a class called Board that is basically a vector of the pointers of these classes, I have a constructor that just fills the vector with rewards. The problem comes that I compille it but doesn't run and I cannot find where is the problem. Thanks
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Box{
protected:
string type;
public:
virtual string getType()=0;
};
class Reward : public Box{
public:
string getType(){
return type;
}
Reward(){
type = "Reward";
}
};
class Board{
private:
vector<Box *> Table;
public:
Board(){
for (int i=0;i<=30;i++){
Table[i]=new Reward;
}
}
string getBoxes(int i){
return Table[i]->getType();
}
};
int main(){
Board game;
string toPrint=game.getBoxes(2);
cout<<toPrint;
return 0;
}
I'm getting this->
error -type ‘Ship’ is not a direct base of ‘CruiseShip’-
I cannot figure it out. This is where the error is occurring I am assuming. I am not really sure I guess how I should be calling the base class?
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y)
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
//A print function that overrides the print function in the base class.
//The CruiseShip class's print function should display only the ship's
//name and the maximum number of passengers.
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include <string>
using namespace std;
class Ship;
class CruiseShip{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.cpp
#include "Ship.h"
#include <iostream>
using namespace std;
string name;
string built;
Ship::Ship(){
}
Ship::Ship(string n, string b)
{
name = n;
built = b;
}
//accessors and mutators methods
string getName()
{
return name;
}
string getBuilt()
{
return built;
}
//A virtual print function that displays
//the ship's name and the year it was built
void print()
{
cout<<"Name:"<<getName()<<"\nYear built:"<<getBuilt()<<endl;
cout<<"-------------------------"<<endl;
}
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif
You need to derive CruiseShip from Ship:
class CruiseShip : public Ship {
this:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y)
^^^^^^^^^^^^
is a base class constructor call, but you have not derived CruiseShip from Ship, yet compiler knows that Ship is a class type.
I wrote a template class but it just works for int, double and does not work for my own class. The code is as follows:
/*OrderedList.h*/
namespace MYLIB
{
class Student
{
public:
display();
private:
char* snum;
char* name;
char* email;
};
template<class T>
class OrderedList
{
public:
OrderedList() :head(), tail(), size(0) {}
~OrderedList(){}
private:
......
};
}
/*main.cpp*/
#include "OrderedList.h"
int main()
{
MYLIB::OrderedList<int> listi;
MYLIB::OrderedList<double> listd;
MYLIB::OrderedList<Student> lists;
}
The error message I get is: 'Error: Student is not defined.' Thank you for help.
Student is defined in the MYLIB namespace, just like class template OrderedList. So you need
MYLIB::OrderedList<MYLIB::Student>
I have a question about the factory pattern. I programmed a factory, that has a static function called registerIt. It accepts a string for the class name and a pointer to the creator function of any class.
static void CharacterFactory::registerit(const std::string& classname, Creator creator)
{
table[classname] = creator;
}
The table is
std::map<std::string, CharacterFactory::Creator> CharacterFactory::table;
Creator
typedef std::auto_ptr<Actor> Type;
typedef Type (*Creator)();
Actor is the Base class
The classes itself have function for registering. For example the class "Player"
static void registerToFactory(){
CharacterFactory::registerit("Player",&create);
std::cout<<"player created"<<std::endl;
}
My Problem is, how can I tell the classes to register itself to the static factory? Everything works, if I call registerToFactory in the main class. But I want to do it more dynamically, so I only have to change code in new classes and not everywhere in my code.
The whole code below:
Factory.h:
#pragma once
#include "Actor.h"
#include <string>
#include<map>
namespace Character{
class Actor;
class CharacterFactory
{
public:
typedef std::auto_ptr<Actor> Type;
typedef Type (*Creator)();
CharacterFactory(void);
~CharacterFactory(void);
Type create(const std::string& classname);
static void registerit(const std::string& classname, Creator creator);
private:
static std::map<std::string, Creator> table;
};
}
Actor:
#pragma once
#include<string>
#include"CharacterFactory.h"
#include<iostream>
namespace Character{
class Actor
{
public:
static Actor* create(){std::cout<<"dummy"<<std::endl;return NULL;};
static Actor* create(int dmg){std::cout<<"dummy"<<std::endl;return NULL;};
Actor(void):damage(0),healthPoints(0),lastUpdate(0){};
Actor(int dmg):damage(dmg){};
~Actor(void);
virtual void update(void)=0;
virtual void update(int deltaMillis)=0;
protected:
int lastUpdate;
//Attribute
int healthPoints;
int damage;
//Amor amor;
//Weapon weapon;
//Ai ai;
//Networking
};
}
Player:
#pragma once
#include "Actor.h"
#include <stdio.h>
#include "CharacterFactory.h"
namespace Character{
#ifndef PLAYER_H
#define PLAYER_H
class Player:public Actor
{
public:
void update(void){};
void update(int deltaMillis){};
static std::auto_ptr<Actor> create(){
return std::auto_ptr<Actor>(new Player);
}
Player(void);
~Player(void);
static void registerToFactory(){
CharacterFactory::registerit("Player",&create);
std::cout<<"player created"<<std::endl;
}
inline int getDamage(void){ return damage;};
};
#endif
}
I hope you can help me :)
You can use either the constructor or dynamic initialisation expression for a static variable, and the compiler will ensure that runs before calling main().