【问题标题】:Passing strings and vectors by value in c++ [closed]在 C++ 中按值传递字符串和向量 [关闭]
【发布时间】:2013-01-22 11:08:32
【问题描述】:

首先我知道这是一件很糟糕的事情,因为它可能需要大量的内存复制......但由于我已经给出了规范,所以需要这样做。

无论如何,我试图通过引用传递一个字符串,并通过值传递一个向量,因为它将被编辑然后销毁,然后将再次使用原始的。

这是函数原型

int print (const string& findme,const string& command,const string& command2,const string& command3, int n, vector<string> list)

这是我的电话,所有变量都是正确的类型。

print (special,command,command2,command3,n,temp);

当我不尝试调用该函数时,一切都会编译并运行......但是当我这样做时,我得到了这个错误:

a2p1.cc: In function âint main(int, char**)â:
a2p1.cc:85: error: no match for call to â(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >) (std::string&, std::string&, std::string&, std::string&, int&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)â

所以我想知道...我应该如何将这些字符串和向量传递给函数,这些字符串可以通过引用传递,但我需要向量的副本,任何语法帮助都会非常有帮助!谢谢!

这里是变量

    int n;

    string command = "rr";
    string command3 = "null";
    string command2 = "f";
    string japan;
    string special;
    special = "fnord";
    string textFileName;

    vector<string> list;
    vector<string> print;

这是对函数的调用,在第 83 行:

while (cin >> inputtemp)
{
...

if ((inputtemp == "p")){

print (special,command,command2,command3,n,list);

}// If
}// While

【问题讨论】:

  • 什么是..函数定义?
  • 你刚刚尝到了一点模板调试的好处。
  • 很抱歉,函数定义相当大,由于IP问题,我实际上无法发布。
  • a2p1.cc 的第 85 行到底是什么?
  • 错误消息似乎表明您实际上调用了temp(...) 而不是temp(...)。你确定不是这样吗?

标签: c++ vector reference copy-constructor stdstring


【解决方案1】:

你有一个函数和一个名为print的变量。

您实际上并没有调用print 函数。

重命名变量。

(你的函数原型是正确的。它会按照你的描述做)

再次查看您的错误以了解这一点。 (我删除了模板以使其更简单)

error: no match for call to (std::vector) (std::string&, std::string&, std::string&, std::string&, int&, std::vector&)

【讨论】:

    猜你喜欢
    • 2021-07-04
    • 2014-02-22
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2021-12-05
    • 2021-06-17
    相关资源
    最近更新 更多