简体   繁体   English

错误:没有匹配的函数调用&#39;std :: vector <std::vector<int> &gt; ::的push_back(标准::矢量 <std::__cxx11::basic_string<char> &gt;&)”

[英]error: no matching function for call to ‘std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char> >&)’

I am trying to debug this code from my supervisor and I'm new to C++. 我正在尝试从我的主管调试此代码,而我是C ++的新手。

I found a few similar no matching function for call to questions, which gave me ideas what the problem might be but was not able to solve it. 我发现了一些类似的no matching function for call to问题,这使我知道了问题可能是什么,但无法解决。

I have listed my thoughts below the error message and relevant function. 我在错误消息和相关功能下面列出了我的想法。

Error message: 错误信息:

In function ‘int main(int, char**)’:
distanceMatrixToSageGraph.c:104:43: error: no matching function for call to 
‘std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char> >&)’     
the_entries.push_back( row_as_vector );

Relevant function: 相关功能:

int main(int argc, char** threshold_and_distanceMatrixfilename)
{
    if (argc < 2 || argc > 2) 
    {
        std::cerr << "Usage: ./distanceMatrixToSageGraph.o <threshold> 

            <distanceMatrix_file_calculated_fromDGEsingleCell_data>" << std::endl;
            return -1;
    }
    string distanceMatrixfilename = threshold_and_distanceMatrixfilename[2];
    int threshold = std::stoi(threshold_and_distanceMatrixfilename[1]);
    std::ifstream distanceMatrixFile(distanceMatrixfilename);

    if (!distanceMatrixFile)
    {
        std::cerr << "Error opening distanceMatrix file: " << distanceMatrixfilename << std::endl;
        return -1;
    }
    string row;
    std::getline(distanceMatrixFile, row); // discard the first row, which specifies the format of the file.
    vector<vector<int>> the_entries;

    while (std::getline(distanceMatrixFile, row))
    {
        std::stringstream row_as_stringstream(row);
        int i; i = 0;
        vector<string> row_as_vector;

        while (row_as_stringstream.good())
        {
            string substr;
            getline(row_as_stringstream, substr, ',');
            row_as_vector.push_back(substr);
        };
        the_entries.push_back(row_as_vector); //LINE 104
    };
}

Ideas: 思路:

  • It would be great if someone could explain to me: std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char>>&) 如果有人可以向我解释会很好: std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char>>&)
  • I understand that std::vector<std::vector<int>> is a matrix and that push_back adds an element at the end of the vector. 我知道std::vector<std::vector<int>>是一个矩阵,而push_back在向量的末尾添加了一个元素。
  • I suspect that we're trying to read in the wrong type in the_entries.push-back(row_as_vector) . 我怀疑我们试图在the_entries.push-back(row_as_vector)读取错误的类型。
  • The original code took a csv file as input, containing integers and strings . 原始代码将一个csv文件作为输入,其中包含integersstrings Now we're reading in a txt file having the shape: 现在,我们正在读取具有以下形状的txt文件:

    TEXT 0; INT; INT; INT; INT; ... 0; INT; INT; INT; INT; ... 18 more lines of the above numbers and semicolons

    In the above, we remove the first row in line 89 . 在上面,我们删除了89行中的第一行。

  • Is it possible the code has to be changed much more to be able to read this txt file instead of the csv file? 是否可能需要对代码进行更多更改才能读取此txt文件而不是csv文件?

Rest of error message: 其余错误消息:

In file included from /usr/include/c++/6/vector:64:0,
                 from distanceMatrixToSageGraph.c:13:
/usr/include/c++/6/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; std::vector<_Tp, _Alloc>::value_type = std::vector<int>]
       push_back(const value_type& __x)
       ^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:914:7: note:   no known conversion for argument 1 from ‘std::vector<std::__cxx11::basic_string<char> >’ to ‘const value_type& {aka const std::vector<int>&}’
/usr/include/c++/6/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; std::vector<_Tp, _Alloc>::value_type = std::vector<int>]
       push_back(value_type&& __x)
       ^~~~~~~~~
