I'm working on a program for an idea I had. However, I ran into some errors I can't figure out regarding an object (for a class) and constructors.
Errors:
1 2 3 4 5 6 7 8
functions.cpp: In function ‘void loadShopFile()’:
functions.cpp:62:18: error: invalid use of ‘Item::Item’
temp.Item(itemName, buyPrice, sellPrice, amount, true); // creates a temp object and stores data in it.
^~~~
functions.cpp: In function ‘void loadShopFile()’:
functions.cpp:62:18: error: invalid use of ‘Item::Item’
temp.Item(itemName, buyPrice, sellPrice, amount, true); // creates a temp object and stores data in it.
^~~~
Main.cpp: (Not including most comments)
1 2 3 4 5 6 7 8
#include <iostream>
#include "functions.cpp"
int main() {
loadShopFile(); // asks user if they'd like to load a shop file etc..
return 0;
}
// functions.cpp
#include <iostream>
#include <string>
#include <fstream> // reading from file
#include <vector>
#include "functions.h" // 'Item' class file.
// Function References
void storeFileData();
// Asks user if they want to load a file with stored data then does actions based on their answer.
void loadShopFile() {
char uIn;
std::string itemName;
double buyPrice = -1, sellPrice = -1; // -1 is default value & means disabled.
int amount = -1;
std::vector<Item> itemList;
std::cout << "Do you have a file (that follows the required format) that you would like to load? (say 'y' or 'n') ";
std::cin >> uIn;
// if user wants to load a shop file, it will read from file and store data into program.
if (uIn == 'y' || uIn == 'Y')
storeFileData();
// else it will ask user how many shop items they want to add and to put '-1' if N/A.
else {
int itemCount;
std::cout << "How many shop items would you like to add (manually): ";
std::cin >> itemCount;
std::cout << "\n"
<< "Important Notes:\n"
<< "- Item name must be EXACT. For example: STICKY_PISTON\n"
<< "- Input 'cancel' for an item name input to stop it from asking you to add items.\n"
<< "- Input '-1' to disable the buy or sell price for an item.\n"
<< "\n"
<< "Example Setup:\n"
<< "Item Name: STICKY_PISTON\n"
<< "Buy Price: 30 (to buy FROM the shop)\n"
<< "Sell Price: 10 (to sell TO the shop)\n"
<< "Amount: 16 (the buy & sell prices are for this amount of items)\n"
<< "\n";
for (int i = 0; i < itemCount; i++) {
std::cout << "Please input an item name: ";
std::cin >> itemName;
// Checks whether user has added enough items and wants to cancel loop.
if (itemName == "cancel") break;
std::cout << "Please input a buy price: ";
std::cin >> buyPrice;
std::cout << "Please input a sell price: ";
std::cin >> sellPrice;
std::cout << "Please input an amount: ";
std::cin >> amount;
// Stores the item contents into class object.
// WIP.
Item temp;
temp.Item(itemName, buyPrice, sellPrice, amount, true); // creates a temp object and stores data in it.
itemList.push_back(temp); // Adds temp object to vector.
}
}
}
// Opens the file the user inputs and reads from it then stores buy & sell prices of items into map.
void storeFileData() {
std::ifstream inFile; // input file stream
std::string userIn; // file name user inputs
std::cout << "Please store the file in the same directory then input the file name you want to load items from: ";
std::cin >> userIn;
inFile.open(userIn);
}
You cannot call a constructor on an object that has already been created.
This
1 2 3
Item temp; //OBJECT CREATED HERE
temp.Item(itemName, buyPrice, sellPrice, amount, true); // Can't call constructor - object has already been constructed
itemList.push_back(temp); // Adds temp object to vector.
should be
1 2
Item temp(itemName, buyPrice, sellPrice, amount, true); // creates a temp object and stores data in it.
itemList.push_back(temp); // Adds temp object to vector.
You cannot call a constructor on an object that has already been created.
This
1 2 3
Item temp; //OBJECT CREATED HERE
temp.Item(itemName, buyPrice, sellPrice, amount, true); // Can't call constructor - object has already been constructed
itemList.push_back(temp); // Adds temp object to vector.
should be
1 2
Item temp(itemName, buyPrice, sellPrice, amount, true); // creates a temp object and stores data in it.
itemList.push_back(temp); // Adds temp object to vector.
Thanks for your reply!
Ah okay, yeah I tried something similar to that but messed up the syntax. Oops!
I've changed the lines to:
1 2 3 4
// Stores the item contents into class object.
// WIP.
Item temp(itemName, buyPrice, sellPrice, amount, true); // creates a temp object and stores data in it.
itemList.push_back(temp); // Adds temp object to vector.
However, now it gives a much stranger error: <Removed Error>
Edit: Oh, I'm dumb. Totally overlooked something that was clearly wrong in Main.cpp haha. All fixed. :)