2012-11-23 34 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

相關問題