小白求助,c++自己编写的头文件中的类和函数怎样调用?

RascoWong 2012-12-05 09:27:37
我是用VC++作为开发工具的:
新建一个“Win32控制台应用程序项目”,
然后建立一个“控制台应用程序”并勾选预编译头。
然后我自己新建一个头文件”Simple.h“和一个执行文件“Simple.cpp”,具体如下:


/*--Simple.h 头文件--*/

#ifndef HARDWARE_H
#define HARDWARE_H

class CSimple
{
public:
int sum(int a, int b);
}

#endif



/*--Simple.cpp 执行文件--*/

#include "stdafx.h"

int CSimple::sum(int a, int b)
{
int sum = a + b;
return sum;
}



/*--Test.cpp 主程序--*/

#include <iostream>
using namespace std;

#include "stdafx.h"
#include "Simple.h"

int _tmain(int argc, _TCHAR* argv[])
{
int a=2, b=3;
Simple simple;
count << simple.sum(a,b) << endl;
return 0;
}

但是编译运行后会报一堆错位,请高手严厉指出错误!最好能提供修正后的完成可运行代码!感激不尽!
...全文
714 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
RascoWong 2012-12-05
  • 打赏
  • 举报
回复
我再发一遍修改后的代码,和编译时报的错误吧(标点都已经一个个校对过了,都是英文标点)
/*--Simple.h 头文件--*/

#ifndef HARDWARE_H
#define HARDWARE_H

class CSimple
{
public:
  int sum(int a, int b);
};

#endif
/*--Simple.cpp 头文件--*/

#include "stdafx.h"
#include "Simple.h"

int CSimple::sum(int a, int b)
{
	int sum = a + b;
  return sum;
}
/*--Test.cpp 主程序--*/

#include "stdafx.h"
#include "Simple.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
  int a=2, b=3;
  Simple simple;
  count << simple.sum(a,b) << endl;
  return 0;
}
1>------ 已启动生成: 项目: Test, 配置: Debug Win32 ------
1>正在编译...
1>Test.cpp
1>d:\projects\test\test\simple.h(9) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\simple.h(9) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\simple.h(9) : error C2146: 语法错误 : 缺少“;”(在标识符“sum”的前面)
1>d:\projects\test\test\simple.h(9) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\projects\test\test\simple.h(9) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\projects\test\test\simple.h(9) : warning C4183: “sum”: 缺少返回类型;假定为返回“int”的成员函数
1>d:\projects\test\test\test.cpp(10) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\test.cpp(10) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\test.cpp(10) : error C2065: “  int”: 未声明的标识符
1>d:\projects\test\test\test.cpp(10) : error C2146: 语法错误 : 缺少“;”(在标识符“a”的前面)
1>d:\projects\test\test\test.cpp(10) : error C2065: “a”: 未声明的标识符
1>d:\projects\test\test\test.cpp(10) : error C2065: “b”: 未声明的标识符
1>d:\projects\test\test\test.cpp(11) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\test.cpp(11) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\test.cpp(11) : error C2065: “  Simple”: 未声明的标识符
1>d:\projects\test\test\test.cpp(11) : error C2146: 语法错误 : 缺少“;”(在标识符“simple”的前面)
1>d:\projects\test\test\test.cpp(11) : error C2065: “simple”: 未声明的标识符
1>d:\projects\test\test\test.cpp(12) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\test.cpp(12) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\test.cpp(12) : error C2065: “  count”: 未声明的标识符
1>d:\projects\test\test\test.cpp(12) : error C2065: “simple”: 未声明的标识符
1>d:\projects\test\test\test.cpp(12) : error C2228: “.sum”的左边必须有类/结构/联合
1>        类型是“'unknown-type'”
1>d:\projects\test\test\test.cpp(12) : error C2065: “a”: 未声明的标识符
1>d:\projects\test\test\test.cpp(12) : error C2065: “b”: 未声明的标识符
1>d:\projects\test\test\test.cpp(13) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\test.cpp(13) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\test.cpp(13) : error C2065: “  return”: 未声明的标识符
1>d:\projects\test\test\test.cpp(13) : error C2143: 语法错误 : 缺少“;”(在“常量”的前面)
1>Simple.cpp
1>d:\projects\test\test\simple.h(9) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\simple.h(9) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\simple.h(9) : error C2146: 语法错误 : 缺少“;”(在标识符“sum”的前面)
1>d:\projects\test\test\simple.h(9) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\projects\test\test\simple.h(9) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\projects\test\test\simple.h(9) : warning C4183: “sum”: 缺少返回类型;假定为返回“int”的成员函数
1>d:\projects\test\test\simple.cpp(9) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\simple.cpp(9) : error C3872: “0x3000”: 此字符不允许在标识符中使用
1>d:\projects\test\test\simple.cpp(9) : error C2065: “  return”: 未声明的标识符
1>d:\projects\test\test\simple.cpp(9) : error C2146: 语法错误 : 缺少“;”(在标识符“sum”的前面)
1>正在生成代码...
1>生成日志保存在“file://d:\Projects\Test\Test\Debug\BuildLog.htm”
1>Test - 36 个错误,2 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
RascoWong 2012-12-05
  • 打赏
  • 举报
回复
引用 8 楼 wanglu343280746 的回复:
Simple.cpp这个文件里的头文件换成Simp.h。然后Test.cpp里面的Simple.h去掉
嗯嗯,“Simple.cpp”已经加上了头文件,但是还是报错
RascoWong 2012-12-05
  • 打赏
  • 举报
