Call method from class

Hello everyone! I'm new to the forums. Anytime I'm having a problem I google it and normally find it pretty quick in this forum, but this time I cannot, so I joined!

Here is all my code. My problem is at the bottom in the printInventory() call. The error I keep getting from dev c++ is:

72 Q:\USB Files\USB Files\Spring 2014\C++ Homework\Session 3 Project\main.cpp conversion from `inventory (*)()' to non-scalar type `std::string' requested

I'm about to start pulling my hair out. Any help would be appreciated! Thanks!

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>

using namespace std;

class inventory
{
      public:
             inventory();
             inventory(string);
             inventory(string, int, double);
             inventory(string, int, double, int);
             void printInventory() const;
             
      private:
              string name;
              int itemNum;
              double price;
              int unitsInStock;
};

inventory::inventory()              //default constructor
{
   name = "";
   itemNum = -1;
   price = 0.0;
   unitsInStock = 0;
}

inventory::inventory (string n)
{
   name = n;
   itemNum = -1;
   price = 0.0;
   unitsInStock = 0;
}

inventory::inventory (string n, int iNum, double cost)
{
   name = n;
   itemNum = iNum;
   price = cost;
   unitsInStock = 0;
}

inventory::inventory (string n, int iNum, double cost, int inStock)
{
   name = n;
   itemNum = iNum;
   price = cost;
   unitsInStock = inStock;
}


void printInventory (string n, int iNum, double cost, int inStock)
{    
     cout << "Item name is : " << n;
     cout << "Item number is : " << iNum;
     cout << "Item price is : " << cost;
     cout << "Items in stock : " << inStock;
}



int main()
{
    inventory item1();
    inventory item2("Dryer");
    inventory item3("Washer", 2345, 278.95);
    inventory item4("Toaster", 8231, 34.49, 200);
    
    printInventory (item1);
    printInventory (item2);
    printInventory (item3);
    printInventory (item4);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
When you call a function, the compiler will try to match the first arguement you pass to the function to the first parameter in the declaration of the function, the second arguement to the second parameter, etc. In your case, you are trying to call the function printInventory with the first parameter being an object of type "inventory".

The parameters for this function looks as follows:
void printInventory (string, int, double, int);

In the call you are making to the function, the compiler is trying to match the type "inventory" to the type "string". Since "inventory" does not inherit from string class, the match fails and you get the compiler error

EDIT:
But if you overload the ostream operator (<<) for your inventory class, as follows:

1
2
3
4
5
6
7
8
friend ostream& operator << (ostream &oss, const inventory& inv)
{
     oss << "Item name is : " << inv.name;
     oss << "Item number is : " << inv.itemNum;
     oss << "Item price is : " << inv.price;
     oss << "Items in stock : " << inv.unitsInStock;
     return oss;
}


This allows you to do something like:
1
2
3
4
cout << item1 << endl;
cout << item2 << endl;
cout << item3 << endl;
cout << item4 << endl;


Hope that helps

EDIT2:
Looking closely at your class methods, it seems that you have a printInventory method but you never implemented it. This is what it should look like after implementation:

1
2
3
4
5
6
7
void inventory::printInventory() const
{
     cout << "Item name is : " << this->name;
     cout << "Item number is : " << this->itemNum;
     cout << "Item price is : " << this->price;
     cout << "Items in stock : " << this->unitsInStock << '\n';
}


Then to use it, you can call it like so
1
2
3
4
item1.printInventory();
item2.printInventory();
item3.printInventory();
item4.printInventory();
Last edited on
So what do you suppose I do? Sorry, I'm pretty new to c++. Thanks for the prompt reply!


EDIT:
Sorry, I just saw your edits. Thanks again!
Last edited on
Topic archived. No new replies allowed.