草庐IT

member-initialization

全部标签

c++ - 使用 bind2nd() : "member function already defined or declared" instead of "reference to reference" 的奇怪编译器错误

我最近花了很多时间来理解在这段代码中调用func()时的错误消息:intmain(){vector>v;doublesum=0;for_each(v.begin(),v.end(),bind2nd(ptr_fun(func),&sum));return0;}当func()像这样声明时,代码编译正常:voidfunc(vectorv,double*sum){}当我使用这个声明(为了提高效率)时,我得到了一个编译器错误:voidfunc(constvector&v,double*sum){}我期望看到的错误类似于reference-to-reference错误,因为binder2nd的op

C++ 错误 : Member declaration not found

我是一个C++新手。今天遇到一个问题:在头文件中,我定义了一个类:templateclassPtr_to_const{private:Array_Data*ap;unsignedsub;public:...Ptr_to_const&operator=(constPtr_to_const&p);};在源文件中,我编程为:templatePtr_to_const&Ptr_to_const::operator=(constPtr_to_const&p){...return*this;}编译时,编译器总是说:'找不到成员声明'。为什么?我使用eclipseCDT+CygwinGCC非常感谢!

c++ - Clang claims that `member reference base type ' X' is not a structure or union`,但 X 是具有推导参数的结构模板

考虑以下代码:templatestructX{X(T){}voidfoo(){}};templatestructY{intobject=0;voidbar(){X(object).foo();}};Liveongcc.godbold.orgGCC8.2编译它,而Clang7吐出以下错误::13:18:error:memberreferencebasetype'X'isnotastructureorunionX(object).foo();~~~~~~~~~^~~~这对我来说像是一个错误。条件非常具体:如果任一结构不是模板,或者object不是成员变量,或者不涉及CTAD(类模板参数推导

c++ - "Member is private"虽然我不从外部访问它,但在使用尾随返回类型时

如何解决以下问题?我正在编写一些函数库,它定义了以下与这个问题相关的函数:call(f,arg):调用带有参数的函数。只是我在某些情况下需要的包装器。comp(f1,f2):返回两个函数的组合。返回表示两个函数组合的辅助仿函数。实现如下所示(简化版本仍能说明问题)://Callfwithoneargumenttemplateautocall(constFn&f,constArg&arg)->decltype(f(arg)){returnf(arg);}//HelperfunctorforthefunctionbelowtemplateclassCompFn{Fn1a;Fn2b;publ

c++ - 空括号调用默认构造函数还是采用 std::initializer_list 的构造函数?

以下引自EffectiveModernC++(第55页):"Supposethatyouuseanemptysetofbracestoconstructanobjectthatsupportsdefaultconstructorandalsosupportsstd::initializer_listconstruction.Whatdoyouremptybracesmean?etc.Theruleisthatyougetdefaultconstruction."我用std::array试过这个:std::arrayarr{};并收到来自g++(版本4.8.2)的警告:warning:m

c++ - std::make_unique(以及 emplace、emplace_back)对 initializer_list 参数的笨拙推导

假设我有这个结构:structposition{intx,y;};和另一个将this作为构造函数参数的类:classpositioned{public:positioned(positionp):pos(p){}private:positionpos;};我怎样才能得到简单的autobla=std::make_unique({1,2});上类?目前,编译器试图通过initializer_list匹配并调用make_unique的数组变体,这很愚蠢,因为positioned只有一个构造函数。emplace出现同样的问题和emplace_back功能。几乎所有将其可变模板参数转发给类的构造

c++ - std::initializer_list 和 c array [] 的优缺点是什么?

假设我有一些假设结构:structX{inti;doubled;}然后我可以写constexprXx_c_array[]{{5,6.3}};或constexprstd::initializer_listx_ilist{{5,6.3}};使用auto是不可能的-编译器必须知道内部类型。这两个版本都有缺点吗?更新:同样值得关注的是,您是否能够使用/将一种类型转换为另一种类型-例如。什么时候构建标准容器? 最佳答案 简单明了:initializer_list不是容器。它是对外部分配元素的不可变View。它完全不适合容器有用的任何场景——考

Docker Desktop 安装与无法运行(Docker Desktop failed to initialize)的诊断

1.DockerDesktop安装i.安装WSL首先要安装WSL,docker现在依赖于WSL才能在Windows上运行可以参照微软的官方教程https://docs.microsoft.com/en-us/windows/wsl/install注意WSL2是必须的,所以upgradeversion拿一本分不能跳过哈如果在安装WSL的过程中遇到了问题,可以参考我的另一篇文章https://blog.csdn.net/Antarctic_Bear/article/details/123489609ii.安装DockerDesktop理论上按照官方教程安装就可以了,第一次安装一般不会有什么问题的h

c++ - 使用以下 has_member 函数时 SFINAE 无法正常工作的原因是什么?

我正在尝试来自WalterBrown'sTMPtalk的示例我正试图让他的has_member实现正常工作。然而,该实现似乎错误地返回true,这让我相信我不理解SFINAE的一些细节。#include#includetemplateusingvoid_t=void;templatestructhas_type_member:std::false_type{};templatestructhas_type_member>:std::true_type{};structFooWithType{typedefinttype;};structFooNoType{};intmain(){std

c++ - 实现观察者模式时出现问题 : "Member reference base type ________ is not a structure or union"

我一直在实现准系统观察者模式,但遇到了一个有点神秘的错误:“成员引用基类型‘Observer*’不是结构或union”。我认为这与我对模板的使用有关,我对模板的使用仍然相当不舒服。这是有问题的代码(为了简化事情而删除了大多数缺点/析构函数):主题界面:classSubject{public:virtualvoidnotify();private:listm_observers;};主题实现:voidSubject::notify(){list::iteratori;for(i=m_observers.begin();i!=m_observers.end();i++){*i->updat