CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2008
    Location
    United States
    Posts
    71

    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?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Error with string as a constructor parameter?

    What is planetName? Show all your relevant code.

  3. #3
    Join Date
    Jun 2008
    Location
    United States
    Posts
    71

    Re: Error with string as a constructor parameter?

    Sorry, I removed irrelevant portions of code to simplify the problem. planetName = myString.

  4. #4
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    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

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Error with string as a constructor parameter?

    Since that's in a header, you should be fully-qualifying std::string.

  6. #6
    Join Date
    Jun 2008
    Location
    United States
    Posts
    71

    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!

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Error with string as a constructor parameter?

    Quote Originally Posted by Robotics Guy View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured