【问题标题】:passing vector of pointers传递指针向量
【发布时间】:2022-01-12 05:06:40
【问题描述】:

我的向量作为参数传入函数时遇到问题。我收到以下错误:

void checkout(std::vector<InvoiceItem,std::allocator<InvoiceItem>>)': cannot convert argument 1 from 'std::vector<InvoiceItem *,std::allocator<InvoiceItem *>>' to 'std::vector<InvoiceItem,std::allocator<InvoiceItem>>'   classwork15 C:\Users\dhuan\source\repos\classwork15\classwork15\main.cpp

我调用了向量

vector<InvoiceItem*> order;

我在我的 main 中调用函数,在一个 while 循环中。

    while (choice <= 4 && again == 'y')
    {
        if (choice == 1)
        {
            invoice = addToCart();
            cart.append(invoice);
            InvoiceItem* ptr = new InvoiceItem(invoice);
            order.push_back(ptr);
        }

        else if (choice == 2)
        {
            cart.display();
        }
        else if (choice == 3)
        {
            checkout(order); // <-here
        }
        cout << "1: add to order, 2: view cart, 3: checkout" << endl;
        cout << "Your choice: " << endl;
        cin >> choice;
        cout << endl;
    }

这是函数,如果有帮助的话:

void checkout(vector<InvoiceItem*> order)
{
    string name;
    char again = 'y';
    int orderNum = 1000;
    double total;
    cout << "Checking out" << endl;
    cout << "Enter name: ";
    cin >> name;
    cout << endl;
    cout << "INVOICE" << endl;
    cout << "Order Number: " << orderNum++ << endl;
    cout << "Customer: " << name << endl;
    cout << endl;
    cout << "QTY \tDescription \t\tEach \tSubtotal" << endl;
    for (int i = 0; i < order.size(); i++)
    {
        cout << i + 1 << "\t" << order[i]->getDescription() << "\t\t" << order[i]->getPrice() << "\t" << order[i]->getTotal() << endl;
        total += order[i]->getTotal();
    }

    cout << "Total Due: ";
    cin >> total;
    cout << endl;
}

【问题讨论】:

  • 你从哪里调用这个函数?你传递给它什么类型的值?请发帖minimal reproducible example
  • 您的函数 definition 采用 vector&lt;InvoiceItem*&gt;,但错误声称该函数需要 vector&lt;InvoiceItem&gt;,这意味着您的函数 声明是错误的,需要修复。
  • 错误消息显示您正在尝试将vector&lt;InvoiceItem*&gt; 分配给vector&lt;InvoiceItem&gt;。事实上,它明确表示在main.cpp(您省略了行号)中声明了void checkout(vector&lt;InvoiceItem&gt;)——这与您问题中的定义不符。
  • 对于这种情况,std::vector 可能是你需要的。阅读isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines,了解如何编写现代 C++(特别是 P.2。尽量避免新建/删除)

标签: c++ vector


【解决方案1】:

我已将您的问题提炼成一个可重复的最小示例。如果你要从你的程序中删除所有不必要的垃圾,你会得到这样的东西:

#include <vector>

using std::vector;

class InvoiceItem {};

// (A)
void checkout(vector<InvoiceItem> order);

int main()
{
    vector<InvoiceItem*> order;
    checkout(order);               //<-- error occurs here
}

// (B)
void checkout(vector<InvoiceItem*> order)
{
}

确实,这会产生相同的编译器错误。

问题在于 main() 知道的函数声明包含错误。因此,在编译器到达您的函数定义之前,它会解析 main 并立即出现类型不匹配。

错误消息不仅应该告诉你发生在哪一行,还应该引导你到它首先获得声明的那一行。

快进到 (B) 处的函数定义。嗯,这与main 所知道的完全不同。因此,它被称为 overload——它是一个不同的函数,恰好与 (A) 同名。

void checkout(vector<InvoiceItem> order);

因此,如果现在不明显,应使 (A) 处的函数签名与 (B) 处的函数签名相匹配。这样,它引用了main 知道的 same 函数。因此,即使在编译器解析 main 时还没有定义函数,它至少引用了具有正确参数类型的正确函数。

【讨论】:

    猜你喜欢
    • 2011-04-20
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    相关资源
    最近更新 更多