gcc编译错误如何解决

wonderfull_zhb 2008-06-19 01:27:04

有以下程序,在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
...全文
254 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
blh 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用楼主 wonderfull_zhb 的帖子:]

有以下程序,在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"…
[/Quote]

要么你用g++,要么你用gcc -x c++
K行天下 2008-06-19
  • 打赏
  • 举报
回复
#include <cstring>
using namespace std;
fallening 2008-06-19
  • 打赏
  • 举报
回复
用g++编译,这里是c++程序,不是c程序
  • 打赏
  • 举报
回复
gcc -o a2 a2.c 这是编译C程序的
a2.c改为a2.cpp

然后用
g++ -o a2 a2.cpp
coverallwangp 2008-06-19
  • 打赏
  • 举报
回复
main函数怎么没有return

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