Note that there are some explanatory texts on larger screens.

plurals
  1. POerror with add method in the header file in C++
    primarykey
    data
    text
    <p>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.</p> <pre><code>vector&lt;string&gt;s Inventory ::add(string item) { inventory.push_back(item); return item; } </code></pre> <p>But doing this shows me an error:</p> <pre><code>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 </code></pre> <p>Here is my Inventory.h</p> <pre><code>#ifndef INVENTORY_H #define INVENTORY_H class Inventory { public: //Constructor Inventory(); //Methods. string add(); void displayInventory(); void showInventory(); private: //Data members }; #endif //INVENTORY_H </code></pre> <p>And my Inventory.cpp.</p> <pre><code>#include "Inventory.h" #include &lt;iostream&gt; #include &lt;vector&gt; // To enable the use of the vector class. #include &lt;string&gt; using namespace std; vector&lt;string&gt; inventory; vector&lt;string&gt;::iterator myIterator; vector&lt;string&gt;::const_iterator iter; Inventory::Inventory() { } vector&lt;string&gt;s Inventory ::add(string item) { inventory.push_back(item); return item; } void Inventory:: showInventory() { char input[80]; cin &gt;&gt; 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&lt;string&gt; inventory; cout&lt;&lt; "You have " &lt;&lt; inventory.size() &lt;&lt; " items.\n"; cout &lt;&lt; "\n******Inventory******"; cout&lt;&lt; "\nYour items:\n"; for (int i= 0; i&lt; inventory.size(); ++i) cout&lt;&lt; inventory[i] &lt;&lt; endl; } </code></pre> <p>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.</p> <p>Edit: New Errors.</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload