denniss
denniss

Reputation: 17629

C++ error no such file or directory

Hey guys, I just started learning C++ and I have this code that I have to complete but the class header is giving me this error

error: string: No such file or directory

#include <vector>
#include <string>

class Inventory
{
public:
    Inventory ();
    void Update (string item, int amount);
    void ListByName ();
    void ListByQuantity ();
private:
    vector<string> items;
};

Upvotes: 0

Views: 23799

Answers (3)

Component 10
Component 10

Reputation: 10507

I don't think your error is anything to do with namespaces.

You say you're getting error: string: No such file or directory which implies that the pre-compiler cannot find the STL string definition file. This is quite unlikely if you're also including vector and having no problems with that.

You should check your compilation output for clues about where it's picking header files from. Any chance you could post the full compilation output?

Upvotes: 3

justin
justin

Reputation: 104728

your code should probably look something more like this:

#include <string>
#include <vector>

class Inventory {
public:
    Inventory();
    void Update(const std::string& item, int amount);
    void ListByName();
    void ListByQuantity();
private:
    std::vector<std::string> items;
};

if #include <string> is in fact your include directive, then you may be compiling the file as a c program. the compiler often determines language by the file extension. what is your file named?

Upvotes: 5

Benoit
Benoit

Reputation: 79243

Either use using std::string (not recommended) or replace string with std::string.

Or, if I have misunderstood, use #include <string> instead of #include "string".

Same goes for vector which is also in std namespace.

Upvotes: 2

Related Questions