创建值并将值插入对象时出现问题

时间:2017-05-27 18:54:38

标签: c++ oop vector

我在这里要做的是创建对象并将它们存储在向量中,并通过获取每一行来通过文件为这些对象赋值。

例如: 我有一个值为name, classType, strength, endurance, and id

的Player类

然后通过text或cfg文件,我想为每个变量分配值,并从中创建一个对象,然后将它们推送到矢量中。

我的问题:

如何将文本文件中的值分配到对象中,然后将该对象推送到向量中?

问题:

我收到了大量错误信息,我将在下面提供。

我的首选结果是:

用于在文件中写入要创建到对象中的文本,并将该对象推入向量中。

以下是我在下面尝试的内容。

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

DEPENDENCIES.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;
}

承诺的错误

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

1 个答案:

答案 0 :(得分:0)

错误消息的关键部分是:

12/06/2017

如果我们阅读例如this std::istream_iterator reference我们会看到

  

error: no matching function for call to 'Player::Player()' 必须满足DefaultConstructible,[...]要求。

由于您的T课程没有默认构造函数,因此您无法与Player一起使用。

简单的解决方案当然是创建默认构造函数。它甚至可以像

一样简单
std::istream_iterator