类名不命名类型 - 拔掉我的头发

Class name does not name a type - Pulling my hair out

本文关键字:我的 头发 类型      更新时间:2023-10-16

我已经为此工作了几个小时,我不知道哪个类给我带来了问题。 我收到一堆"VendSlot"没有命名类型"错误。 感谢您提供的任何帮助。 我是C++新手,非常沮丧。

//class Snack
#ifndef SNACK_HPP
#define SNACK_HPP
#include <iostream>
#include <string>
using namespace std;
class Snack
{
private:
    string name;
    double price;
    int calories;
public:
    Snack();
    Snack(string name, double price, int calories);
    string getName();
    double getPrice();
    int getNumCalories();
};

#endif

零食.cpp

#include "Snack.hpp"
#include <iostream>
#include <string>
using namespace std;
Snack::Snack()
{
    name = "bottled water";
    price = 1.75;
    calories = 0;
}
Snack::Snack(string n, double p, int c)
{
    name = n;
    price = p;
    calories = c;
}
string Snack::getName()
{
    return name;
}
double Snack::getPrice()
{
    return price;
}
int Snack::getNumCalories()
{
    return calories;
}

VendSlot.hpp

#ifndef VENDSLOT_HPP
#define VENDSLOT_HPP
#include "Snack.hpp"
class VendSlot
{
private:
    Snack s;
    int amount;
public:
    VendSlot();
    VendSlot(Snack s, int a);
    Snack getSnack();
    int getAmount();
    int decrementAmount();
};
#endif

旺德老虎机.cpp

#include "VendSlot.hpp"
#include <iostream>
#include <string>
using namespace std;
VendSlot();
VendSlot::VendSlot()
{
    Snack s1;
    s = s1;
    amount = 5;
}
VendSlot::VendSlot(Snack s1, int a)
{
    s = s1;
    amount = 5;
}
Snack VendSlot::getSnack()
{
    Snack s1;
    s = s1;
    return s;
}
int VendSlot::getAmount()
{
    return amount;
}
int VendSlot::decrementAmount()
{
    amount--;
    return amount;
}

MiniVend.hpp

#ifndef MINIVEND_HPP
#define MINIVEND_HPP
#include "VendSlot.hpp"
class MiniVend
{
private:
    VendSlot vs1;
    VendSlot vs2;
    VendSlot vs3;
    VendSlot vs4;
    double money;
public:
    MiniVend();
    MiniVend(VendSlot v1, VendSlot v2, VendSlot v3, VendSlot v4, double money);
    int numEmptySlots();
    double getMoney();
    double valueOfSnacks();
    void buySnack();
};
#endif

迷你文德.cpp

#include "VendSlot.hpp"
using namespace std;
MiniVend::MiniVend(VendSlot v1, VendSlot v2, VendSlot v3, VendSlot v4, double m)
{
    vs1 = v1;
    vs2 = v2;
    vs3 = v3;
    vs4 = v4;
    money = m;
}
double MiniVend::getMoney()
{
    return money;
}
int MiniVend::numEmptySlots()
{
    int numSlots = 0;
    if (vs0.getAmount() == 0)
        slots++;
    if (vs1.getAmount() == 0)
        slots++;
    if (vs2.getAmount() == 0)
        slots++;
    if (vs3.getAmount() == 0)
        numSlots++;
    return numSlots;
}
double MiniVend::valueOfSnacks()
{
    double value = 0;
    value = (vs0.getSnack().getPrice() * vs0.getAmount() +
    (vs1.getSnack().getPrice() * vs1.getAmount()) +
    (vs2.getSnack().getPrice() * vs2.getAmount()) +
    (vs3.getSnack().getPrice() * vs3.getAmount()))
    return value;
}
void MiniVend::buySnack(int slot);
{
    if (slot == 0 && vs0.getAmount >= 1)
    {
        money = money + vs0.getSnack.getPrice();
        vs0.decrementAmount();
    }
    else if (slot == 1 && vs1.getAmount >= 1)
    {
        money = money + vs1.getSnack.getPrice();
        vs1.decrementAmount();
    }
    else if (slot == 2 && vs2.getAmount >= 1)
    {
        money = money + vs2.getSnack.getPrice();
        vs2.decrementAmount();
    }
    else if (slot == 3 && vs3.getAmount >= 1)
    {
        money = money + vs3.getSnack.getPrice();
        vs3.decrementAmount();#include "Snack.hpp"
    }
    else
        cout << "Sold out.  Please select another Snack." << endl;
}

主要

#include "MiniVend.hpp"
#include "Snack.hpp"
#include "VendSlot.hpp"
#include <iostream>
#include <string>
using namespace std;
 int main()
{
 Snack s2("candy bar", 1.25, 300);
 Snack s3("root beer", 2.00, 450);
 VendSlot groucho(s1, 2);
 VendSlot harpo(s2, 1);
 VendSlot chico(s3, 0);
 VendSlot zeppo;  // five bottles of water
 MiniVend machine(groucho, harpo, chico, zeppo, 0);
 cout << machine.numEmptySlots() << endl;
 cout << machine.valueOfSnacks() << endl;
 cout << machine.getMoney() << endl;
 machine.buySnack(1);
 cout << machine.numEmptySlots() << endl;
 cout << machine.valueOfSnacks() << endl;
 cout << machine.getMoney() << endl;
 return 0;
}
  • MiniVend.hpp你的 buySnack 不void buySnack();任何参数,但在 Minivend.cpp你在函数头上定义一个参数 void MiniVend::buySnack(int slot);,你也不应该使用分号。
  • 您的 MiniVend.cpp 文件也应该包含 "MiniVend.hpp" 标头。

  • 在MiniVend.cpp在函数声明中int MiniVend::numEmptySlots() vs0没有在代码的任何地方定义,也没有定义slots

  • 在你的主函数中,你试图声明这个VendSlot groucho(s1, 2);,但你没有在你的主函数的任何地方定义s1

这就是我到目前为止发现的。希望这有所帮助,请在发布问题时使用评论,以便每个人都可以理解代码的目的。