Constructor Error

I have two files, main.cpp and GroceryItem.cpp. I created a class called GroceryItem and a constructor that takes 4 arguments with member initialization list. I call a function to display the output but I get an error saying "no function for call to GroceyItem::GroceryItem()'".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//main.cpp
#include <iostream>
#include "GroceryItem.cpp"

int main(){
  GroceryItem groceryItem;
  groceryItem.productName("coke");
  return 0;
}

//GroceryItem.cpp
#include <iostream>
#include <string>
class GroceryItem{
public:
  GroceryItem(const std::string productName, const std::string brandName, const std::string upc, const double price);

  void productName(const std::string &productName);

private:
  std::string productName_ = " ";
  std::string brandName_ = " ";
  std::string upc_ = " ";
  double price_ = 0.0;
};

GroceryItem::GroceryItem(const std::string productName, const std::string brandName, const std::string upc, const double price) : productName_(productName), brandName_(brandName), upc_(upc), price_(price){}

void GroceryItem::productName(const std::string &productName){
  std::cout << productName << std::endl;
}
Last edited on
Where is your main() function?

You really need to do something about those long lines.

1
2
3
4
5
6
7
8
9
10
...
  GroceryItem(const std::string productName, const std::string brandName, 
                       const std::string upc, const double price);

...
   GroceryItem::GroceryItem(const std::string productName, const std::string brandName, 
                                              const std::string upc, const double price) 
                                            : productName_(productName), brandName_(brandName), 
                                              upc_(upc), price_(price){}


It appears that you maybe are trying to create an instance of your class with the no-argument constructor that doesn't exist, but without the rest of the error message and the code that goes with it, I'm only guessing.

In future don't paraphrase the error messages, copy them exactly as they appear in your development environment (all of them). Those messages have important information embedded within them to aid in locating and fixing the problems.

Last edited on
GroceryItem groceryItem;
That line would call a GroceryItem constructor taking no arguments.

You haven't defined a GroceryItem constructor taking no arguments.

You have defined a GroceryItem constructor taking four arguments, and that will stop the compiler automatically supplying a zero-argument constructor.

Thanks guys, that makes sense.
#include "GroceryItem.cpp"

Recommended: NEVER include source files. Include headers that declare functions/classes you define in source files.

With this simple an example including source files is OK, but is a bad habit that can later bite you big time.

https://stackoverflow.com/questions/19547091/including-cpp-files
You just need to add the default constructor for GroceryItem:
1
2
...
GroceryItem::GroceryItem() {}


because
GroceryItem groceryItem;

calls a GroceryItem constructor taking no arguments.
Topic archived. No new replies allowed.