views:

110

answers:

3

I am new to c++, trying to debug the following line of code

class cGameError
{
    string m_errorText;
    public:
        cGameError( char *errorText )
        {
            DP1("***\n*** [ERROR] cGameError thrown! text: [%s]\n***\n",
            errorText );
            m_errorText = string( errorText );
        }

        const char *GetText()
        {
            return m_errorText.c_str();
        }
};

enum eResult
{
    resAllGood = 0, // function passed with flying colors
    resFalse = 1, // function worked and returns 'false'
    resFailed = –1, // function failed miserably
    resNotImpl = –2, // function has not been implemented
    resForceDWord = 0x7FFFFFFF
};

This header file is included in the program as followed

#include "string.h"
#include "stdafx.h"
#include "Chapter 01 MyVersion.h"
#include "cGameError.h"
+4  A: 

You need to include <string>, not "string.h". Or in addition to "string.h".

string.h is the C header for the standard C string handling functions (strcpy() and friends.)

<string> is the standard C++ header where 'string' is defined.

You also need to specify the std namespace when using string:

std::string m_errorText;

Or by using:

using namespace std;

Somewhere at the top of your file.

You should also use angle brackets for system include files.

Ori Pessach
Yes, also needed the namespace std;
numerical25
+1  A: 

You've provided little enough information that this is only a wild guess, but at first glance, I'd guess the problem is that you haven't included <string>, only "string.h" (the former defines the C++ std::string class, the latter the C functions for manipulating nul-terminated strings.

As an aside, you normally want to use angle-brackets for system headers, so it should be <string.h>.

Jerry Coffin
actually, "string.h" is not part of *any* standard - when you enclose the header name in quotes, the file is searched in the *current directory* only.
Stefan Monov
@Stefan: Not so. It is first searched in an implementation defined manner (typically the current directory), and if that fails, reprocessed as if it had used angle brackets (C99, §6.10.2/3, C++03 §16.2/3).
Jerry Coffin
Ahh, ok. I see.
Stefan Monov
+1  A: 

Try #include <string>, instead of #include "string.h", string.h/cstring is the old C-string header, string is the new C++ std::string class header. And you normally use angle-brackets for system headers.

Jacob