简体   繁体   中英

error with add method in the header file in C++

Hey i have a vector in a header file(Inventory.h) along with a cpp(Inventory.cpp) file for the header file(Inventory.h). I cant figure out how to use the push and pop functions without creating a seperate function called add that looks like this.

vector<string>s Inventory ::add(string item)
{
inventory.push_back(item);
return item;
}

But doing this shows me an error:

Error   1   error C2511: 'void Inventory::add(std::string)' : overloaded member function not found in 'Inventory'   c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 19  1   MaroonedCA2

Here is my Inventory.h

#ifndef INVENTORY_H
#define INVENTORY_H


class Inventory
{
public:
    //Constructor
    Inventory();

    //Methods.
    string add();
    void displayInventory();
    void showInventory();
private:
    //Data members
    };


#endif //INVENTORY_H

And my Inventory.cpp.

#include "Inventory.h"
#include <iostream>
#include <vector>   //  To enable the use of the vector class.
#include <string>


using namespace std;
vector<string> inventory;
vector<string>::iterator myIterator;
vector<string>::const_iterator iter;


Inventory::Inventory()
{

}

vector<string>s Inventory ::add(string item)
{
inventory.push_back(item);
return item;
}

void Inventory:: showInventory()
{
char input[80];
    cin >> input;
    char inventoryRequest[] = "i";
    int invent = strcmp (input,inventoryRequest);
    //compare the player input to inventoryRequest (i) to see if they want to look at inventory.
    if(invent == 0)
    {
        displayInventory();
    }


}
void Inventory:: displayInventory()
{
//vector<string> inventory;
    cout<< "You have " << inventory.size() << " items.\n";
    cout << "\n******Inventory******";
    cout<< "\nYour items:\n";
    for (int i= 0; i< inventory.size(); ++i)
        cout<< inventory[i] << endl;
}

If i could get help with this function or a solution to using the push/pop in my main whilst in the header file it would be appreciated. Thanks in advance.

Edit: New Errors.

Error   1   error C2146: syntax error : missing ';' before identifier 'add' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h   17  1   MaroonedCA2
Error   2   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h   17  1   MaroonedCA2
Error   3   error C2061: syntax error : identifier 'string' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h   17  1   MaroonedCA2
Error   5   error C2146: syntax error : missing ';' before identifier 'add' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h   17  1   MaroonedCA2
Error   6   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h   17  1   MaroonedCA2
Error   7   error C2061: syntax error : identifier 'string' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h   17  1   MaroonedCA2

You have to make sure your .h and .cpp methods match

.h

string add(string item);

.cpp

string Inventory::add(string item)

It says overloaded function not found because you're trying to call a function you did not define in your header.

It seems the declaration of add() and its implementation use different signatures:

class Inventory
{
public:
    // ...
    string add();
};

vector<string>s Inventory ::add(string item)
{
    // ...
}

where the extra 's' after vector<string> seems to be entirely out of place anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM