标题:初学者。。。求罩。。非静态成员函数的非法调用???
只看楼主
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
得分:10 
以下是引用gwcome在2013-3-17 15:02:54的发言:

//自己写着玩的,是关于友元函数
#include<iostream>
class Lovers
{
   
public:
    void kiss_action();
protected:
    void kiss();
private:
   
};

class Girlfriend :public Lovers
{
friend class Others;   
public:
    void kiss_action();
protected:
    void kiss();
   
private:
   
};

class Boyfriend :public Lovers
{
public:
    void kiss_action();
protected:
    void kiss();
private:
};

class Others
{
   
public:
    void kiss_other();
protected:
   
private:
};

void Lovers::kiss()
{
    std::cout<<"I want kiss";
}

void Lovers::kiss_action()
{
    Lovers::kiss();
}

void Girlfriend::kiss()
{
    Lovers::kiss_action();
    std::cout<<"my girlfriend  I am a normal boy";
}

void Girlfriend::kiss_action()
{
    Girlfriend::kiss();
}

void Boyfriend::kiss()
{
    Lovers::kiss_action();
    std::cout<<"my boyfriend  I am a gayboy";
}

void Boyfriend::kiss_action()
{
    Boyfriend::kiss();
}

void Others::kiss_other()
{
    Girlfriend::kiss();//“Girlfriend::kiss_action”: 非静态成员函数的非法调用
    std::cout<<"I am a third..hehe";
}
int main()
{
    Lovers lovers;//定义对象 ...TAT...
    lovers.kiss_action();
    Girlfriend girlfriend;
    girlfriend.kiss_action();
    Boyfriend boyfriend;
    boyfriend.kiss_action();
    Others others;
    others.kiss_other();
    return 0;
}
我定义了对象  还是有错

程序代码:
#include "stdafx.h"
//自己写着玩的,是关于友元函数
#include<iostream>
class Lovers
{

public:
    void kiss_action();
protected:
    void kiss();
private:

};

class Girlfriend :public Lovers
{
    friend class Others;    
public:
    void kiss_action();
protected:
    void kiss();

private:

};

class Boyfriend :public Lovers
{
public:
    void kiss_action();
protected:
    void kiss();
private:
};

class Others
{

public:
    void kiss_other();
protected:

private:
};

void Lovers::kiss()
{
    std::cout<<"I want kiss";
}

void Lovers::kiss_action()
{
    Lovers::kiss();
}

void Girlfriend::kiss()
{
    Lovers::kiss_action();
    std::cout<<"my girlfriend  I am a normal boy";
}

void Girlfriend::kiss_action()
{
    Girlfriend::kiss();
}

void Boyfriend::kiss()
{
    Lovers::kiss_action();
    std::cout<<"my boyfriend  I am a gayboy";
}

void Boyfriend::kiss_action()
{
    Boyfriend::kiss();
}

void Others::kiss_other()
{
    Girlfriend girl;
    girl.kiss();
    std::cout<<"I am a third..hehe";
}
int main()
{
    Lovers lovers;//定义对象 ...TAT...
    lovers.kiss_action();
    Girlfriend girlfriend;
    girlfriend.kiss_action();
    Boyfriend boyfriend;
    boyfriend.kiss_action();
    Others others;
    others.kiss_other();
    return 0;
}

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-03-18 12:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-401071-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057056 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved