草庐IT

friends_checked

全部标签

c++ - friend 类的部分模板特化?

我有一个简单的类X和一组模板化类Y。我希望第一个模板化参数恰好是X的所有类Y都成为X本身的friend。下面希望传达我想要的,但是friend语句给出了编译错误。templateclassY{};classX{public:X(intvalue):i(value){}constint&getI()const{returni;}private:inti;templatefriendclassY;};我不确定完全允许友元语句模板化(更不用说友元语句的部分模板化了)。有没有办法做到这一点?还是我无法一一列出所有friend?谢谢,马特 最佳答案

c++ - 限制类的模板 friend

考虑以下代码:#includeclassS{staticconstinti=42;templatefriendvoidf();};templatevoidf(){std::cout();f();}我在这里只想允许访问类的私有(private)部分S至f,但不适用于f.IE。我想得到类似'i'isaprivatememberof'S'的编译器错误对于f()行。如何实现? 最佳答案 模板实例化是一个函数,所以命名就可以了:voidf().不过,您需要事先声明:[C++03:11.4/9|C++11/C++14:11.3/11]:Ifaf

C++ : friend declaration ‘declares a non-template function

我在重载时遇到问题流运算符(operator),我找不到解决方案:templateclassNVector{inlinefriendstd::ostream&operator&rhs);};templateinlinestd::ostream&NVector::operator&rhs){/*SOMETHING*/returnlhs;};它产生以下错误信息:warning:frienddeclaration‘std::ostream&operatorerror:‘std::ostream&NVector::operator如何解决这个问题?非常感谢。 最佳答

c++ - 使用 C++ 对象 Q_PROPERTY 绑定(bind)复选框 'checked' 属性

我正在学习QtQuick,并且正在研究C++类和QML属性之间的数据绑定(bind)。在我的C++对象模型中,我有两个属性:Q_PROPERTY(QStringnameREADgetNameWRITEsetNameNOTIFYnameChanged)Q_PROPERTY(boolstatusREADgetStatusWRITEsetStatusNOTIFYstatusChanged)在我的.qml文件中:TextEdit{placeholderText:"Enteryourname"text:user.name}Checkbox{checked:user.status}当我从我的C++

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++ - 与模板特化成为 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++ - BOOST_CHECK_EQUAL(和衍生品)添加自定义消息

我们最近开始使用BoostTest框架,到目前为止很喜欢它。但是,在某些测试中,如果我们可以将自定义消息添加到现有帮助程序,那将会很棒。例如,我可以在mytest和mytest2中获取输出,但在mytest3中找不到输出:#defineBOOST_TEST_MODULEmytests#includeBOOST_AUTO_TEST_SUITE(myunit)BOOST_AUTO_TEST_CASE(mytest){//Thisgiveaniceoutput[2+2!=5]BOOST_CHECK_EQUAL(2+2,5);}BOOST_AUTO_TEST_CASE(mytest2){//T