对象矢量c++

Object Vector C++

本文关键字:c++ 对象      更新时间:2023-10-16

我正在努力学习更多关于向量和存储对象。我正在从txt文件中读取数据。我不知道我犯了什么错误,它不起作用。

下面是我的主要方法

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::Food(string newName, int newCalories, int newCost) {
    this->name = newName;
    this->calories = newCalories;
    this->cost = newCost;
}

在我的main.cpp文件中,我只是创建对象Reader(现在没有构造函数)并调用方法。

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

我想用对象填充Vector,这些对象从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 */

我刚刚编译了你的代码,它对我来说很好....我用了g++....你需要修复的东西…

  1. 删除。h文件中没有定义的构造函数
  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

reader.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

我猜你的exe运行与你期望的不同的相对路径。在调用file.open()后检查file.good()是否为真。当编程IDE(例如Visual Studio)在不同的工作文件夹下运行exe时,这个问题很常见。