|
-
September 21st, 2011, 02:26 PM
#1
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:

If I change myString to type INT, there is no compilation error. So what am I doing wrong?
-
September 21st, 2011, 02:32 PM
#2
Re: Error with string as a constructor parameter?
What is planetName? Show all your relevant code.
-
September 21st, 2011, 02:36 PM
#3
Re: Error with string as a constructor parameter?
Sorry, I removed irrelevant portions of code to simplify the problem. planetName = myString.
-
September 21st, 2011, 02:37 PM
#4
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
-
September 21st, 2011, 02:37 PM
#5
Re: Error with string as a constructor parameter?
Since that's in a header, you should be fully-qualifying std::string.
-
September 21st, 2011, 02:42 PM
#6
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!
-
September 21st, 2011, 03:18 PM
#7
Re: Error with string as a constructor parameter?
 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).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|