0

嘿,我在头文件(Inventory.h)中有一个向量以及头文件(Inventory.h)的cpp(Inventory.cpp)文件。如果不创建一个名为 add 的单独函数,我无法弄清楚如何使用 push 和 pop 函数,看起来像这样。

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

但这样做向我展示了一个错误:

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

这是我的 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

还有我的 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;
}

如果我能获得有关此功能的帮助或在头文件中使用主文件中的推送/弹出的解决方案,将不胜感激。提前致谢。

编辑:新错误。

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
4

2 回答 2

4

您必须确保您的.h.cpp方法匹配

。H

string add(string item);

.cpp

string Inventory::add(string item)

它说未找到重载函数,因为您试图调用未在标题中定义的函数。

于 2012-11-21T23:42:41.423 回答
2

似乎声明add()及其实现使用了不同的签名:

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

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

无论如何,额外的's'vector<string>似乎完全不合适。

于 2012-11-21T23:43:04.297 回答