我在这里尝试做的是创建对象并将它们存储在 vector 中,并通过获取每一行通过文件为这些对象分配值。
例如:
我有一个 Player 类,其值为 name、classType、strength、endurance 和 id
然后通过文本或 cfg 文件,我想为这些变量中的每一个赋值并从中创建一个对象,然后将它们放入一个 vector 中。
我的问题:
如何将文本文件中的值赋给对象,然后将该对象放入 vector 中?
问题:
我收到了一大堆错误消息,我将在下面显示这些消息。
我希望的结果是什么:
将文件中写入的文本创建为一个对象,并将该对象插入一个 vector 。
这是我在下面尝试过的。
播放器.txt
example1 Scribe 3 6 1
example2 Cursed 3 3 2
example3 Thief 3 1 3
example4 Paladin 9 5 4
example5 Hobo 3 7 5
依赖关系.h
#ifndef DEPENDENCIES_H
#define DEPENDENCIES_H
std::istream& operator>>(std::istream& str, Player& player) {
std::string line;
if (std::getline(str, line)) {
std::stringstream lineStream(line);
std::string name;
std::string classType;
int strength;
int endurance;
int id;
if (lineStream >> name >> classType >> strength >> endurance >> id) {
// Set up the player object.
player = Player(name, classType, strength, endurance, id);
}
else {
// Error when reading player characteristics from line.
// Need to set the errror state of the stream.
str.setstate(std::ios::failbit);
}
}
return str;
}
std::vector<Player> getPlayers() {
std::ifstream playerFile("Player.txt");
std::istream_iterator<Player> playerIter(playerFile);
std::istream_iterator<Player> end;
return std::vector<Player>(playerIter, end);
}
#endif
main.cpp
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <vector>
#include <iterator>
#include <sstream>
#include <fstream>
#include "DEPENDENCIES.h"
int main() {
std::vector<Player> allPlayers(getPlayers());
//Do whatever with the vector here
return 0;
}
promise 的错误
In file included from /usr/local/include/c++/7.1.0/iterator:66:0,
from main.cpp:18:
/usr/local/include/c++/7.1.0/bits/stream_iterator.h: In instantiation of 'constexpr std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator() [with _Tp = Player; _CharT = char; _Traits = std::char_traits<char>; _Dist = long int]':
DEPENDENCIES.h:79:35: required from here
/usr/local/include/c++/7.1.0/bits/stream_iterator.h:65:46: error: no matching function for call to 'Player::Player()'
: _M_stream(0), _M_value(), _M_ok(false) {}
^
In file included from main.cpp:21:0:
DEPENDENCIES.h:23:3: note: candidate: Player::Player(std::__cxx11::string, std::__cxx11::string, int, int, int)
Player(std::string inpName, std::string inpClassType, int inpStrength, int inpEndurance, int inpId) {
^~~~~~
DEPENDENCIES.h:23:3: note: candidate expects 5 arguments, 0 provided
DEPENDENCIES.h:11:7: note: candidate: Player::Player(const Player&)
class Player {
^~~~~~
DEPENDENCIES.h:11:7: note: candidate expects 1 argument, 0 provided
DEPENDENCIES.h:11:7: note: candidate: Player::Player(Player&&)
DEPENDENCIES.h:11:7: note: candidate expects 1 argument, 0 provided
In file included from /usr/local/include/c++/7.1.0/iterator:66:0,
from main.cpp:18:
/usr/local/include/c++/7.1.0/bits/stream_iterator.h: In instantiation of 'std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type&) [with _Tp = Player; _CharT = char; _Traits = std::char_traits<char>; _Dist = long int; std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type = std::basic_istream<char>]':
DEPENDENCIES.h:78:56: required from here
/usr/local/include/c++/7.1.0/bits/stream_iterator.h:69:40: error: no matching function for call to 'Player::Player()'
: _M_stream(std::__addressof(__s))
^
In file included from main.cpp:21:0:
DEPENDENCIES.h:23:3: note: candidate: Player::Player(std::__cxx11::string, std::__cxx11::string, int, int, int)
Player(std::string inpName, std::string inpClassType, int inpStrength, int inpEndurance, int inpId) {
^~~~~~
DEPENDENCIES.h:23:3: note: candidate expects 5 arguments, 0 provided
DEPENDENCIES.h:11:7: note: candidate: Player::Player(const Player&)
class Player {
^~~~~~
DEPENDENCIES.h:11:7: note: candidate expects 1 argument, 0 provided
DEPENDENCIES.h:11:7: note: candidate: Player::Player(Player&&)
DEPENDENCIES.h:11:7: note: candidate expects 1 argument, 0 provided
exit status 1
最佳答案
错误消息的关键部分是:
error: no matching function for call to 'Player::Player()'
如果我们阅读例如this std::istream_iterator
reference我们会看到
T
must meet theDefaultConstructible
, [...] requirements.
由于您的 Player
类没有默认构造函数,您不能将它与 std::istream_iterator
一起使用。
简单的解决方案当然是创建一个默认构造函数。它甚至可以像这样简单
Player() = default;
关于c++ - 创建值并将值插入对象的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220560/