Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:00:17+00:00 2026-06-14T17:00:17+00:00

Hey i set up a header and a .cpp file for my inventory. Im

  • 0

Hey i set up a header and a .cpp file for my inventory. Im using a vector. I cant use anyof the push/ or pop methods that come in the vector library. I want to use them in the main. Also i am gettin 3 errors to do with the add method i made so that i could add to the vector in the main.

Can anyone help me to understand why i cant use the vectors functions and why i am getting these errors.

Here is my code:
Inventory.h

 #ifndef INVENTORY_H
    #define INVENTORY_H
    #include <string>


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

    //Methods.
    std:: string add(string item);
    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()
{

}

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

Errors

Error   1   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   2   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   3   error C2511: 'std::string 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:00:18+00:00Added an answer on June 14, 2026 at 5:00 pm

    Your issue is in the header, and you lack of using namespace std. I never, ever use this command. I think it’s must better to be explicit with your namespaces, and you will avoid issues like this.

    // Compiler knows what std::string is, but not string on it's own.
    std:: string add(string item);
    

    If it was my project, I would remove the using namespace std and use std:: everywhere.

    Furthermore, you’re not using your class properly. You have member methods that are operating on global variables in the .cpp file.

    vector<string> inventory;
    

    Should be a a private member of the Inventory class.

    Iterarors

    To address your comments, what I’m trying to say is that you don’t need to declare iterators like that to use them, unless you need to store an iterator for later use (for some reason. Although can be dangerous when iterators are invalidated). Here are a few ways you can use them.

    typedef

    Using typedefs makes your life easier.

    typedef std::vector<std::string> StringVec;
    typedef StringVec::iterator StringVecIter;
    

    Now you can use them in loops.

    for(StringVecIter it = inventory.begin(); iter != inventory.end(); ++it)
    { ... }
    

    auto

    An easier way to do these loops, is with the auto keyword. The compiler assign the appropriate type to the variable, which in this case is std::vector<std::string>::iterator

    for(auto it = inventory.begin(); it != inventory.end(); ++it)
    { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all how can i set this up for a loop? data.row9_1 I cant
Hey i dont know if that is possible but i want to set a
Hey! I am trying to set up routes in a Rails application so that,
Hey guys..I am designing a set-up wizard using CPropertySheet and CpropertyPage in MFC application...I
Hey everyone, some background info: In the config file for my website, I set
Hey i'm using the following code to set a cookie and get it :
hey guys, what's you take on this. i want to set a header image
Hey guys. I would like to set up the alias in my enviromental file
hey, i am trying to sort my set container using afunctor: struct CompareCatId :
Hey guys, given a data set in plain text such as the following: ==Events==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.