The question is hard to ask but I'll try to explain:
I have a class World that import Class Species, and holds list of Species.
I need to have pointer to class World in class Species, but if I import Class World before declaring Species errors occurs.
Example in pseudo code:
import B
Class A
{
list of B
}
Class B
{
Class A* a
}
so I need to import class A to make pointer to it, but when I do Class B is not declared in clas A.
my code:
#pragma once
#include <iostream>
class Species
{
protected:
std::string speciesName;
int strengh;
int effort;
int posX;
int posY;
int specieSymbol;
public:
Species();
Species(std::string speciesName, int strengh, int effort, int posX, int posY);
int getPosX();
int getPosY();
void setPosX(int x);
void setPosY(int y);
int getSpeciesSymbol();
std::string getSpeciesName();
virtual void action() = 0;
virtual void colision() = 0;
//void setWorld(World* w);
//World* getWorld();
~Species();
};
#pragma once
#include <iostream>
#include "Species.h"
#include <list>
#include "Board.h"
class World
{
private:
std::list <Species*> species;
Board* board;
public:
World(Board *board);
void addSpecies(Species* s);
void printSpecies();
void printWorld();
void Update();
~World();
};
Related
When I was doing my C++ homework yesterday, I tried using pure virtual functions in the basic class and completing them in derived class.
But there's something wrong. I googled it and I found I can't solve it. Google said that I need complete them in the derived class. And yes, I did. But still not work.
Here's Code.
//This is a cpp code
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include<iterator>
using namespace std;
class HardWare {
public:
virtual void Work()=0;
protected:
double num;string name2;
string name;
};
class CPU: public HardWare{
public: CPU(string a,double b) {
name=a;num=b;
}
void Work() override{
cout<<name<<" CPU Work with "<<num<<"GHz"<<endl;
}
};
class MainBoard: public HardWare{
public:
MainBoard(string a,string b){
name=a;name2=b;
}
void Work() override{
cout<<name<<" MainBoard Work with "<<name2<<endl;
}
};
class HardDisk: public HardWare{
public:
HardDisk(string a,double b) {
name=a;num=b;
}
void Work() override{
cout<<name<<" HardDisk Work with "<<num<<"G"<<endl;
}
};
class PC{
protected:
vector<HardWare> a;
public:
void AddToPC(HardWare *p){
a.push_back(*p);
}
void Work(){
for (vector<HardWare>::iterator it=a.begin();it!=a.end();it++)
it->Work();
cout<<"PC Work";
}
};
//StudybarCommentBegin
int main()
{
PC *p = new PC();
HardWare *p1 = new CPU("Intel",4.5);
HardWare *p2 = new CPU("AMD",3.8);
HardWare *p3 = new MainBoard("ASUS","ATX");
HardWare *p4 = new HardDisk("Seagate",500);
HardWare *p5 = new HardDisk("Sumsang",1000);
p->AddToPC(p1);
p->AddToPC(p2);
p->AddToPC(p3);
p->AddToPC(p4);
p->AddToPC(p5);
p->Work(); //output all of the parts of the PC
}
//StudybarCommentEnd
I want to be able to use private variables from one class in another class fx with regards to A_class I want to be able to use the values of the int n,m and the arrays A and b. With regards to a and m I tried setting them equal to public variables, but that didn't work
class A_class{
public:
void Indlaes();
private:
double A[Nmax][Nmax],b[Nmax];
int n,m;
};
class B{
public:
void Indtast_b();
void Overfoer_b();
private:
double A[Nmax][Nmax],b[Nmax];
int n,m;
};
class C{
public:
void Indlaes_C();
void Indtast_C1();
void Indtast_C2();
void Overfoer_C();
void Projektion_b();
private:
double A84[Nmax][Nmax],Q[Nmax][Nmax],R[Nmax][Nmax],A[Nmax][Nmax],b[Nmax];
int n,m;
};
class Metode1{
public:
void brug2(double A1[Nmax][Nmax],double b1[Nmax]){
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
A[i][j]=A1[i][j];
}
}
for(int i=0;i<n;++i)b[i]=b1[i];
}
void Metode1_MatrixProd();
void Metode1_MatrixVekProd();
void Metode1_DanTotalMatrix();
void Metode1_Gauss();
void Metode1_Backwardsubstitution();
void Metode1_UdskrivVektor();
void Kontrol_Metode1();
private:
double A[Nmax][Nmax],M[Nmax][Nmax],AT[Nmax][Nmax],b[Nmax],W[Nmax],TM[Nmax][Nmax],FM[Nmax][Nmax],sum,x[Nmax],bpNy[Nmax];
int n,m;
};
If you want other classes to have access to certain variables, you should use public and inheritance.
class A_class
{
public:
int whatever;
};
#include "A_class"
class B : public A_class
{
public:
// stuff
};
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 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().
so, i have basically designed a "Hello, World!" OOP with c++.
I have 2 classes, chicken and dog that inherit from public animal.
In int main, when i create an instance of each, i get error messages that claims i have created multiple instances of the class Animal.
Animal.h
#ifndef ANIMAL
class Animal
{
int x;
int y;
int z;
public:
void setPosition(int newX, int newY, int newZ);
void setX(int newX);
void setY(int newY);
void setZ(int newZ);
int getPosition();
int getX();
int getY();
int getZ();
};
#endif
Chicken.h
#ifndef ANIMAL_H
#include "../animal.h"
#endif
class Chicken : public Animal
{
int id;
bool isClucking;
bool isEnraged;
public:
void setID(int newID);
void setClucking(bool yn);
void setEnraged(bool yn);
int getID();
bool getClucking();
bool getEnraged();
};
Dog.h
#include "../animal.h"
class Dog : public Animal
{
int id;
public:
void setID(int newID);
int getID();
};
Code here: Source Code
You have the animal.h defined as
#ifndef ANIMAL
class Animal
{
should be
#ifndef ANIMAL
#define ANIMAL
class Animal
{
so that animal.h is not included multiple times.
Also for consistency, have all your headers have a include guard. So dog and chicken need it.
Unless you really need it
#ifndef ANIMAL_H
#include "../animal.h"
#endif
should simply be
#include "../animal.h"
because the former construct requires the define exactly match which in your cause is already not matching. animal.h defines ANIMAL or at least tries to but where you include is you are checking for ANIMAL_H