错误 C2504:"自动售货机":未定义基类

Error C2504: 'Vending': base class undefined

本文关键字:基类 未定义 自动售货机 错误 C2504      更新时间:2023-10-16

我有两个类,Vending和Payment。付款是自动售货的孩子。我不断得到"基类未定义"的错误在我的代码。

下面是两个头文件:

//Parent class (Vending.h)
#ifndef VENDING_H
#define VENDING_H
#include "Main.h";
namespace Vending
{
    class Vending
    {
    public:
        Vending();
        Vending(int);
        void setRequiredAmount(int);
        int getRequiredAmount();

    protected:
        int selectedItem;
        int requiredAmount;
    };
}
#endif VENDING_H
//child class (Payment.h)
#ifndef PAYMENT_H
#define PAYMENT_H
#include "Vending.h"
namespace Vending
{
    class Payment : public Vending
    {
    public:
        Payment(int);
        int getEnteredAmount();
        void setEnteredAmount(int);
        protected:
        int enteredAmount;

    };
}
#endif PAYMENT_H

如果我能得到一些帮助来解决这个错误,我将不胜感激

你说Main.h包括Payment.h,这确实会导致循环依赖。阅读这篇文章了解更多信息:http://forums.codeguru.com/showthread.php?288147-C2504-Base-class-undefined-(other-posts-have-no-solution)&p=919112#post919112

你需要重新考虑你的项目,这样的情况不应该发生。只需尝试从Vending.h中删除#include "Main.h",并编译Payment.cpp…