回复
引用 6 楼 super_admi 的回复:
Simple.cpp中要包含Simple.h
谢谢,这个的确是低级错误...
wanglu343280746 2012-12-05
  • 打赏
  • 举报
回复
引用 8 楼 wanglu343280746 的回复:
Simple.cpp这个文件里的头文件换成Simp.h。然后Test.cpp里面的Simple.h去掉
错了,后半句去掉
wanglu343280746 2012-12-05
  • 打赏
  • 举报
回复
Simple.cpp这个文件里的头文件换成Simp.h。然后Test.cpp里面的Simple.h去掉
super_admi 2012-12-05
  • 打赏
  • 举报
回复
某些标示符出了问题,就要检查一下自己是否使用了中文标点。
super_admi 2012-12-05
  • 打赏
  • 举报
回复
Simple.cpp中要包含Simple.h
RascoWong 2012-12-05
  • 打赏
  • 举报
回复
引用 4 楼 turingo 的回复:
出错信息是怎样的? 引用 3 楼 RascoWong 的回复:引用 1 楼 turingo 的回复:class声明的最后少了一个分号。分号加上了,但是错误还是一堆堆的...
1>------ 已启动生成: 项目: Test, 配置: Debug Win32 ------ 1>正在编译... 1>Test.cpp 1>d:\projects\test\test\simple.h(10) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\simple.h(10) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\simple.h(10) : error C2146: 语法错误 : 缺少“;”(在标识符“sum”的前面) 1>d:\projects\test\test\simple.h(10) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 1>d:\projects\test\test\simple.h(10) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 1>d:\projects\test\test\simple.h(10) : warning C4183: “sum”: 缺少返回类型;假定为返回“int”的成员函数 1>d:\projects\test\test\test.cpp(12) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\test.cpp(12) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\test.cpp(12) : error C2065: “  int”: 未声明的标识符 1>d:\projects\test\test\test.cpp(12) : error C2146: 语法错误 : 缺少“;”(在标识符“a”的前面) 1>d:\projects\test\test\test.cpp(12) : error C2065: “a”: 未声明的标识符 1>d:\projects\test\test\test.cpp(12) : error C2065: “b”: 未声明的标识符 1>d:\projects\test\test\test.cpp(13) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\test.cpp(13) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\test.cpp(13) : error C2065: “  Simple”: 未声明的标识符 1>d:\projects\test\test\test.cpp(13) : error C2146: 语法错误 : 缺少“;”(在标识符“simple”的前面) 1>d:\projects\test\test\test.cpp(13) : error C2065: “simple”: 未声明的标识符 1>d:\projects\test\test\test.cpp(14) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\test.cpp(14) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\test.cpp(14) : error C2065: “  count”: 未声明的标识符 1>d:\projects\test\test\test.cpp(14) : error C2065: “simple”: 未声明的标识符 1>d:\projects\test\test\test.cpp(14) : error C2228: “.sum”的左边必须有类/结构/联合 1> 类型是“'unknown-type'” 1>d:\projects\test\test\test.cpp(14) : error C2065: “a”: 未声明的标识符 1>d:\projects\test\test\test.cpp(14) : error C2065: “b”: 未声明的标识符 1>d:\projects\test\test\test.cpp(15) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\test.cpp(15) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\test.cpp(15) : error C2065: “  return”: 未声明的标识符 1>d:\projects\test\test\test.cpp(15) : error C2143: 语法错误 : 缺少“;”(在“常量”的前面) 1>Simple.cpp 1>d:\projects\test\test\simple.cpp(6) : error C2653: “CSimple”: 不是类或命名空间名称 1>d:\projects\test\test\simple.cpp(9) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\simple.cpp(9) : error C3872: “0x3000”: 此字符不允许在标识符中使用 1>d:\projects\test\test\simple.cpp(9) : error C2065: “  return”: 未声明的标识符 1>d:\projects\test\test\simple.cpp(9) : error C2146: 语法错误 : 缺少“;”(在标识符“sum”的前面) 1>正在生成代码... 1>生成日志保存在“file://d:\Projects\Test\Test\Debug\BuildLog.htm” 1>Test - 32 个错误,1 个警告 ========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
图灵狗 2012-12-05
  • 打赏
  • 举报
回复
出错信息是怎样的?
引用 3 楼 RascoWong 的回复:
引用 1 楼 turingo 的回复:class声明的最后少了一个分号。分号加上了,但是错误还是一堆堆的...
RascoWong 2012-12-05
  • 打赏
  • 举报
回复
引用 1 楼 turingo 的回复:
class声明的最后少了一个分号。
分号加上了,但是错误还是一堆堆的...
lilianjie001 2012-12-05
  • 打赏
  • 举报
回复
lz这错误犯的,细节需注意。
图灵狗 2012-12-05
  • 打赏
  • 举报
回复
class声明的最后少了一个分号。
super_admi 2012-12-05
  • 打赏
  • 举报
回复
修改办法:选中一个空白,然后进行替换操作,全部换成空格符。
super_admi 2012-12-05
  • 打赏
  • 举报
回复
我大概知道是什么原因了。 这里的代码,你是不是从网上复制下来的?? 我仔细研究了一下,问题多半出在空白上面,你所看到的空白,应该不是空格符,而是其它某种空白符号。你注意看错误提示,人家说的不是"int","return"错误,而是" int"," return"错误。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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