2012-11-23 21 views
0

嘿,我试着在inventory.h中创建我的向量,这样我就拥有了像push_back,pop等那样的所有向量函数......而不是打字所有的功能,如弹出,推我想要一个指针工作,但我得到了错误。任何人都可以帮助我解决这个问题,并告诉我,我会走在正确的道路上。当我在我的Inventory.h中指向一个向量时出现错误

在此先感谢。

这里是我的代码:

Inventory.h

//------------------------------------------------------------------------------- 
// Inventory.h 
//------------------------------------------------------------------------------- 

#ifndef INVENTORY_H 
#define INVENTORY_H 
#include <string> 
#include <vector> 

using namespace std; 
class Inventory 
{ 
public: 
    //Constructor 
    Inventory(); 

    //Methods. 
    string add(string item); 
    void displayInventory(); 
    void showInventory(); 
    //vector<string> &GetContainer(); 
private: 
    //Data members 
    vector<string> inventory; 
    vector<string>::iterator myIterator; 
    vector<string>::const_iterator iter; 
    }; 


#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; 



Inventory::Inventory() 
{ 

} 

string 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; 
} 

的main.cpp

int main() 
{ 
inventory.GetContainer().push_back("Stone"); 
} 

下面是错误:

Error 2 error LNK2019: unresolved external symbol "public: class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > & __thiscall Inventory::GetContainer(void)" ([email protected]@@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@XZ) referenced in function _main C:\Users\Conor\Documents\College\DKIT - Year 2 - Repeat\DKIT - Year 2 - Semester 1 - Repeat\Games Programming\MaroonedCA2\MaroonedCA2\Main.obj MaroonedCA2 
Error 3 error LNK1120: 1 unresolved externals C:\Users\Conor\Documents\College\DKIT - Year 2 - Repeat\DKIT - Year 2 - Semester 1 - Repeat\Games Programming\MaroonedCA2\Debug\MaroonedCA2.exe MaroonedCA2 
+0

为什么你要在.cpp文件中包含矢量和字符串,只要你包含“inventory.h”,那么它就会包含矢量和字符串。 – Aaron

+1

@ForgiveMe,但这样做没有任何损失。事实上,有一些好处:如果由于某种原因头文件不再需要'std :: vector'并且你从它中移除包含文件,.cpp文件仍然可以很好地编译。 –

回答

1

它看起来像的Inventors::GetContainer()实施丢失。您必须将其放入.cpp文件中,或者在Inventory类声明中内联。

.cpp文件:

vector<string>& Inventory::GetContainer() { return inventory; } 

虽然小心,你暴露私有数据成员。如果你打算这样做,你也可以让这个成员公开。这样,你不会幻想你有某种形式的封装。

+0

哦,我明白了。我究竟会把什么放在.cpp中。因为我实际上不需要任何东西,因为它是一个指针。 – Pendo826

+0

我认为这将工作:向量库存:: GetContainer() { } – Pendo826

+1

@ Pendo826你通过引用返回向量。我添加了必要的行。在C++中没有任何事情发生,你必须在大多数情况下是明确的。 – juanchopanza

相关问题