![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
运行一个c++小程序时出现了下面错误
using namespace std;
class father
{
public:
virtual void run()const{cout<<"父亲可以跑万米\n";}
};
class son:public father
{
public:
void run()const{cout<<"儿子可以跑十万米\n";}
};
class daughter:public father
{
public:
void run()const{cout<<"女儿可以跑五万米\n";}
};
void one(father); //参数为father类的一个对象
void two(father*); //参数为指向father类的一个指针
void three(father&); //参数为father类的一个引用
int main()
{
father *p=0;
int choice;
while (1)
{
bool quit=false;
cout<<"(0)quit (1)son (2)daughter (3)father:";
cin>>choice;
switch (choice)
{
case 0:quit=true;
break;
case 1:p=new son;
one(*p);
break;
case 2:p=new daughter;
two(p);
break;
case 3:p=new father;
three(*p);
break;
default:cout<<"请输入0到3之间的数字\n";
if (quit)
{
break;
}
}
return 0;
}
void one(father one)//用对象调用虚函数run
{
one.run();
}
void two(father *two)//用指针调用虚函数run
{
two->run();
}
void three(father &three) //用引用调用虚函数run
{
three.run();
}
错误提示如下:
:\Program Files\C++\第十三章_虚函数\三种调用虚函数的比较.cpp(58) : error C2601: 'one' : local function definitions are illegal
D:\Program Files\C++\第十三章_虚函数\三种调用虚函数的比较.cpp(63) : error C2601: 'two' : local function definitions are illegal
D:\Program Files\C++\第十三章_虚函数\三种调用虚函数的比较.cpp(68) : error C2601: 'three' : local function definitions are illegal
D:\Program Files\C++\第十三章_虚函数\三种调用虚函数的比较.cpp(71) : fatal error C1004: unexpected end of file found 展开
//他们两位说的对, 你少了一个右括号, 希望你能用一个语法高亮之类的编辑器
//执行程序, 选择1, 输出是父亲
#include <iostream>
using namespace std;
class father
{
public:
virtual void run()const{cout<<"父亲可以跑万米\n";}
};
class son:public father
{
public:
void run()const{cout<<"儿子可以跑十万米\n";}
};
class daughter:public father
{
public:
void run()const{cout<<"女儿可以跑五万米\n";}
};
void one(father); //参数为father类的一个对象
void two(father*); //参数为指向father类的一个指针
void three(father&); //参数为father类的一个引用
int main()
{
father *p=0;
int choice;
while (1)
{
bool quit=false;
cout<<"(0)quit (1)son (2)daughter (3)father:";
cin>>choice;
switch (choice)
{
case 0:quit=true;
break;
case 1:p=new son;
one(*p);
break;
case 2:p=new daughter;
two(p);
break;
case 3:p=new father;
three(*p);
break;
default:cout<<"请输入0到3之间的数字\n";
}
if (quit)
{
break;
}
}
return 0;
}
void one(father one)//用对象调用虚函数run
{
one.run();
}
void two(father *two)//用指针调用虚函数run
{
two->run();
}
void three(father &three) //用引用调用虚函数run
{
three.run();
}