I'm designing a class hierarchy that follows a diamond pattern, and I'm trying to debug through about a million errors right now; however, most of them are simple fixes that I should be able to figure out. However, I'm having difficulty understanding the compiler's complaints in this one.
Basically, I start off with a simple Entity class that has two derived classes: Buyer and Seller. A fourth class Retailer, in turn, is descended from both classes - that is, it uses multiple inheritance(and yes, I know what kind of mess that's asking for, unfortunately that's exactly the point of the project).
for reference, the header files for my classes is as follows:
Entity.h
#pragma once
#include <string>
class Entity {
public:
Entity(std::string &, std::string &, double);
/*Accessor methods for private members*/
std::string getName();
std::string getID();
double getBalance();
/*Mutator methods for private members*/
void setName(std::string &);
void setID(std::string &);
void setBalance(double);
/*Additional methods*/
virtual void list();
virtual void step() = 0;
protected:
/*Private members of the entity class*/
std::string name;
std::string id;
double balance;
};
for the Buyer.h file
#pragma once
#include "Entity.h"
#include "Order.h"
#include "Seller.h"
#include <queue>
#include <string>
class Seller;
class Buyer : virtual public Entity {
public:
Buyer(std::string, std:: string, double);
virtual ~Buyer() { }
void addSeller(Seller *);
std::queue<Seller *> getSellers();
void addOrder(Order *);
void list();
void step() override;
protected:
std::queue<Order *> orders;
std::queue<Seller *> sellers;
};
For Seller.h
#pragma once
#include "Entity.h"
#include "Order.h"
#include "Buyer.h"
#include "Inventory.h"
#include <string>
#include <vector>
class Buyer;
class Seller : virtual public Entity {
public:
Seller(std::string, std::string, double);
virtual ~Seller() {}
void addBuyer(Buyer *);
std::vector<Buyer> getBuyers();
void setInventory(Inventory *);
Inventory * getInventory();
void list();
double fillOrder(Order *);
void step();
protected:
Inventory inventory;
std::vector<Buyer *> buyers;
};
And finally for Retailer.h
#pragma once
#include "Buyer.h"
#include "Seller.h"
#include <string>
class Retailer : public Buyer, public Seller {
public:
Retailer(std::string, std::string, double);
virtual ~Retailer() { }
void list();
void step();
};
The majority of the errors I get when trying to compile these files are along the lines of
Buyer.h:9:7: note: candidate expects 1 argument, 0 provided
Seller.h:14:3: note: candidate expects 3 arguments, 0 provided
Which is odd, because for that first line, I shouldn't even have to provide an argument and the second one is the definition of the constructor....
Basically, what I'm failing to understand is what does the compiler mean by a line of code expecting a different number of arguments than were provided? Should I be including default constructors that use no arguments? Is there something wrong with the way they're declared? I can also post the code for my .cpp files if necessary, although they don't seem to be mentioned a lot by the compiler error reports.
It means that the compiler is considering that function for overload resolution, but it's not a match because of differing number of arguments.
Related
the title might seem a little bit confusing but I'll try to explain that.
I've got a class CGameMode:
#pragma once
#include <string>
#include <iostream>
#include "../Hero/Hero.h"
#include "../Weapon/Weapon.h"
#include "../Armor/Armor.h"
class CGameMode
{
private:
CHero Hero;
CWeapon Weapon;
CArmor Armor;
std::string FileName = "save.txt";
protected:
public:
void InitializeGame();
void CreateGame();
void LoadGame();
void SaveGame();
CGameMode* GetGameMode() { return this; }
};
As you can see, the class contains objects of classes: CHero, CWeapon and CArmor. Let's take a look on one of these classes:
#pragma once
#include "../Enemy/Enemy.h"
#include "../GameMode/GameMode.h"
class CHero : public CEnemy
{
private:
int Energy = 0;
int ExperiencePoints = 0;
int Level = 0;
friend class CGameMode;
protected:
public:
CHero();
CHero(std::string Name, int Health, int Energy, int ExperiencePoints, int Level);
};
The most important is this line of code:
friend class CGameMode;
(CArmor and CWeapon also friend the CGameMode class).
The problem is the program does not compile. But if I remove Hero, Weapon and Armor members from CGameMode class, the program compiles and everything works just fine.
So the question is: How can I make the CGameMode class friended with CWeapon, CArmor and CHero and in the same time, the objects of these classes can be contained by CGameMode?
Having trouble understanding why I'm getting an 'undeclared identifier' error when I've made sure to include the header file that has the declaration of the class I'm making a vector of.
#pragma once
#include <vector>
#include "Member.h"
class Party {
private:
std::vector<Member> members;
public:
Party();
int get_party_size();
void add_member(Member new_member);
Member& get_member(int num);
};
Here's "Member.h"
#pragma once
#include <vector>
#include <string>
#include "Party.h"
class Member
{
private:
int hp;
bool is_stunned;
bool is_alive;
public:
Member();
~Member();
int get_hp();
bool get_is_stunned();
bool get_is_alive();
void take_damage(int amt);
void stun();
virtual void turn(std::vector<Party>& parties, int my_party, int my_member_number);
virtual std::string get_class_name();
};
Pretty new to the language, so sure I'm missing something obvious.
You have circular dependency between Member and Party
Remove the line
virtual void turn(
std::vector<Party>& parties,
int my_party,
int my_member_number);
in Member and remove the #include "Party.h" in Member.h
Instead think along the lines that a Party is just a collection of Members so there is no need for an individual Member to know about the container
So after input from #some-programmer-dude you could also solve it by adding a forward declaration in your Member.h instead of including the Party.h
class Party;
class Member { ... }
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Chess_tool
{
public:
Chess_tool(string color, char name);
virtual bool legal_movement(int source[], int dest[]) const = 0;
private:
string _color;
char _name;
};
Im trying to create chess game, so I create abstract class for chess tool (queen, king, rook...)
I also created king tool to check my code:
#pragma once
#include "Chess_tool.h"
class King : Chess_tool
{
public:
King(string color, char name);
virtual bool legal_movement(int source[], int dest[]);
};
and I create game_board class:
#pragma once
#include "Game_board.h"
#include "Chess_tool.h"
#include <iostream>
#define BOARD_SIZE 8
using namespace std;
class Chess_tool;
class Game_board
{
public:
Game_board();
~Game_board();
void move(string panel);
protected:
Chess_tool* _board[BOARD_SIZE][BOARD_SIZE];
};
the problem is here, when i try to add object to the matrix its show me error :
1 IntelliSense: object of abstract class type "King" is not allowed:
pure virtual function "Chess_tool::legal_movement" has no overrider
#pragma once
#include "Chess_tool.h"
#include "Game_board.h"
#include "King.h"
using namespace std;
enum Turn { WIHTE, BLACK };
class Manager : Game_board
{
public:
Manager();
~Manager();
virtual bool legal_movement(int source[], int dest[]) const = 0;
};
....
#include "Manager.h"
Manager::Manager()
{
_board[0][0] = new King();
}
The member function in the base class is const-qualified, not in the derived class.
So these are not the same functions through inheritance. You've declared a new virtual function, not overriden the first one.
Add const to the second one so that it actually override the base class function.
Remember that for virtual function overriding to kick in, there are a few condition to actually satisfy. They must have:
the same name
the same return type
the same parameters count and type
the same const-qualification (our case here)
a few other minor things (for example, compatible exceptions specifications)
If any condition isn't satisfied, you create a very similar, but different, function for the compiler.
With C++11, you should use override for the functions you want to override, so the compiler knows your intention and tells you that you've made a mistake. E.g.:
virtual bool legal_movement(int source[], int dest[]) override;
// ^^^^^^^^
just getting an odd error and I'm not entirely sure as to why.
I have 4 files (two headers and two implementations). The issue is inside the headers:
The main file ONLY includes the Station.h, which is why Stations.h is included inside it.
Station.h
#ifndef STATION_H
#define STATION_H
#include "Stations.h"
#include <string>
enum PassType{student, adult};
class Station{
std::string station_name;
unsigned int student_passes;
unsigned int adult_passes;
public:
Station();
void set(const std::string&, unsigned, unsigned);
void update(PassType, int);
unsigned inStock(PassType) const;
const std::string& getName() const;
};
#endif
Stations.h
#ifndef STATIONS_H
#define STATIONS_H
#include "Station.h"
namespace w2{
class Stations{
Station *station;
public:
Stations(char *);
void update() const;
void restock() const;
void report() const;
~Stations();
};
}
#endif
It doesn't know what Station is. I'm getting the following error:
./Stations.h:9:2: error: unknown type name 'Station'; did you mean 'Stations'?
Station *station;
What exactly am I missing here?
You are #includeing Stations.h in Station.h. As a result, the compiler sees class Stations before class Station. In this case, it doesn't appear that Station requires Stations, so you can simply remove the include.
If Station did need to know about Stations, then you'd have to use a forward declaration in one of the headers or the other (and be careful not to use the forward-declared class in a way that required the full definition).
Don't forget to put a semicolon after you declare the Stations class:
class Stations {
Station *station;
};
U need to do the forward declaration.
Remove the #include "Stations.h" from the Station.h
#ifndef STATIONS_H
#define STATIONS_H
#include "Station.h"
namespace w2{
class Station;
class Stations{
Station *station;
public:
Stations(char *);
void update() const;
void restock() const;
void report() const;
~Stations();
};
}
#endif
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 8 years ago.
I have these two classes that need each other and inherited from the same class. Wehn I compile Seller.h, it compiles fine but when I compile Buyer.h I get errors from Seller.h.
So when i compile Buyer.h I get errors such as :
Seller.h:14:16: error: âBuyerâ has not been declared
void addBuyer(Buyer*);
^
Seller.h:15:14: error: âBuyerâ was not declared in this scope
std::vector<Buyer*> getBuyers() const;
Seller.h:20:17: error: âOrderâ has not been declared
void fillOrder(Order*);
^
They are #included but it still says out of scope.
#ifndef SELLER_H
#define SELLER_H
#include "Entity.h"
#include <string>
#include <vector>
#include "Inventory.h"
#include "Buyer.h"
#include "Order.h"
class Seller : public virtual Entity
{
public:
Seller(const std::string &, const std::string &, double=0.0);
virtual~Seller(){}
void addBuyer(Buyer*);
std::vector<Buyer*> getBuyers() const;
void setInventory(Inventory*);
Inventory* getInventory() const;
virtual void list() const override;
virtual void step() override;
void fillOrder(Order*);
private:
Inventory* inv;
std::vector <Buyer*> buyers;
};
#endif
Buyer.h
#ifndef BUYER_H
#define BUYER_H
#include <string>
#include "Entity.h"
#include <queue>
#include "Order.h"
#include "Seller.h"
class Buyer : public virtual Entity
{
public:
Buyer(const std::string &, const std::string &, double =0.0 );
virtual ~Buyer(){}
void addSeller(Seller *);
std::queue <Seller *> getSellers() const;
void addOrder(Order *);
std::queue <Order*> getOrders() const;
virtual void list() const override;
virtual void step() override;
private:
std::queue <Order*> orders;
std::queue <Seller*> sellers;
};
#endif
You have a cyclic dependency between Seller and Buyer. This will never work because the compiler requires the declaration of Seller in order to compile Buyer... yet it also requires the declaration of Buyer to compile Seller.
You can instead forward declare your classes because all you actually use are pointers to these types. For example:
#ifndef SELLER_H
#define SELLER_H
#include "Entity.h"
#include <string>
#include <vector>
#include "Inventory.h"
#include "Order.h"
// forward declaration of Buyer
class Buyer;
class Seller : public virtual Entity
{
public:
Seller(const std::string &, const std::string &, double=0.0);
virtual ~Seller(){}
void addBuyer(Buyer*);
std::vector<Buyer*> getBuyers() const;
void setInventory(Inventory*);
Inventory* getInventory() const;
virtual void list() const override;
virtual void step() override;
void fillOrder(Order*);
private:
Inventory* inv;
std::vector <Buyer*> buyers;
};
#endif
If you had an instance of Buyer as a member of Seller (i.e., Buyer _buyer;), or if any method took/returned an instance of Buyer, you would be forced to change your structure. Since you don't have that problem, a forward declaration will suffice.
As an aside, and admitting that I am not privy to the structure of your program, it is usually a bad sign when one sees so many naked pointers being used in a C++ program. You can store instances. You can use safe pointers (shared_ptr and unique_ptr) depending on your ownership semantics.
For example, addBuyer can easily take a Buyer& instead of a pointer. Now you don't have to worry about invalid pointers. I'm assuming that you add these to your buyers vector... but how are you guaranteeing that these pointers remain valid for the lifetime of a Seller instance? You can't; you're at the mercy of whomever called addBuyer.
Why not just store a std::vector<Buyer> and take a reference in your add method? Is the cost of a copy so prohibitive as to warrant this design? If so, could you not use shared_ptr?