传递指针向量
passing vector of pointers
我在将向量作为参数传入函数时遇到问题。我收到以下错误:
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;
我在 while 循环中调用 main 中的函数。
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;
}
我已将您的问题提炼为最小的可重现示例。如果你要从你的程序中删除 所有 不必要的垃圾,你会得到这样的东西:
#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
,它至少引用了具有正确参数类型的正确函数。
我在将向量作为参数传入函数时遇到问题。我收到以下错误:
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;
我在 while 循环中调用 main 中的函数。
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;
}
我已将您的问题提炼为最小的可重现示例。如果你要从你的程序中删除 所有 不必要的垃圾,你会得到这样的东西:
#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
,它至少引用了具有正确参数类型的正确函数。