【问题标题】:Is it possible to define a friend function of a class with static members in C++?是否可以在 C++ 中定义具有静态成员的类的友元函数?
【发布时间】:2021-01-10 01:23:36
【问题描述】:

我正在尝试用静态成员声明一个类的友元函数。我在 Visual Studio 2017 中编译了我的程序并遇到了这个编译时错误:

无法解析的外部符号“private: static struct Number * user::Link”(?Link@user@@0PAUNumber@@A)

这是我的代码:

#include<iostream>
using namespace std;

typedef struct Number
{
    int number;
    struct Number *Link;
}
num_t;


class user
{
private:
    static num_t *Link;
    static int Length;

public:
    static void Create()
    {
        cout << "You called a function." << endl;
        Link->number = 1;
    }

    friend void Show_menu();
};

void Show_menu()
{
    user::Create();
}

int user::Length = 1;
num_t user::*Link = nullptr;

int main()
{

     return 0;
}

一般来说,是否可以在 C++ 中定义具有静态成员的类的友元函数?如果是这样,我该如何解决上述问题?

【问题讨论】:

    标签: c++11 static-members friend-function


    【解决方案1】:

    我想你想要num_t* user::Link = nullptr;。 该错误与声明的朋友无关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-27
      • 2018-12-15
      • 2013-12-31
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 2013-11-19
      相关资源
      最近更新 更多