Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 没有对象无法调用成员函数,但我使用对象调用函数 #包括 #包括 使用名称空间std; 阶级人 { 私人: 字符串名; 智力年龄; 好友类实用程序; 公众: 人工(字符串输入名,int输入) { 名称=输入名称; 年龄=输入; } }; 类效用 { 公众: 无效显示年龄(常数人和人) { cout_C++ - Fatal编程技术网

C++ 没有对象无法调用成员函数,但我使用对象调用函数 #包括 #包括 使用名称空间std; 阶级人 { 私人: 字符串名; 智力年龄; 好友类实用程序; 公众: 人工(字符串输入名,int输入) { 名称=输入名称; 年龄=输入; } }; 类效用 { 公众: 无效显示年龄(常数人和人) { cout

C++ 没有对象无法调用成员函数,但我使用对象调用函数 #包括 #包括 使用名称空间std; 阶级人 { 私人: 字符串名; 智力年龄; 好友类实用程序; 公众: 人工(字符串输入名,int输入) { 名称=输入名称; 年龄=输入; } }; 类效用 { 公众: 无效显示年龄(常数人和人) { cout,c++,C++,Utility中的DisplayAge不是一个静态函数。因此需要一个Uitility的实例才能调用它 因此,要么将函数设置为静态的,要么通过匿名临时调用它 更好的方法是,将DisplayAge设为Human的成员函数,使用static关键字,然后就可以在类中调用函数 我在下面编辑了您的代码: Utility().DisplayAge(FirstMan); #包括 #包括 使用名称空间std; 阶级人 { 私人: 字符串名; 智力年龄; 好友类实用程序; 公众: 人工(字符串输入名,int输入)

Utility
中的
DisplayAge
不是一个
静态
函数。因此需要一个
Uitility
的实例才能调用它

因此,要么将函数
设置为静态的
,要么通过匿名临时调用它


更好的方法是,将
DisplayAge
设为
Human

的成员函数,使用
static
关键字,然后就可以在类中调用函数

我在下面编辑了您的代码:

Utility().DisplayAge(FirstMan);
#包括
#包括
使用名称空间std;
阶级人
{
私人:
字符串名;
智力年龄;
好友类实用程序;
公众:
人工(字符串输入名,int输入)
{
名称=输入名称;
年龄=输入;
}
};
类效用
{
朋友类人;
公众:
Utility()=默认值;
静态无效显示年龄(常数人和人)
{

无论何时定义函数,都不能使用类。请使用命名空间:

#include <iostream>
#include<string>
using namespace std;

class Human
{
private:
    string Name;
    int Age;
    friend class Utility;
public:
    Human(string InputName,int InputAge)
    {
        Name = InputName;
        Age = InputAge;
    }
};

class Utility
{
friend class Human;
public:
    Utility() = default;
    static void DisplayAge(const Human& Person)
    {
        cout<<Person.Age<<endl;
    }
};

int main(void)
{
    Human FirstMan("Adam",25);
    cout<<"Accessing private member Age via friend class: ";
    Utility::DisplayAge(FirstMan);
}
名称空间实用程序
{
内联无效显示年龄(常数人和人)
{

可以将其设置为静态。然后它将工作。
static void DisplayAge(const Human&Person)
。在实用程序类中,所有函数在大多数情况下都是静态的:)您没有使用对象调用函数。类名不是对象。请先创建对象:
utility myutility;
然后调用其函数:
myutility.DisplayAge(FirstMan);
。是的,你是对的。。谢谢:)
Age
是私有的,不作更多更改就无法访问。但是你不能
friend
一个名称空间;这在这里很重要。我不会使用我的答案,因为这通常是不好的做法。使用@Bathsheba中的答案
#include <iostream>
#include<string>
using namespace std;

class Human
{
private:
    string Name;
    int Age;
    friend class Utility;
public:
    Human(string InputName,int InputAge)
    {
        Name = InputName;
        Age = InputAge;
    }
};

class Utility
{
friend class Human;
public:
    Utility() = default;
    static void DisplayAge(const Human& Person)
    {
        cout<<Person.Age<<endl;
    }
};

int main(void)
{
    Human FirstMan("Adam",25);
    cout<<"Accessing private member Age via friend class: ";
    Utility::DisplayAge(FirstMan);
}
namespace Utility
{
    inline void DisplayAge(const Human& Person)
    {
        cout<<Person.Age<<endl;
    }
}

int main()
{
    Human FirstMan("Adam",25);
    cout<<"Accessing private member Age via friend class: ";
    Utility::DisplayAge(FirstMan);
}