Error with string as a constructor parameter?
Here's my code:
Code:
#include <string>
#ifndef MAIN_H
class MyClass
{
public:
MyClass();
MyClass(string myString);
};
MyClass::MyClass()
{
}
MyClass::MyClass(string myString)
{
}
#endif
Visual Studio gives me the following error:
http://img683.imageshack.us/img683/9997/errorgx.png
If I change myString to type INT, there is no compilation error. So what am I doing wrong?
Re: Error with string as a constructor parameter?
What is planetName? Show all your relevant code.
Re: Error with string as a constructor parameter?
Sorry, I removed irrelevant portions of code to simplify the problem. planetName = myString.
Re: Error with string as a constructor parameter?
I can't see the link, but your compiler does not know what 'string' is.
Code:
#include <string>
#ifndef MAIN_H
class MyClass
{
public:
MyClass();
MyClass(std::string myString);
};
MyClass::MyClass()
{
}
MyClass::MyClass(std::string myString)
{
}
#endif
Re: Error with string as a constructor parameter?
Since that's in a header, you should be fully-qualifying std::string.
Re: Error with string as a constructor parameter?
Oh, thanks! I didn't realize I had the "using namespace std;" line below the inclusion of this header file and I had no idea what this error meant..
Appreciate your help!
Re: Error with string as a constructor parameter?
Quote:
Originally Posted by
Robotics Guy
Oh, thanks! I didn't realize I had the "using namespace std;" line below the inclusion of this header file and I had no idea what this error meant..
Appreciate your help!
By and large, it's best not to rely on "using" statements in header files for two reasons:
1) You don't want the inclusion of a header file to automatically bring a given namespace into scope, because that could preclude using two particular headers together for no good reason in some cases.
2) You don't want the header to compile or break based on the order of inclusion.
That said, "using" statements are permissible in header files so long as they have restricted scope (such as an inline function implementation).