C++ C++;超载<&书信电报;(非静态成员的使用无效)

C++ C++;超载<&书信电报;(非静态成员的使用无效),c++,overloading,C++,Overloading,问题是我希望我的程序的输出使用 Ackermann<M,N>::wartosc; 阿克曼:瓦托斯; 我想通过重载操作符0{返回计数(m-1,计数(m,n-1));} 返回-1; } 公众: 阿克曼() { n0=N; m0=M; } int wartosc() { 返回计数(m0、n0); } 模板 friend ostream&operator您需要在实例s上调用成员函数: return output << s.wartosc()

问题是我希望我的程序的输出使用

Ackermann<M,N>::wartosc;
阿克曼:瓦托斯; 我想通过重载操作符0{返回计数(m-1,计数(m,n-1));} 返回-1; } 公众: 阿克曼() { n0=N; m0=M; } int wartosc() { 返回计数(m0、n0); } 模板
friend ostream&operator您需要在实例
s
上调用成员函数:

return output << s.wartosc();
//                        ^^

您需要在实例
s
上调用成员函数:

return output << s.wartosc();
//                        ^^

您的问题似乎就在这里:
Ackermann::wartosc

您将其视为静态方法。改为使用传入的
s
参数

template<int M, int N>
ostream & operator<< (ostream &output, const Ackermann<M,N> &s)         
{
    return output << s.wartosc;
}
模板

ostream&operator您的问题似乎在这里:
Ackermann::wartosc

您将其视为静态方法。改为使用传入的
s
参数

template<int M, int N>
ostream & operator<< (ostream &output, const Ackermann<M,N> &s)         
{
    return output << s.wartosc;
}
模板

ostream&operator您需要调用成员函数,最好是在实例上

return output << s.wartosc();

返回输出您需要调用成员函数,最好是在实例上

return output << s.wartosc();

返回输出我发现clang++错误消息更容易理解:

ack.cpp:37:38: error: call to non-static member function without an object
      argument
    return output << Ackermann<M,N>::wartosc;
                     ~~~~~~~~~~~~~~~~^~~~~~~
ack.cpp:37:38:错误:在没有对象的情况下调用非静态成员函数
论点

返回输出我发现clang++错误消息更容易理解:

ack.cpp:37:38: error: call to non-static member function without an object
      argument
    return output << Ackermann<M,N>::wartosc;
                     ~~~~~~~~~~~~~~~~^~~~~~~
ack.cpp:37:38:错误:在没有对象的情况下调用非静态成员函数
论点

return output>return output ack.cpp:38:29:错误:将'const Ackermann'作为'int-Ackermann::wartosc()[with int M=3,int N=3]'的'this'参数传递会丢弃限定符[-fppermissive],但有没有办法使用>Ackermann::wartosc?当我使用const时它会起作用。但这是使用Ackermann::wartosc的一种方式吗?@user3009072有很多种方式,但很复杂。你为什么要这样做?为了学习和学校。我的老师想按我说的那样使用它。如果您现在还不能解释,请说出来,因为您已经解决了我的问题,但使用了其他方法。@user3009072类似的内容:
s.Ackermann::wartosc()
。基本上,您从中什么也得不到。>返回输出ack.cpp:38:29:错误:将'const Ackermann'作为'int Ackermann::wartosc()[with int M=3,int N=3]'的'this'参数传递会丢弃限定符[-fpermissive],但是有没有办法使用>Ackermann::wartosc?当我使用const时它会起作用。但这是使用Ackermann::wartosc的一种方式吗?@user3009072有很多种方式,但很复杂。你为什么要这样做?为了学习和学校。我的老师想按我说的那样使用它。如果您现在还不能解释,请说出来,因为您已经解决了我的问题,但使用了其他方法。@user3009072类似的内容:
s.Ackermann::wartosc()
。基本上,你从中一无所获。
return output << s.wartosc;