I'm attempting to write a program that will allow me to buy and sell items using LIFO and FIFO depending on the user input. Buying works well enough (though the compiler says there is an invalid conversion from Widget* to int), but it refuses to recognize any of the declared objects in the Sell function.
#include "InventoryManager.h"
#include <iostream>
#include <iomanip>
int main()
{
int num = 1;
Widget* widget = new Widget(10);
InventoryManager* deque = new InventoryManager(1);
deque->buyWidgets(widget, num);
delete widget;
delete deque;
return 0;
}
Error message:
In file included from InventoryManager.cpp:1:0:
InventoryManager.h: In member function 'void InventoryManager::buyWidgets(double, int)':
InventoryManager.h:51:26: error: invalid conversion from 'int' to 'Widget*' [-fpermissive]
stack->push(num_to_buy);
^
In file included from InventoryManager.h:4:0,
from InventoryManager.cpp:1:
StackDeque.h:71:6: error: initializing argument 1 of 'void StackDeque<T>::push(T*) [with T = Widget]' [-fpermissive]
void StackDeque<T>::push(T* item)
^
In file included from InventoryManager.cpp:1:0:
InventoryManager.h:59:34: error: invalid conversion from 'int' to 'Widget*' [-fpermissive]
queue->enqueueDeque(num_to_buy);
^
In file included from InventoryManager.h:5:0,
from InventoryManager.cpp:1:
QueueDeque.h:94:6: error: initializing argument 1 of 'void QueueDeque<T>::enqueueDeque(T*) [with T = Widget]' [-fpermissive]
void QueueDeque<T>::enqueueDeque(T* item)
^
In file included from InventoryManager.cpp:1:0:
InventoryManager.h: In function 'double getTotalProfit()':
InventoryManager.h:67:9: error: 'profit' was not declared in this scope
return profit;
^
InventoryManager.h: In function 'double sellWidgets(double, int)':
InventoryManager.h:72:5: error: 'inv_choice' was not declared in this scope
if(inv_choice == 1)
^
InventoryManager.h:76:4: error: 'stack' was not declared in this scope
stack->pop();
^
InventoryManager.h:77:4: error: 'sell' was not declared in this scope
sell = (price * num_to_sell);
^
InventoryManager.h:84:4: error: 'queue' was not declared in this scope
queue->dequeueDeque();
^
InventoryManager.h:85:4: error: 'sell' was not declared in this scope
sell = (price * num_to_sell);
^
InventoryManager.h:91:9: error: 'sell' was not declared in this scope
return sell;
^
Line 51: stack is a stack of Widgets (line 18). You can't push an int when a Widget is expected.
Line 59: Ditto for queue.
Line 65: getTotalProfit is not qualified by the class name.
Line 70: Ditto for sellWidgets.
BTW, you should NOT include your function implementations in your .h file. They belong in a .cpp file. Doing so will cause linker errors, should you include inventorymanager.h in more than one place.
InventoryManager.o:InventoryManager.cpp:(.text+0x2e): undefined reference to `Widget::Widget(double)'
InventoryManager.o:InventoryManager.cpp:(.text+0x52): undefined reference to `Widget::Widget(double)'
InventoryManager.o:InventoryManager.cpp:(.text+0x273): undefined reference to `Widget::Widget(double)'
InventoryManager.o:InventoryManager.cpp:(.text+0x3c9): undefined reference to `Widget::Widget(double)'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: InventoryManager.o: bad reloc address 0x2c in section `.text$_ZN10StackDequeI6WidgetEC1Ev[__ZN10StackDequeI6WidgetEC1Ev]'
collect2.exe: error: ld returned 1 exit status
'InventoryManager.exe' is not recognized as an internal or external command,
operable program or batch file.
You didn't post your current .h file, so I have to assume you're using the same class declaration as before.
Line 24: Implements a constructor that takes an int.
The linker errors say that the linker is expecting a constructor that takes a double. I see no constructor implemented in your new main that takes a double.
Thanks for your help. I have one last question. After successfully testing it in IM.cpp, I tried moving it to InventoryDriver.cpp to finish the program. However, it says that I have multiple definitions of main. I checked my includes, and I don't have any .cpp files, and I deleted the main() that was in my IM.cpp, so I'm not sure what the problem could be.
InventoryManager.o:InventoryManager.cpp:(.text+0x0): multiple definition of `main'
InventoryDriver.o:InventoryDriver.cpp:(.text+0x468): first defined here
collect2.exe: error: ld returned 1 exit status
IM.cpp is the same as it was before, just without the main function.
Sorry to keep bothering you with this, but I have one final problem, and it involves the main point of my program. It runs fine, but it refuses to keep the total profit. No matter what I input, it always outputs $0.00 as the total profit.
Alright, I've made some major changes to my buy and sell functions, including deleting junk data, and using another file that my professor prefers we use as opposed to cin and cout statements. But it still continues to output nothing but 0.00, no matter what I try.
//DO THIS
//sell Widgets and return the profit (check invalid input and reprompt if necessary)
double sellWidgets(InventoryManager* im)
{
Keyboard* kb = Keyboard::getKeyboard();
int num_to_sell = kb->readInt("How many widgets would you like to sell? ");
double price = kb->readDouble("What is the selling price for each widget? ");
int inventory_choice;
InventoryManager* deque = new InventoryManager(inventory_choice);
double sell = 0;
deque->sellWidgets(price, num_to_sell);
cout << setprecision(2) << sell;
delete deque;
return sell;
}
class Keyboard
{
private:
Keyboard();
public:
virtual ~Keyboard();
static Keyboard* getKeyboard();
//pre: the string (character literal) that will prompt the user for input
//post: the input read from the keyboard interpreted as an int is returned
int readInt(string prompt);
int getValidatedInt(string prompt, int min, int max);
//pre: the string that will prompt the user for input
//post: the input read from the keyboard interpreted as a double is returned
double readDouble(string prompt);
double getValidatedDouble(string prompt, double min, double max);
Example output:
Are you using (1) LIFO or (2) FIFO inventory management? 1
1
1. Buy Widgets
2. Sell Widgets
3. Quit
What would you like to do? 1
1
How many widgets would you like to buy? 5
What is the buying price for each widget? 9.6
1. Buy Widgets
2. Sell Widgets
3. Quit
What would you like to do? 2
2
How many widgets would you like to sell? 4
What is the selling price for each widget? 15
0.001. Buy Widgets
2. Sell Widgets
3. Quit
What would you like to do? 3
3
Your total profit for all transactions is $0.00