对象矢量c++
Object Vector C++
我正在努力学习更多关于向量和存储对象。我正在从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++....你需要修复的东西…
- 删除。h文件中没有定义的构造函数
- 给出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时,这个问题很常见。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 使用"std::unordereded_map"映射到"std::list"对象