m-use
m-use

Reputation: 363

C++ Unknown type name 'string'

I'm working on a C++ header file and I keep getting the error "Unknown type name 'string'; did you mean 'std::string'?" #include <string> is already at the top of my code, so I'm not sure how else to remedy this issue. Any thoughts? I'm using Xcode, if that makes any difference.

#ifndef POINT_HPP
#define POINT_HPP
#include <string>
using namespace std;

class Point {
public:
    Point();
    Point(int pInitX, int pInitY);
    Point(int pInitX, int pInitY, int pInitColor);
    double distance(Point pAnotherPoint);
    int getColor();
    int getX();
    int getY();
    void move(int pNewX, int pNewY);
    void setColor(int pNewColor);
    void setX(int pNewX);
    void setY(int pNewY);
    string toString; // Error: Unknown type name 'string'; did you mean 'std::string'?
private:
    void init(int pInitX, int pInitY, int pInitColor);
    int mColor;
    int mX;
    int mY;
};

#endif

Upvotes: 3

Views: 15545

Answers (2)

Ahmad Khudeish
Ahmad Khudeish

Reputation: 1137

Make sure to also set the path to the Point class (header and cpp) inside CMakeLists.txt.

If you are using QT, it should be something like

set(PROJECT_SOURCES
        ...
        point.cpp
        point.h
)

After that you need to import the string class at the top like this #include <string> and then declare using namespace std;

Upvotes: 0

Daniel
Daniel

Reputation: 64

you must use std::string toString()or declare using namespace std;globally

Upvotes: 1

Related Questions