gcc编译错误如何解决
有以下程序,在Linux下用gcc编译成可执行代码错误如下:
a2.c:
#include <string.h>
class CName
{
char m_szFirst[256];
char m_szLast[256];
public:
void SetName(char* pszFirst, char* pszLast)
{
strcpy(m_szFirst, pszFirst);
strcpy(m_szLast, pszLast);
}
};
int main()
{
char* pCharArray = new char[256]; // Allocate memory for the array
strcpy(pCharArray, "Array of characters");
delete [] pCharArray; // Deallocate memory for the array
pCharArray = NULL;
CName* pName = new CName; // Allocate memory for the object
pName->SetName("Firstname", "Lastname");
delete pName; // Deallocate memory for the object
pName = NULL;
}
用gcc -o a2 a2.c进行编译,错误如下:
a2.c:3: error: syntax error before "CName"
a2.c:4: error: syntax error before '{' token
a2.c:7: error: syntax error before ':' token
a2.c:11: warning: parameter names (without types) in function declaration
a2.c:11: error: conflicting types for 'strcpy'
a2.c:11: error: conflicting types for 'strcpy'
a2.c:11: warning: data definition has no type or storage class
a2.c:12: error: syntax error before '}' token
a2.c: In function `main':
a2.c:17: error: `new' undeclared (first use in this function)
a2.c:17: error: (Each undeclared identifier is reported only once
a2.c:17: error: for each function it appears in.)
a2.c:17: error: syntax error before "char"
a2.c:19: error: `delete' undeclared (first use in this function)
a2.c:19: error: syntax error before ']' token
a2.c:21: error: `CName' undeclared (first use in this function)
a2.c:21: error: `pName' undeclared (first use in this function)
a2.c:21: error: syntax error before "CName"
a2.c:23: error: syntax error before "pName"
a2.c:25:2: warning: no newline at end of file