|
| » 首页 » 电脑_数码 » 编程 » 函数重载的问题 |
函数重载的问题 |
|
class A { public: func() { cout << 'A' << endl;} } class B : public A { public: func(char *p) { cout << p << endl;} } class C :public A { public: func() { cout << 'C' << endl; } func(char *p) { cout << p << endl; } } main() { A a, *pa; B b; C c; a.func(); b.func(); c.func(); b.func("BB"); c.func("CC"); pa = &b; pa->func(); pa->func("BBB"); pa = &c; pa->func(); pa->func("CCC"); } |
![]() |
|
|
1>------ 已启动生成: 项目: 0000000000, 配置: Debug Win32 ------ 1>正在编译... 1>template.cpp 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2236: 意外的“class”“B”。是否忘记了“;”? 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2143: 语法错误 : 缺少“;”(在“:”的前面) 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2059: 语法错误 : “:” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2065: “cout”: 未声明的标识符 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2065: “endl”: 未声明的标识符 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2059: 语法错误 : “public” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(8) : error C2143: 语法错误 : 缺少“;”(在“{”的前面) 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(8) : error C2447: “{”: 缺少函数标题(是否是老式的形式表?) 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(16) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(16) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(17) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(17) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(21) : error C3874: “main”的返回类型应为“int”而非“C” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2065: “B”: 未声明的标识符 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2146: 语法错误 : 缺少“;”(在标识符“b”的前面) 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2065: “b”: 未声明的标识符 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(27) : error C2228: “.func”的左边必须有类/结构/联合 1> 类型是“'unknown-type'” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(30) : error C2228: “.func”的左边必须有类/结构/联合 1> 类型是“'unknown-type'” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(35) : error C2660: “A::func”: 函数不接受 1 个参数 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(39) : error C2660: “A::func”: 函数不接受 1 个参数 1>生成日志保存在“file://e:\ooooooooooo\c++{answer}00000000\Debug\BuildLog.htm” 1>0000000000 - 19 个错误,3 个警告 ========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ========== ========================================================= #include<iostream> using namespace std; class A { public: void func() { cout << "A" << endl;} }; class B : public A { public: void func(char *p) { cout << p << endl;} }; class C : public A { public: void func() { cout << "C" << endl; } void func(char *p) { cout << p << endl; } } ; void main() { A a; A *pa = NULL; B b; C c; char *p = "abd"; a.func(); b.func(p); c.func(); b.func("BB"); c.func("CC"); pa = &b; pa->func(); pa->func(); pa = &c; pa->func(); pa->func(); system("pause"); } ============================================= 1。 #include<iostream> using namespace std; 2。 类的定义以“;”结束 3。 定义函数要有返回值,没有返回值可用void func()==>>void func() 4。 使用“cout” 出现问题 5。 若要在dos下看到效果则在代码后加“system("pause");” 1>------ 已启动生成: 项目: 0000000000, 配置: Debug Win32 ------ 1>正在编译... 1>template.cpp 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2236: 意外的“class”“B”。是否忘记了“;”? 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2143: 语法错误 : 缺少“;”(在“:”的前面) 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2059: 语法错误 : “:” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2065: “cout”: 未声明的标识符 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(4) : error C2065: “endl”: 未声明的标识符 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(7) : error C2059: 语法错误 : “public” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(8) : error C2143: 语法错误 : 缺少“;”(在“{”的前面) 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(8) : error C2447: “{”: 缺少函数标题(是否是老式的形式表?) 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(16) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(16) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(17) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(17) : warning C4183: “func”: 缺少返回类型;假定为返回“int”的成员函数 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(21) : error C3874: “main”的返回类型应为“int”而非“C” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2065: “B”: 未声明的标识符 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2146: 语法错误 : 缺少“;”(在标识符“b”的前面) 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(23) : error C2065: “b”: 未声明的标识符 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(27) : error C2228: “.func”的左边必须有类/结构/联合 1> 类型是“'unknown-type'” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(30) : error C2228: “.func”的左边必须有类/结构/联合 1> 类型是“'unknown-type'” 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(35) : error C2660: “A::func”: 函数不接受 1 个参数 1>e:\ooooooooooo\c++{answer}00000000\template.cpp(39) : error C2660: “A::func”: 函数不接受 1 个参数 1>生成日志保存在“file://e:\ooooooooooo\c++{answer}00000000\Debug\BuildLog.htm” 1>0000000000 - 19 个错误,3 个警告 ========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ========== ========================================================= #include<iostream> using namespace std; class A { public: void func() { cout << "A" << endl;} }; class B : public A { public: void func(char *p) { cout << p << endl;} }; class C : public A { public: void func() { cout << "C" << endl; } void func(char *p) { cout << p << endl; } } ; void main() { A a; A *pa = NULL; B b; C c; char *p = "abd"; a.func(); b.func(p); c.func(); b.func("BB"); c.func("CC"); pa = &b; pa->func(); pa->func(); pa = &c; pa->func(); pa->func(); system("pause"); } ============================================= 1。 #include<iostream> using namespace std; 2。 类的定义以“;”结束 3。 定义函数要有返回值,没有返回值可用void func()==>>void func() 4。 使用“cout” 出现问题 5。 若要在dos下看到效果则在代码后加“system("pause");” |
| 《函数重载的问题》答案收集时间:2008-06-14 14:37:02 |