C++ 类定义需要参数?
我正在设计一个遵循菱形模式的类层次结构,现在我正试图调试大约一百万个错误;然而,其中大多数都是简单的修复,我应该能够弄明白。然而,我很难理解编译器在这篇文章中的抱怨 基本上,我从一个简单的实体类开始,它有两个派生类:买方和卖方。第四类零售商又是这两类零售商的后代——也就是说,它使用多重继承(是的,我知道这会造成什么样的混乱,不幸的是,这正是项目的重点) 作为参考,我的类的头文件如下:C++ 类定义需要参数?,c++,constructor,virtual,multiple-inheritance,C++,Constructor,Virtual,Multiple Inheritance,我正在设计一个遵循菱形模式的类层次结构,现在我正试图调试大约一百万个错误;然而,其中大多数都是简单的修复,我应该能够弄明白。然而,我很难理解编译器在这篇文章中的抱怨 基本上,我从一个简单的实体类开始,它有两个派生类:买方和卖方。第四类零售商又是这两类零售商的后代——也就是说,它使用多重继承(是的,我知道这会造成什么样的混乱,不幸的是,这正是项目的重点) 作为参考,我的类的头文件如下: Entity.h #pragma once #include <string> class Ent
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;
};
对于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;
};
#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();
};
我在编译这些文件时遇到的大多数错误都是
Buyer.h:9:7: note: candidate expects 1 argument, 0 provided
Seller.h:14:3: note: candidate expects 3 arguments, 0 provided
这很奇怪,因为对于第一行,我甚至不需要提供参数,第二行是构造函数的定义
基本上,我没有理解的是,编译器所说的一行代码期望的参数数量与提供的参数数量不同是什么意思?我应该包括不使用参数的默认构造函数吗?它们的申报方式有问题吗?如果有必要,我也可以发布.cpp文件的代码,尽管编译器错误报告中似乎没有太多提到它们。这意味着编译器正在考虑使用该函数来解决重载问题,但由于参数数量不同,因此不匹配。这些“错误”您发布的是与您未发布的其他错误相关的附加信息。尝试发布实际错误。可能您没有在代码中正确调用实体的构造函数,而您并没有显示该代码。显示所有与错误相关的代码。实际上,我一点以前就意识到了这一点。不幸的是,完整的错误列表比我实际复制到帖子中的时间长得多。然而,下面的答案已经帮助我在大多数代码中找到了导致编译器出错的问题。似乎我的大多数问题实际上都是由于未能将某些文件包含在彼此中而导致的——换句话说,循环引用失败。在这种情况下,从第一个错误开始并修复它,有时这也会修复由第一个eror引起的其他错误。
Buyer.h:9:7: note: candidate expects 1 argument, 0 provided
Seller.h:14:3: note: candidate expects 3 arguments, 0 provided