2013-05-02 2 views
0

벡터에 대해 자세히 배우고 그 안에 개체를 저장하려고합니다. 나는 txt 파일에서 데이터를 읽는다. 내가하고있는 실수가 작동하지 않는다는 것을 나는 볼 수 없다. 내 MAIN.CPP 파일에서Object Vector C++

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

난 그냥 객체 리더 (지금은없는 생성자)를 만드는거야 및 :

여기 내 주요 방법은

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(); 
    } 
} 

그리고 여기 내 식품 생성자되어 있습니다 메소드를 호출하십시오.

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

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

벡터를 txt 파일의 데이터를 가져 와서 (지금은) 인쇄하는 개체로 채우고 싶습니다.

제안 사항?

편집; 내 .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

+0

getStorage()를 호출 할 때 출력이 없으며 빈 줄만 표시됩니다. – ChrisA

+0

코드를 디버깅 할 때 무엇을 사용하고 있습니까? – johnathon

답변

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::cout가있는 곳을 추가하면 각각의 항목이 자신의 줄에 추가됩니다. std::endl을 추가하지 않았습니다. 또한 사용을 마친 후 close 스트림을 보내야합니다.

+0

고맙습니다. 'Food(); 음식 (식품 & 오트 섭취); virtual ~ Food(); ' 문제 해결을 도왔습니다. 미래를 염두에 두겠습니다. – ChrisA

+0

다행스럽게도 ... .h로 생성자를 정의하지만 정의를 제공하지 않을 때마다 C++은 생성자를 인스턴스화하는 방법을 이해하지 못하기 때문에'undefined reference'라고 불평합니다 (http://stackoverflow.com/questions)./5393293/undefined-reference-to-classclass) – Bill

0

나는 당신의 EXE가 예상 한 것과 다른 상대 경로로 실행 같은데요. file.open()을 호출 한 후 file.good()가 true인지 확인하십시오. 이 문제는 프로그래밍 IDE (예 : Visual Studio)가 exe가 작성되는 다른 작업 폴더로 exe를 실행할 때 일반적입니다.

난 그냥 코드를 컴파일하고 나를 위해 잘 작동