3

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
m-use
  • 363
  • 1
  • 4
  • 16
  • 1
    Never put "use namespace std" in a header file. Better yet, [never put "use namespace std" anywhere](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice). Simply forget that "use namespace" exists in C++. Put it out of your mind, completely. Always use full namespace qualification when referring to classes, i.e. "std::string". – Sam Varshavchik Apr 03 '16 at 01:55
  • Unrelated, but public member variables are generally a poor idea. In this case I'd make `toString` a method instead. – MrEricSir Apr 03 '16 at 01:57
  • Sam, I've heard your advice from others as well and it makes sense. Unfortunately, this is a class assignment and we're required to use "using namespace std;" as per our instructor's directions. – m-use Apr 03 '16 at 02:03
  • 3
    I'm sorry to hear that your instructor is incompetent. The best you could do, perhaps, is move "use namespace std" into a .cpp file, and not the header file. In the case your instructor is telling you to put it in the header file, then if it's possible for you to drop this class, and pick a different instructor, you should do that. You're not going to get anyone here help you learn bad programming practices, sorry. – Sam Varshavchik Apr 03 '16 at 02:05
  • I think it's imposible you are getting that error, at least with the pasted code we see. Perhaps the error is related to other pieces of code you are not including here, like other functions or variables with name "string" declared before, or some problems with macros. If you write `std::string` the issue is solved? – ABu Apr 03 '16 at 05:24

2 Answers2

2

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

Daniel
  • 66
  • 1
  • 9
0

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;

Ahmad Khudeish
  • 1,017
  • 13
  • 15