2013-05-02 72 views
0

我想了解更多關於向量和存儲對象的信息。我正在從txt文件讀取數據。我看不出我犯了什麼錯誤,因爲它不起作用。對象向量C++

這裏是我的主要方法

void Reader::readFoodFile() { 
    string name; 
    int cal, cost; 
    ifstream file; 
    file.open("food.txt"); 
    while (file >> name >> cal >> cost) { 
     Food f(name, cal, cost); 
     foodStorage.push_back(f); 

    } 
} 
void Reader::getStorage() { 
    for (unsigned int i = 0; i < foodStorage.size(); i++) { 
     cout << foodStorage[i].getName(); 
    } 
} 

,這裏是我的菜構造:

Food::Food(string newName, int newCalories, int newCost) { 
    this->name = newName; 
    this->calories = newCalories; 
    this->cost = newCost; 
} 

在我的main.cpp文件,我只是創建對象讀卡器(現在還沒有構造函數)和調用方法。

int main(int argc, char** argv) { 

     Reader reader; 
     reader.readFoodFile(); 
     reader.getStorage(); 
} 

我想使用從txt文件中獲取數據然後將其打印出來(現在)的對象填充Vector。

有什麼建議嗎?

編輯;我的.txt文件的佈局是

apple 4 2 
strawberry 2 3 
carrot 2 2 

這是我Food.h和Reader.h

#ifndef FOOD_H 
#define FOOD_H 

#include <string> 
#include <fstream> 
#include <iostream> 

using namespace std; 

class Food { 
public: 
    Food(); 
    Food(string, int, int); 
    Food(const Food& orig); 
    virtual ~Food(); 
    string getName(); 
    int getCalories(); 
    int getCost(); 
    void setCost(int); 
    void setCalories(int); 
    void setName(string); 
    int calories, cost; 
    string name; 
private: 

}; 

#endif /* FOOD_H */` 

and Reader.h 
`#ifndef READER_H 
#define READER_H 
#include <string> 
#include <fstream> 
#include <iostream> 
#include <vector> 
#include "Food.h" 

using namespace std; 

class Reader { 
public: 
    Reader(); 
    Reader(const Reader& orig); 
    virtual ~Reader(); 

    void readFoodFile(); 
    void getStorage(); 
    vector<Food> foodStorage; 

private: 

}; 

#endif /* READER_H */ 
+1

你看到的不正確的行爲是什麼? – yiding 2013-05-02 00:12:20

+0

當我嘗試調用getStorage()時,我沒有輸出,只是一個空行。 – ChrisA 2013-05-02 00:16:13

+0

你用什麼來調試代碼? – johnathon 2013-05-02 00:20:41

回答

0

我剛剛編譯你的代碼,它爲我工作得很好......我用G ++ .... 事情你需要修復..

  1. 擺脫你的.h有構造函數文件並且未在.cpp中定義
  2. 給出getName的實現。

以下是我的代碼:

food.h

#ifndef FOOD_H 
#define FOOD_H 

#include <string> 
#include <fstream> 
#include <iostream> 

using namespace std; 

class Food { public: 
    //Food(); 
    Food(string, int, int); 
    //Food(const Food& orig); 
    //virtual ~Food(); 
    string getName(); 
    int getCalories(); 
    int getCost(); 
    void setCost(int); 
    void setCalories(int); 
    void setName(string); 
    int calories, cost; 
    string name; private: 

}; 

#endif 

food.cpp

#include<iostream> 
#include<string> 
#include "food.h" 
using namespace std; 
Food::Food(string newName, int newCalories, int newCost) { 
    this->name = newName; 
    this->calories = newCalories; 
    this->cost = newCost; } string Food::getName() { 
     return name; } 

reader.h

#ifndef READER_H 
#define READER_H 
#include <string> 
#include <fstream> 
#include <iostream> 
#include <vector> 
#include "food.h" 

    using namespace std; 

    class Reader { public: 
     //Reader(); 
     //Reader(const Reader& orig); 
     //virtual ~Reader(); 

     void readFoodFile(); 
     void getStorage(); 
     vector<Food> foodStorage; 

    private: 

    }; 

    #endif 

重ader.cpp

#include <iostream> 
#include "reader.h" 

using namespace std; void Reader::readFoodFile() { 
    string name; 
    int cal, cost; 
    ifstream file; 
    file.open("food.txt"); 
    while (file >> name >> cal >> cost) { 
     Food f(name, cal, cost); 
     foodStorage.push_back(f); 

    } } void Reader::getStorage() { 
    for (unsigned int i = 0; i < foodStorage.size(); i++) { 
     cout << foodStorage[i].getName(); 
    } } 

的main.cpp

#include<iostream> 
#include<fstream> 
#include "food.h" 
#include "reader.h" 

using namespace std; 

int main(int argc, char** argv) { 
     Reader reader; 
     reader.readFoodFile(); 
     reader.getStorage(); 
} 

我使用編譯 - g++ main.cpp reader.cpp food.cpp

和輸出我得到 - applestrawberrycarrots

所以,我不知道是什麼你失蹤....希望這可以幫助

我沒有添加std::endl ..如果你確實添加了你有std::cout的地方,那麼每個項目都會在他們自己的行上。 此外,你應該使用close流。

+0

非常感謝。去除 '食品(); 食品(常規食品和原料); 虛擬〜食物();' 幫助我解決問題。我將銘記未來! – ChrisA 2013-05-02 10:29:07

+0

很高興幫助...每當你在'.h'中定義一個構造函數,但是dnt給出一個定義時,C++將會抱怨'undefined reference',因爲它不知道如何實例化構造函數(http://stackoverflow.com/questions/5393293 /未定義引用到classclass) – Bill 2013-05-02 12:26:40

0

我猜你的exe文件與你所期望的一個不同的相對路徑運行。調用file.open()後檢查file.good()是否爲true。當編程IDE(例如Visual Studio)使用不同的工作文件夾運行帶有exe的文件時,這個問題很常見。