/usr/include/c++/6/bits/stl_vector.h:932:7: note:   no known conversion for argument 1 from ‘std::vector<std::__cxx11::basic_string<char> >’ to ‘std::vector<std::vector<int> >::value_type&& {aka std::vector<int>&&}’

Whole code: 整个代码:

// Convert distanceMatrix tables of protein interactions to SAGE graph.
///////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <list>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;

void writeGraphInSageFormat(string name, std::vector<std::vector<int>> TheEdges) 
{
    //////////////////////////////////////////////////////////////////////////////////////
    // Write out the edges in SAGE format.
    ///////////////////////////////////////////////////////////////////////////////////////
    int edgeNumber = TheEdges.size();
    ofstream d1sageFile(name, ios::out);
    d1sageFile << "g = Graph([" << endl;

    for (int n = 0; n < edgeNumber; n++) {
        d1sageFile << "(" << TheEdges[n][0] + 1 << "," << TheEdges[n][1] + 1 << ")," << endl;
    }
    d1sageFile << "])" << endl;
    d1sageFile << "g.show()" << endl;
    d1sageFile.close();
    std::cout << "SAGE graph written into the file " << name << std::endl;
}

std::vector<std::vector<int>> ConvertEntriesMatrixToEdges(vector<vector<int>> the_entries, int threshold) 
{
    ////////////////////////////////////////////////////////////////////////////////////////////
    // Construct the edge-vertex incidence matrix (d_1) from the distanceMatrix entries matrix:
    ////////////////////////////////////////////////////////////////////////////////////////////
    std::vector<std::string> proteinNames;
    std::vector<std::vector<int>> TheEdges;
    std::cout << "Registering only edges shorter than " << threshold << "." << std::endl;
    int thisDistance;
    for (int i = 0; i < the_entries.size(); i++)
    {
        for (int j = i + 1; j < the_entries.size(); j++)
        {
            // we could use the_entries.size() instead of the_entries.at(i).size(), because this is a square matrix.
            thisDistance = the_entries.at(i).at(j);
            if (thisDistance < threshold) 
            {
                std::vector<int> CurrentEdge(2);
                CurrentEdge[0] = i;
                CurrentEdge[1] = j;
                TheEdges.push_back(CurrentEdge);
            };
        };
    };
    return TheEdges;
}

///////////////////////////////////////////
// Main Program: Extract edges from a distanceMatrix file.
///////////////////////////////////////////
int main(int argc, char** threshold_and_distanceMatrixfilename)
{
    if (argc < 2 || argc > 2)
    {
        std::cerr << "Usage: ./distanceMatrixToSageGraph.o <threshold> <distanceMatrix_file_calculated_fromDGEsingleCell_data>" << std::endl;
        return -1;
    }
    string distanceMatrixfilename = threshold_and_distanceMatrixfilename[2];
    int threshold = std::stoi(threshold_and_distanceMatrixfilename[1]);
    std::ifstream distanceMatrixFile(distanceMatrixfilename);
    if (!distanceMatrixFile) {
        std::cerr << "Error opening distanceMatrix file: " << distanceMatrixfilename << std::endl;
        return -1;
    }
    string row;  //LINE 88
    std::getline(distanceMatrixFile, row); // discard the first row, which specifies the format of the file.
    vector<vector<int>> the_entries;

    while (std::getline(distanceMatrixFile, row))
    {
        std::stringstream row_as_stringstream(row);
        int i; i = 0;
        vector<string> row_as_vector;
        while (row_as_stringstream.good())
        {
            string substr;
            getline(row_as_stringstream, substr, ',');
            row_as_vector.push_back(substr);
        };
        the_entries.push_back(row_as_vector); //LINE 104
    };
    ////////////////////////////////////////////////////////////
    // Now we assemble the entries to an edges matrix, and write it into a Sage file:
    ////////////////////////////////////////////////////////////
    std::vector<std::vector<int>> TheEdges = ConvertEntriesMatrixToEdges(the_entries, threshold);    
    char outputFilename[60]; strcpy(outputFilename, distanceMatrixfilename.c_str()); strcat(outputFilename, "AtThreshold"); string thrshld = std::to_string(threshold); strcat(outputFilename, thrshld.c_str()); strcat(outputFilename, ".txt");
    writeGraphInSageFormat(outputFilename, TheEdges);
    return 0;
}

