::c_str': non-standard syntax; use -6ren">
gpt4 book ai didi

c++ - 为什么会出现 "Non-standard syntax; use ' &' to create pointer to member"和 "no overloaded function takes 2 arguments"错误?

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:14 27 4
gpt4 key购买 nike

我的代码中出现了这两个错误:

Error   C3867   'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str': non-standard syntax; use '&' to create a pointer to member 59 
Error C2661 'Product::Product': no overloaded function takes 2 arguments 59

似乎当我试图调用我的非默认构造函数时它只得到 2 个参数,即使我试图传递给它 4 个。这只是猜测,但我怀疑我可能需要添加一些 NULL 检查器或者其他的东西?但我不明白我传递给它的任何论点怎么可能是 NULL,所以我被卡住了。

这是我的非默认构造函数的声明和定义:

Product(bool restocking, string name, int quantity, double price); //Declaration
Product::Product(bool restocking, string name, int quantity, double price):InventoryItem(restocking), quantity_(quantity), price_(price) { } //Definition

产品派生自 InventoryItem

这是麻烦的代码:

void InventorySystem::BuildInventory(void) {
int i{ 0 };
string name_buffer;
string quantity_buffer;
string price_buffer;
ifstream fin("in_inventory.txt");
if (fin) {
while (getline(fin, name_buffer, ';') && i < g_kMaxArray) {
getline(fin, quantity_buffer, ';');
getline(fin, price_buffer, '\n');
p_item_list_[i] = new Product(false, name_buffer, atoi(quantity_buffer.c_str), atof(price_buffer.c_str)); \\ Error on this line
i++;
item_count_++;
}
}
else {
cout << "Error: Failed to open input file." << endl;
}
fin.close();
}

最佳答案

cstr() 是一个函数,因此请确保调用它来获取结果(而不是将其视为成员变量)

         p_item_list_[i] =  new Product(false, name_buffer, atoi(quantity_buffer.c_str()), atof(price_buffer.c_str()));

关于c++ - 为什么会出现 "Non-standard syntax; use ' &' to create pointer to member"和 "no overloaded function takes 2 arguments"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58584393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com