简体   繁体   中英

syntax error : missing ';' before identifier

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"

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.

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> .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM