首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

protected函数调用有关问题

2012-02-17 
protected函数调用问题调用基类的protected成员函数时,在派生类中写一个public成员函数,用其调用基类的pro

protected函数调用问题
调用基类的protected成员函数时,在派生类中写一个public成员函数,用其调用基类的protected成员函数。
为什么编译无法通过,显示fucktiong   renturns   fucktion?

如:
//animal.h

#ifndef   ANIMAL_H
#define   ANIMAL_H
#include   <string>
#include   <iostream>

using   namespace   std;

class   Animal{
public:
Animal(int   wt,   string   theName);
protected:
void   who()const;//调用该函数
private:
int   weight;
string   name;
};

class   Lion:public   Animal{
public:
Lion(int   wt,   string   theName):Animal(wt,   theName){
cout < < "The   Lion   constructed " < <endl;
}

void   LionWho()const();//重写一个成员函数
};


class   Aardvark:public   Animal{
Aardvark(int   wt,   string   theName):Animal(wt,   theName){
cout < < "The   Aardvark   constructed " < <endl;
}
using   Animal::who;
};

#endif


//animal.cpp

#include   <iostream>
#include   <string>
#include   "animal.h "

using   namespace   std;


Animal::Animal(int   wt,   string   theName):weight(wt),   name(theName){
}

void   Animal::who()const{
cout < < "The   animal 's   name   is     " < <name < <endl;
cout < < "The   animal 's   weight   is     " < <weight < <endl;
}

void   Lion::TheWho()const(){//利用成员函数调用who函数
cout < < "\nI   am   a   Lion 'who() ";
Animal::who();
}


//main.cpp

#include   <iostream>
#include   <string>
#include   "animal.h "

using   namespace   std;

int   main(){
Lion   myLion(20,   "devil ");
myLion.TheWho();

return   0;
}


--------------------Configuration:   thirtytwo   -   Win32   Debug--------------------
Compiling...
animal.cpp
e:\myprogrme\fifteen\thirtytwo\animal.h(26)   :   error   C2091:   function   returns   function
e:\myprogrme\fifteen\thirtytwo\animal.cpp(18)   :   error   C2091:   function   returns   function
main.cpp
e:\myprogrme\fifteen\thirtytwo\animal.h(26)   :   error   C2091:   function   returns   function
Error   executing   cl.exe.

thirtytwo.exe   -   3   error(s),   0   warning(s)


[解决办法]
//animal.h

#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
#include <iostream>

using namespace std;

class Animal{
public:
Animal(int wt, string theName);
protected:
void who()const;//调用该函数
private:
int weight;
string name;
};

class Lion:public Animal{
public:
Lion(int wt, string theName):Animal(wt, theName){
cout < < "The Lion constructed " < <endl;
}

// > > 没见过const后面带()的,语法错误 < <
//void LionWho()const();//重写一个成员函数
void LionWho()const;
};


class Aardvark:public Animal{


Aardvark(int wt, string theName):Animal(wt, theName){
cout < < "The Aardvark constructed " < <endl;
}
using Animal::who;
};

#endif


//animal.cpp

#include <iostream>
#include <string>
#include "animal.h "

using namespace std;


Animal::Animal(int wt, string theName):weight(wt), name(theName){
}

void Animal::who()const{
cout < < "The animal 's name is " < <name < <endl;
cout < < "The animal 's weight is " < <weight < <endl;
}

// > > 此处修改原因同上,另外TheWho和LionWho两者必须统一 < <
//void Lion::TheWho()const(){//利用成员函数调用who函数
void Lion::LionWho()const{//利用成员函数调用who函数
cout < < "\nI am a Lion 'who() ";
Animal::who();
}


//main.cpp

#include <iostream>
#include <string>
#include "animal.h "

using namespace std;

int main(){
Lion myLion(20, "devil ");
// > > 由于我的函数名取为LionWho,所以此处也要改 < <
//myLion.TheWho();
myLion.LionWho();
return 0;
}

运行结果:
The Lion constructed

I am a Lion 'who()The animal 's name is devil
The animal 's weight is 20

热点排行