C++ 如何在cpp中保持子类的对象在另一个类中?基类未定义错误
我正在用cpp编写简单的商店程序。我有三门课:商店、客户、水桶。Shop是Bucket的父类。商店有客户载体,每个客户都有自己的桶。 我对“包括”有意见。我必须在Shop.h中包含Client.h,这样商店才能看到客户的向量,但出于类似的原因,我似乎还必须在Clinet.h中包含Bucket.h。 这就产生了一个问题:Bucket是在购物前包含的,所以我得到了“基类未定义”错误。 我怎样才能做到这一点 商店C++ 如何在cpp中保持子类的对象在另一个类中?基类未定义错误,c++,inheritance,C++,Inheritance,我正在用cpp编写简单的商店程序。我有三门课:商店、客户、水桶。Shop是Bucket的父类。商店有客户载体,每个客户都有自己的桶。 我对“包括”有意见。我必须在Shop.h中包含Client.h,这样商店才能看到客户的向量,但出于类似的原因,我似乎还必须在Clinet.h中包含Bucket.h。 这就产生了一个问题:Bucket是在购物前包含的,所以我得到了“基类未定义”错误。 我怎样才能做到这一点 商店 #pragma once #include <vector> #includ
#pragma once
#include <vector>
#include <string>
#include "functions.h"
#include "Client.h"
class Shop {
protected:
std::vector<int> quantities;
std::vector<std::string> products;
std::vector<float> prices;
private:
std::vector<Client*> clients;
int loggedClient;
public:
Shop();
~Shop();
int readProducts();
int loadClientsBase();
int checkLoginData(std::string log, std::string pass, int *logged);
int checkIfSameLogin(std::string log);
int addClient();
void login();
void logout();
int sell();
virtual void display();
void displayLoggedClient();
int saveHistory();
};
#pragma一次
#包括
#包括
#包括“functions.h”
#包括“Client.h”
班级商店{
受保护的:
std::矢量量;
std::向量积;
病媒价格;
私人:
向量客户端;
int loggedClient;
公众:
商店();
~Shop();
int readProducts();
int loadClientsBase();
int checkLoginData(std::字符串日志,std::字符串传递,int*日志);
int checkIfSameLogin(标准::字符串日志);
int addClient();
无效登录();
无效注销();
int sell();
虚拟虚空显示();
void displayLoggedClient();
int saveHistory();
};
客户:h
#pragma once
#include "Bucket.h"
class Client
{
private:
float money=0.0;
Bucket bucket;
std::string login;
std::string password;
std::string description;
public:
Client();
~Client();
void addLoginData(std::string log, std::string pass, std::string desc, float mon);
std::string getLogin() { return login; };
std::string getPassword() { return password; };
std::string getDescription() { return description; };
float getMoney() { return money; };
void addLogin(std::string log);
void addPassword(std::string pass);
void addDescription(std::string desc);
void addMoney(float m);
void addToBucket(std::string prod, int quant, float price);
void displayBucket();
Bucket getBucket() { return bucket; };
friend std::ostream& operator<<(std::ostream& os, Client& client);
};
#pragma一次
#包括“Bucket.h”
类客户端
{
私人:
浮动货币=0.0;
铲斗;
std::字符串登录;
std::字符串密码;
std::字符串描述;
公众:
客户机();
~Client();
void addLoginData(std::string log、std::string pass、std::string desc、float mon);
std::string getLogin(){return login;};
std::string getPassword(){return password;};
std::string getDescription(){return description;};
float getMoney(){return money;};
void addLogin(std::字符串日志);
void addPassword(std::string pass);
void addDescription(std::string desc);
无效附加货币(浮动m);
void addToBucket(标准::字符串产品、整数数量、浮动价格);
void displayback();
Bucket getBucket(){return Bucket;};
friend std::ostream&operator要解决眼前的问题,您需要在Shop.h
中转发声明Client
类。但是,您在这里构建的类层次结构至少可以说是混乱的,例如,每个客户端都拥有一个Shop
,将类层次结构更改为更合理的应该删除问题就在这里。要解决眼前的问题,您需要在Shop.h
中向前声明客户机
类。但是,您在这里构建的类层次结构至少可以说是混乱的,例如每个客户机
都拥有一个商店
,将类层次结构更改为更合理的应该删除问题就在这里。Bucket.h
包括Shop.h
,其中包括Client.h
,其中包括Bucket.h
…等等。这是一种循环依赖关系
Shop.h
不需要包含Client.h
,它只需要向前声明Client
类:
#pragma once
#include <vector>
#include <string>
#include "functions.h"
// #include "Client.h"
class Client; // Forward declaration of the class, that's needed for pointers to it
class Shop {
...
};
#pragma一次
#包括
#包括
#包括“functions.h”
//#包括“Client.h”
类客户机;//类的前向声明,这是指向它的指针所需要的
班级商店{
...
};
Shop
的实现需要Client
的完整定义,因此Shop
源文件(Shop.cpp
?)需要包括Client.h
Bucket.h
包括Shop.h
,其中包括Client.h
,其中包括Bucket.h
…等等。这是一个循环依赖项
Shop.h
不需要包含Client.h
,它只需要向前声明Client
类:
#pragma once
#include <vector>
#include <string>
#include "functions.h"
// #include "Client.h"
class Client; // Forward declaration of the class, that's needed for pointers to it
class Shop {
...
};
#pragma一次
#包括
#包括
#包括“functions.h”
//#包括“Client.h”
类客户机;//类的前向声明,这是指向它的指针所需要的
班级商店{
...
};
Shop
的实现需要Client
的完整定义,因此Shop
源文件(Shop.cpp
?)需要包含Client.h
bucket以何种方式是一种商店?bucket以何种方式是一种商店?