【问题标题】:error: no matching function for call to ‘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> >&)’
【发布时间】:2019-07-24 08:29:13
【问题描述】:

我正在尝试从我的主管那里调试此代码,而且我是 C++ 新手。

我发现了一些类似的 no matching function for call to 问题,这让我知道了问题可能是什么但无法解决。

我在错误信息和相关功能下方列出了我的想法。

错误信息:

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 );

相关功能:

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

想法:

  • 如果有人能给我解释一下就好了:std::vector&lt;std::vector&lt;int&gt;&gt;::push_back(std::vector&lt;std::__cxx11::basic_string&lt;char&gt;&gt;&amp;)
  • 我知道std::vector&lt;std::vector&lt;int&gt;&gt; 是一个矩阵并且 push_back 在向量末尾添加一个元素。
  • 我怀疑我们正在尝试读取错误的类型 the_entries.push-back(row_as_vector)
  • 原始代码以csv 文件作为输入,包含integersstrings。现在我们正在读取具有以下形状的txt 文件:

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

    在上面,我们删除了89 行中的第一行。

  • 代码是否可能需要进行更多更改才能 阅读这个txt 文件而不是csv 文件?

错误信息的其余部分:

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>&&}’

完整代码:

// 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;
}

【问题讨论】:

  • the_entriesvector&lt;vector&lt;int&gt;&gt;,而您正在尝试 push_back vector&lt;string&gt;。那是string 的向量,而不是int。这和错误信息说的一模一样,也许它叫std::stringstd::__cxx11::basic_string&lt;char&gt;让你感到困惑。
  • 您试图将row_as_vector(字符串向量)放入the_entries,这是整数向量的向量。因此你的错误。
  • @Blaze 没错,我不知道std::__cxx11::basic_string&lt;char&gt; 只是std::string

标签: c++ c++11 data-structures stdvector function-call


【解决方案1】:

我怀疑我们正在尝试读取错误的类型 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 );

是的,你说得对。错误消息表明您正在尝试将 字符串向量 推回 整数向量向量。它们是完全不同的类型,因此不可能。

您可能希望将the_entries 的类型更改为

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

使用std::stoi 将字符串转换为整数,同时推回std::vector&lt;int&gt; 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 );

是否有可能代码必须进行更多更改才能阅读 这个txt 文件而不是csv 文件?

如果这两者的内容相同,则无需对代码进行太大改动。但是,; 应该是 parsed out,然后再调用 std::stoi 给他们。因为它可能通过 invalid_argument 异常为坏参数。


一些建议:

【讨论】:

  • 非常感谢。但是,之前我们使用了除csv 文件中的第一行之外的所有内容。现在我假设我们只对txt 文件中的integers 感兴趣,而不是分号。我们不需要先删除那些,或者告诉代码忽略那些吗?
  • @rlamesch 是的,您需要先parse the semicolens,然后再将其转换为int
  • 现在代码可以正常编译了,但是还是有一些问题。首先,if (argc &lt; 2 || argc &gt; 2) 似乎有问题。它会抛出“Usage: ./a.o ”错误消息。但是,两者都是严格的不等式,因此代码应该可以使用 2 个参数。在原始代码中它显示为 if (argc &lt; 2 || argc &gt; 3) ,我将其改回。
  • " 还有一些问题。":看起来你现在有不同的问题(除了上面提到的)。因此,我建议就您的新(其他)问题提出一个新问题。
猜你喜欢
  • 2018-12-05
  • 1970-01-01
  • 2022-01-19
  • 2021-05-05
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多