【问题标题】:'<function-style-cast>': cannot convert from 'initializer list' to 'Robot'“<function-style-cast>”:无法从“初始化列表”转换为“机器人”
【发布时间】:2020-12-11 18:03:02
【问题描述】:

我在代码的第 58 行有一个错误:Robot robot = robot(seglist[0]...) 我尝试了不同的方法,但没有任何效果。我需要将 txt 文件中的数字放入向量中,然后分配 robotsNum、RobotTeam、robotPosX 和 robotPosY 的值。 希望有人可以帮助我。

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <vector>

struct Robot {
    std::string robotNum;
    std::string robotTeam;
    std::string robotPosX;
    std::string robotPosY;
    public:
        Robot(string robotNum = "empty", string robotTeam = "empty", string robotPosX = "empty", string robotPosY = "empty") {
            this->robotNum = robotNum;
            this->robotTeam = robotTeam;
            this->robotPosX = robotPosX;
            this->robotPosY = robotPosY;
}

};

using namespace std;

string* sortArray(string* myArray) {
    
    //cout << "pingas";
    //cout << myArray[6];
    return myArray;
}

int main()
{
    
    ifstream myfile;
    myfile.open("start.txt");
    string line;
    std::vector<Robot> myArray;

    while (getline(myfile, line))
    {
        std::istringstream iss(line);
        string line;

        std::string segment;
        std::vector<std::string> seglist;

        while (std::getline(iss, segment, ','))
        {
            seglist.push_back(segment);
        }
        
        
        Robot robot = Robot(seglist[0], seglist[1], seglist[2], seglist[3]);

        myArray.push_back(robot);
        cout << "\n";
    }


    if (myfile.is_open()) {
        string myArray[2];
        for (int i = 0; i < 1; i++)
        {
            myfile >> myArray[i];
        }
        //string* arraySorted = sortArray(myArray);
        sort(myArray->begin(), myArray->end(), greater<int>());

        //for (auto& elem : myArray)
        //  cout << elem << " - " << endl;

        
    }
    //cout << myArray[];
    myfile.close();
    return 0;

}

【问题讨论】:

  • Robot robot(seglist[0]... 甚至删除整个中间对象并执行myArray.emplace_back(seglist[0]...
  • 您正在组合 ...您认为这是个好主意吗?

标签: c++


【解决方案1】:

在结构定义中应该有

std:: 命名空间

Robot 构造函数中的字符串:

public:
        Robot(std::string robotNum = "empty", std::string robotTeam = "empty", std::string robotPosX = "empty", std::string robotPosY = "empty") {

之后代码对我有用。

【讨论】:

  • 大声笑。他在定义 Robot 之后有一个using namespace std;。所以在此之前他使用的是 cstring。
猜你喜欢
  • 1970-01-01
  • 2011-04-22
  • 2019-08-21
  • 1970-01-01
  • 2015-10-10
  • 2018-06-02
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
相关资源
最近更新 更多