Overload Constructor Errors

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:
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
// 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);
}


functions.h:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// functions.h (Item class file used in functions.cpp)

// Guard
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <iostream>
#include <string>


class Item {
    private:
        // Declaring values & setting defaults.
        std::string name;
        double buy; // buy FROM market price
        double sell; // sell TO market price
        int amount; // Amount that the buy & sell prices are for.
        //double buyPriceEach;
        //double sellPriceEach; 
        bool enabled; // option for whether item is enabled or disabled.

    public:
        // default constructor
        Item();

        // Overload constructor
        Item(const std::string newName, double newBuy, double newSell, int newAmount, bool newEnabledSetting);

        // getters
        std::string getName() const; // ex usage: std::cout << objName.getName() << '\n';
        double getBuy() const;
        double getSell() const;
        int getAmount() const;
        bool getEnabled() const;

        // setters
        void setName(const std::string newName);
        void setBuy(double newBuy);
        void setSell(double newSell);
        void setAmount(double newAmount);
        void setEnabled(bool newEnabledSetting);
};


// Member function definitions

// default constructor
Item::Item() {
    name = "";
    buy = -1;
    sell = -1;
    amount = 0;
    enabled = false;
}

// constructor
Item::Item(std::string newName, double newBuy, double newSell, int newAmount, bool newEnabledSetting) {
    name = newName;
    buy = newBuy;
    sell = newSell;
    amount = newAmount;
    enabled = newEnabledSetting;
}

// getters
std::string Item::getName() const {
    return name;
}
double Item::getBuy() const {
    return buy;
}
double Item::getSell() const {
    return sell;
}
int Item::getAmount() const {
    return amount;
}
bool Item::getEnabled() const {
    return enabled;
}

// setters
void Item::setName(std::string newName) {
    name = newName;
}
void Item::setBuy(double newBuy) {
    buy = newBuy;
}
void Item::setSell(double newSell) {
    sell = newSell;
}
void Item::setAmount(double newAmount) {
    amount = newAmount;
}
void Item::setEnabled(bool newEnabledSetting) {
    enabled = newEnabledSetting;
}


#endif 


I'm sure it's a simple fix of something I overlooked or forgot, I just can't tell what. I tried checking online but have had no luck so far.

Help would be greatly appreciated. Thanks!
Last edited on
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. 
Last edited on
Repeater wrote:

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. :)
Last edited on
Topic archived. No new replies allowed.