2017-05-13 3 views
-2

私はこれを数時間続けてきましたが、どのクラスが私に問題をもたらしているのか分かりません。私は。私はCに新しい++と非常にイライラしています「のエラーが。私はあなたが提供することができます任意の助けに感謝タイプに名前を付けていない「VendSlot」の束を得る。クラス名の型名がわかりません - 私の髪の毛を引っ張って

//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; 
} 
あなたのbuySnackがパラメータ void buySnack();を取ることはありませんが、Minivend.cppであなたは MiniVend.hppで3210

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; 
} 
+1

'MiniVend.cpp'は' MiniVend.hpp'を含んでいません。 – user2357112

+0

'VendSlot();' - この行の目的は 'VendSlot.cpp'の先頭近くですか? – user2357112

+0

不必要なものではなく、質問にエラーメッセージを含めてください。エラーメッセージは、主に問題のある行を示します。その質問には関連するものだけを含める。 –

答えて

0
  • 関数ヘッダーで引数を定義しています void MiniVend::buySnack(int slot);、セミコロンも使用しないでください。
  • MiniVend.cppファイルには、"MiniVend.hpp"ヘッダーも含める必要があります。機能int MiniVend::numEmptySlots()vs0の宣言でMiniVend.cppで

  • slots

  • だけでなく、コードのどこに定義されていないで、あなた、あなたがこのVendSlot groucho(s1, 2);を宣言しようとしているが、あなたはs1を定義していない主な機能あなたの主な機能のどこでも

これまでに私が見つけたのはこれです。これがうまくいくと思っています。皆さんがコードの目的を理解できるように、質問を投稿する際にコメントを使用してください。

関連する問題