我已经在这工作了好几个小时,我无法弄清楚哪个班级给我带来了问题。我得到了一堆' 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
Snack.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
VendSlot.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
MiniVend.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;
}
答案 0 :(得分: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
这就是我到目前为止所发现的。希望这有帮助,请在发布问题时使用评论,以便每个人都能理解代码的目的。