I suspect that we're trying to read in the wrong type in the_entries.push-back(row_as_vector) 我怀疑我们正在尝试在the_entries.push-back(row_as_vector)中读取错误的类型

error: no matching function for call to 
‘std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char> >&)’     
the_entries.push_back( row_as_vector );

Yes , your right about this. 是的 ,您对此有权。 The error message says that you are trying to push back a vector of strings to a vector of vector of integers . 错误消息表明您正在尝试将字符串向量推回整数向量向量 They are entirely different types and hence not possible. 它们是完全不同的类型,因此不可能。

You probably want to either change the type of the_entries to 您可能想要将the_entries的类型the_entries

std::vector<std::vector<std::string>> the_entries;
//                      ^^^^^^^^^^^^

or 要么

convert the strings to integers using std::stoi while pushing back to the std::vector<int> row_as_vector . 使用std::stoi将字符串转换为整数,同时推回std::vector<int> row_as_vector

std::vector<int> row_as_vector;
while(row_as_stringstream.good())
{
        // ......
        row_as_vector.push_back(std::stoi(substr));
        //                     ^^^^^^^^^^^^^^^^^^^
};
the_entries.push_back( row_as_vector );

Is it possible the code has to be changed much more to be able to read this txt file instead of the csv file? 是否可能需要对代码进行更多更改才能读取此txt文件而不是csv文件?

If the contents of those two are same, you don't need to make much difference in your code. 如果这两个内容相同,则无需在代码中做太多改变。 However, ; 但是; should be parsed out before calling std::stoi to them. 应该在调用std::stoi之前解析它们。 Because it may through invalid_argument exception for the bad argument. 因为它可能通过invalid_argument异常为错误的参数。


A few suggestions: 一些建议:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:没有匹配函数来调用&#39;std :: vector <std::__cxx11::basic_string<char> &gt; ::的push_back(INT&)” - error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’ 错误:无法从“向量”转换“标签” <std::vector<std::__cxx11::basic_string<char> &gt;&gt;' 到 ' 向量<std::__cxx11::basic_string<char> &gt;' </std::__cxx11::basic_string<char></std::vector<std::__cxx11::basic_string<char> - error: could not convert 'tab' from 'vector<std::vector<std::__cxx11::basic_string<char> >>' to 'vector<std::__cxx11::basic_string<char>>' 错误:没有匹配的函数调用&#39;std :: map <std::__cxx11::basic_string<char> - error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char> 填充向量的向量时出错:没有匹配的函数来调用std :: basic_string :: push_back - Error populating a vector of vectors: no matching function to call for std::basic_string::push_back 错误:没有匹配的函数调用&#39;std::__cxx11::basic_string<char> ::basic_string(int&amp;)&#39; - error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(int&)’ C++ 错误:没有匹配的函数调用&#39;std::__cxx11::basic_string<char> ::附加<int> (int, int)&#39; - c++ error: no matching function for call to ‘std::__cxx11::basic_string<char>::append<int>(int, int)’ 第 5 行:字符 54:错误:没有匹配的 function 调用 'min(int, std::__cxx11::basic_string<char> ::尺码类型)'</char> - Line 5: Char 54: error: no matching function for call to 'min(int, std::__cxx11::basic_string<char>::size_type)' 没有匹配的函数调用&#39;std::vector &gt;::push_back(char&amp;)&#39; - no matching function for call to 'std::vector >::push_back(char&)' 没有匹配的 function 调用 'std::vector <std::vector<int> &gt;::push_back(int)' </std::vector<int> - no matching function for call to 'std::vector<std::vector<int> >::push_back(int)' 没有用于调用std :: vector的匹配函数 <std::tuple> 推回 - no matching function for call to std::vector<std::tuple> push_back
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM