1

I'm getting these two errors in my code:

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 

It seems like when I'm trying to call my non-default constructor it's only getting 2 arguments even though I'm trying to pass it 4. This is just speculation, but I suspect maybe I need to add some NULL checkers or something? but I don't see how any of the arguments I'm passing it could be NULL so I'm stuck.

Here's my declaration and definition for my non-default constructor:

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

Product is a derived from InventoryItem

Here's the troublesome piece of code:

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();
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
t_dog473
  • 27
  • 3

3 Answers3

0

cstr() is a function, so make sure you call it to get the result (rather than treating as a member variable)

         p_item_list_[i] =  new Product(false, name_buffer, atoi(quantity_buffer.c_str()), atof(price_buffer.c_str()));
robthebloke
  • 9,331
  • 9
  • 12
0

Use empty parentheses to call a member function with no parameters:

... atoi(quantity_buffer.c_str()) ...

If the compiler sees c_str without parentheses, it makes a very unreasonable assumption that you want to refer to the function itself, using a pointer to it. This is a rarely-used feature.

To complicate the matters even more, there are two possible syntaxes for pointer to member function, and one of them is non-standard. This is what the compiler complains about. You don't need any of this, so add parentheses to tell the compiler that you want to call the function and not take a pointer to it.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
0

The pair () is the function call operator. Without it you only get the function pointer, no calls are made

But don't use atoi(). See the reason why it should be avoided. Use stoi() instead, and use stod() to get a double

p_item_list_[i] = new Product(false, name_buffer,
                              stoi(quantity_buffer), stod(price_buffer));

As you can see, the code is much cleaner because there's no .c_str() everywhere, as the sto* family receives std::string directly (which is much better than receiving a const char*)

Another note: don't use such a long lines. No one likes horizontal scrolling

phuclv
  • 37,963
  • 15
  • 156
  • 475