草庐IT

c++ - 为什么这个友元函数不能访问私有(private)变量?

classStudent{public:Student(inttest):key(705){if(test==key){cout>testkey;Studentbob(testkey);printResult();}函数printResult似乎无法从Student类访问私有(private)变量allow。我是在错误的地方制作了printResult原型(prototype)还是语法错误?据我所知,我们可以在类里面的任何地方为friend制作原型(prototype)。 最佳答案 printResult不是成员函数,所以你需要给它

c++ - 如何解决 "class must be used when declaring a friend"错误?

classtwo;classone{inta;public:one(){a=8;}friendtwo;};classtwo{public:two(){}two(onei){cout我从Dev-C++收到此错误:aclass-keymustbeusedwhendeclaringafriend但是用MicrosoftVisualC++编译器编译时它运行良好。 最佳答案 你需要friendclasstwo;代替friendtwo;此外,您不需要单独转发声明您的类,因为友元声明本身就是一个声明。你甚至可以这样做://noforward-de

c++ - 如何调用类中定义的友元模板函数?

我从书中得到了这个例子,但我不知道如何实际调用票证功能。这是代码:#includeclassManager{public:templatefriendintticket(){return++Manager::counter;}staticintcounter;};intmain(){Managerm;std::cout()我收到“候选函数不可访问”错误消息。 最佳答案 几点可以帮助您弄清楚这里发生了什么:I)类内的友元函数定义只能在从类定义外部调用时通过参数相关查找找到。II)提供显式模板参数的函数模板不会进行ADL,除非编译器得到

c++ - 与模板特化成为 friend 时可能出现 gcc 错误

在回答关于SO的另一个问题时,我遇到了一个有点可疑的gcc编译器错误。有问题的片段是templateclassA;templatevoidoperator*(A,A);templateclassA{friendvoid::operator*(A,A);...最后一行给出了著名的警告frienddeclaration'voidoperator*(A,A)'declaresanon-templatefunction稍后会导致硬错误。完整代码可见here.现在,问题是我认为这种行为不合适。[temp.friend]/1中的标准说:Forafriendfunctiondeclarationth

c++ - 来自不同命名空间的模板模板参数可以成为 friend 吗?

如果这个问题的标题没有帮助,我深表歉意;如果不给出以下示例,我不知道如何简洁地提出这个问题:templateclassArg>classC{typedefCtype;friendclassArg;public:C(){a_.set(this);}private:inti_;Arga_;};templateclassArg1{public:voidset(Type*t){t_=t;t_->i_=1;}private:Type*t_;};namespaceNS{templateclassArg2{public:voidset(Type*t){t_=t;t_->i_=2;}private:T

c++ - g++ 错误 : specialization after instantiation (template class as friend)

考虑以下C++代码:templateclassSingleton{};classConcreteSingleton:publicSingleton{templatefriendclassSingleton;};intmain(){}Singleton应该是ConcreteSingleton的friend:它适用于Microsoft的可视化C++编译器。但是,我不能用g++4.8.4编译它。错误是:error:specializationof‘Singleton’afterinstantiationtemplatefriendclassSingleton;有什么办法可以解决吗?

c++ - 私有(private)函数作为其他类的 friend

我有以下用C++编写的代码:#includeusingnamespacestd;classWindow;classLevel{intlevel;intget(Window&w);public:Level(void):level(3){}voidshow(Window&w);};voidLevel::show(Window&w){cout我想访问类Window的私有(private)成员,只能通过友元函数get访问,这也是类Level的私有(private)函数.当我尝试编译时出现错误C2248。是否可以将私有(private)函数设为其他类的友元? 最佳答案

c++ - 显式特化不能是友元声明

代码templatevoidfoo(constT&t){}templateclassA{templatefriendvoidfoo(constT&t){}};给出编译错误"definingexplicitspecialization‘foo’infrienddeclarationfriendvoidfoo(constT&t)"用gcc编译时"errorC3637:'A::foo':afriendfunctiondefinitioncannotbeaspecializationofaunctiontemplate"在VS2013中编译时我知道标准是这样说的,但为什么呢?我想了解原因(幕后

c++ - 关于来自 VC12 和 VC14 的 c++ 友元和继承的不同行为

classBase{protected:voidfunc1();};classDerived:publicBase{friendclassThird;};classThird{voidfoo(){Derived;d.func1();}};我可以在VC14(VisualStudio2015)中编译代码而不会出错但从VC12(VisualStudio2013)得到错误cannotaccessprotectedmemberdeclaredinclass'Base'谁是对的?这种具有继承性的友元的正确性是什么?来自MSDNhttps://msdn.microsoft.com/en-us/lib

c++ - 同级友元运算符似乎不参与重载决议

在编写使类能够根据模板参数为operator+提供重载的CRTP模板时,我发现类内友元运算符似乎不参与重载决议,如果没有的话arguments是它在其中定义的类的类型。归结:enumclassFooValueT{zero,one,two};classFoo{FooValueTval_;public:Foo(FooValueTx):val_(x){};Foo&operator+=(Fooother){val_=(FooValueT)((int)val_+(int)other.val_);return*this;}//overloadforFoo+Foo,FooValueT+FooandF