草庐IT

Non-declaration

全部标签

c++ - 为什么 gcc 提示 "declaration of ' foo' 隐藏了之前的调用 [-Werror=shadow]”

我有这个MCVE:autobar()->double{return8.0;}intmain(){if(autofoo=bar()){returnfoo;}elseif(autofoo=bar()){returnfoo;}}使用gcc7.3和这些选项-c-Werror-Wextra-Wall-Wshadow编译它会生成以下错误消息:test-shadow.cpp:Infunction‘intmain()’:test-shadow.cpp:9:17:error:declarationof‘foo’shadowsapreviouslocal[-Werror=shadow]elseif(aut

c++ - 错误 : out-of-line definition of 'test' does not match any declaration in 'B<dim>'

我有一个小问题让我很烦!!我不知道下面的代码似乎有什么问题。我应该能够实现从父类(superclass)继承的功能,不是吗?但我得到error:out-of-linedefinitionof'test'doesnotmatchanydeclarationin'B'templateclassA{public:virtualdoubletest()const;};templateclassB:publicA{};templatedoubleB::test()const{return0;}我在Mac上使用clang(AppleLLVM5.1版)。 最佳答案

c++ - 为什么不能实例化带有 "non const"复制构造函数的对,而没有实例化一对是可能的?

假设您有以下类(class):structA{A(){}A(A&)=delete;};intmain(){std::pairp1;return0;}以下代码将无法编译(使用-std=c++11和g++)并出现以下错误:/usr/include/c++/5/bits/stl_pair.h:Ininstantiationof‘structstd::pair’:test.cpp:13:23:requiredfromhere/usr/include/c++/5/bits/stl_pair.h:127:17:error:‘constexprstd::pair::pair(conststd::pa

c++ - 错误 C2248 : 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

收到此错误,我很确定它在operatorvoidCRational::print()const{print(cout);}voidCRational::print(ostream&sout)const{if(m_denominator==1)cout 最佳答案 您需要通过引用而不是值返回ostream。它试图调用构造函数。也可以传递'a'作为引用:ostream&operator我还注意到打印方法可能是错误的。它有sout作为流的名称传递,但随后直接使用cout实现。应该是voidCRational::print(ostream&s

c++ - 错误 : invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘std::vector<bool>::reference {aka std::_Bit_reference}’

为什么我会收到错误:从类型为“std::vector::reference{akastd::_Bit_reference}”的右值对类型为“bool&”的非常量引用进行无效初始化?vector>vis;bool&visited(intx,inty){returnvis[x][y];//error}据我所知,vector中的operator[]返回引用,所以它应该是一个左值,但它不起作用。我应该怎么做才能让它发挥作用? 最佳答案 那是因为std::vector不是它看起来的样子。std::vector有一个特化与类型bool-它是空间

c++ - "a declaration shadows a parameter"是什么意思?

我正在尝试创建一个函数,该函数返回我将传递给它的整数的两倍。我的代码收到以下错误消息:declarationof'intx'shadowsaparameterintx;"这是我的代码:#includeintdoublenumber();usingnamespacestd;intdoublenumber(intx)//>a;doublenumber(a);return0;} 最佳答案 您将x作为参数,然后尝试将其也声明为局部变量,这就是对“阴影”的提示。 关于c++-"adeclarati

C++ : Why cant static functions be declared as const or volatile or const volatile

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:C++-Whystaticmemberfunctioncan’tbecreatedwith‘const’qualifier想知道为什么静态成员函数不能声明为const或volatile或constvolatile的原因?#includeclassTest{staticvoidfun()const{//compilererrorreturn;}};

c++ - 将 C++ 与 Objective-C 结合使用,如何修复 "Conflicting declaration ' typedef int BOOL'"?

我有很多C++代码,最初是在PC上构建的。我试图让它在Mac上与Objective-C一起工作。为此,我创建了一个Objective-C框架来容纳C++代码并添加了一个瘦包装器。但是我在我的C++代码中遇到了typedef问题。当我在PC上使用C++时,我使用了WinDef.h中定义的BOOL变量。因此,当我在Mac上移动所有内容时,我添加了typedefintBOOL;以确保BOOL变量仍会按预期编译。但是当我尝试编译时出现错误:“Conflictingdeclaration'typedefintBOOL'”。我假设这是因为BOOL是Objective-C中的关键字,因此已经定义了。

c++ - 铿锵错误 : non-type template argument refers to function that does not have linkage -- bug?

我有一些非常简单的(C++11)代码,最新的clang(version3.4trunk187493)无法编译,但GCC编译正常。代码(下面)实例化函数模板foo使用局部函数类型Bar然后尝试将其地址用作类模板Func的非类型模板参数:templatestructFunc{};templateexterninlinevoidfoo(){usingFoo=Func>;}intmain(){structBar{};//function-localtypefoo();return0;}clang发出以下错误:error:non-typetemplateargumentreferstofunct

c++ - 重载运算符 : const vs non-const return type : any difference of performance?

如果我们去维基百科article关于C++运算符,我们有一个例子:Addition:a+b->TT::operator+(constT&b)const;因此运算符返回类型为T的非常量。如果我们看这个guideline作者说返回类型应该是const以避免以下语法:(a+b)=c现在假设我不介意这种语法,并考虑a和b是大数组。从“纯”性能的角度来看,返回类型中缺少const关键字是否会阻止编译器的优化(g++和带有-O3的英特尔icpc)?如果答案是"is",为什么? 最佳答案 这是一个有趣的问题。在C++03中,没有更好的机会使用这两