草庐IT

as-needed

全部标签

c++ - "local variables at the outermost scope of the function may not use the same name as any parameter"是什么意思?

我一直在阅读C++入门第5版。在第6.1章功能参数列表的第三段中。它写道“此外,函数最外层范围内的局部变量不得使用与任何参数相同的名称”。什么意思?我不是以英语为母语的人。我不明白函数的“最外层范围”的实际含义。 最佳答案 函数的最外层是定义函数体的block。您可以将其他(内部)block放入其中,并在该block的本地变量中声明变量。内部block中的变量可以与外部block中的变量或函数参数具有相同的名称;他们将名称隐藏在外部范围内。外部block中的变量不能与函数参数同名。演示:voidf(inta)//functionha

c++ - MinGW: "gcc is not recognized as an internal or external command"

我下载并安装了MinGW。我使用图形程序安装C++编译器。在Windows命令行中键入gcc会打印:gccisnotrecognizedasaninternalorexternalcommand我检查了,gcc.exe存在于C:\MinGW\bin中。怎么了? 最佳答案 虽然是一个老问题,但这里的答案都没有帮助我。我发现到达目的地的唯一路线是在命令提示符下输入以下行:复制:设置Path=C:\MinGW\bin;%PATH%之后,只需输入gcc-v。希望这对解决我遇到的问题的人有所帮助!

c++ - 错误 : passing 'const …' as 'this' argument of '…' discards qualifiers

error:passing'constA'as'this'argumentof'voidA::hi()'discardsqualifiers[-fpermissive]我不明白为什么会出现这个错误,我没有返回任何东西,只是传递了对象的引用,就是这样。#includeclassA{public:voidhi(){std::cout@edit我使用const正确性修复了它,但现在我试图在同一个方法中调用方法,我得到了同样的错误,但奇怪的是我没有传递对这个方法的引用。#includeclassA{public:voidsayhi()const{hello();world();}voidhel

C++ 模板类特化 : why do common methods need to be re-implemented

在示例中:#includeusingnamespacestd;classB{public:virtualvoidpvf()=0;};templateclassD:publicB{public:D(){}virtualvoidpvf(){}private:stringdata;};templateclassD:publicB{public:D();virtualvoidpvf(){coutd1;Dd2;}我收到以下错误:test.cpp:(.text+0x1c):undefinedreferenceto`D::D()'请注意,我不只是专门化D()本身的原因是我想消除对字符串D::data

c++ - C++ 中的 "As a rule of thumb, make all your methods virtual"- 合理的建议?

我只是偶然看到了标题中的陈述。完整的报价是:Asaruleofthumb,makeallyourmethodsvirtual(includingthedestructor,butnotconstructors)toavoidproblemsassociatedwithomissionofthevirtualkeyword.我在Wrox的书ProfessionalC++中找到了这个。Youcangoogleittocheck.这有什么关系吗?我原以为您只会提供选择的扩展点,而不是默认的可扩展性。例如,a2001articlebyHerbSuttersaysso.从那以后,有什么发生了巨大

c++ - "cannot be used as a function error"

我正在编写一个使用不同.cpp文件中的函数的简单程序。我所有的原型(prototype)都包含在一个头文件中。我将一些函数传递给其他函数,但不确定我是否正确执行。我得到的错误是“'functionname'不能用作函数”。它说不能使用的函数是growthRate函数和estimatedPopulation函数。数据通过输入函数输入(我认为这是有效的)。谢谢!头文件:#ifndefheader_h#defineheader_h#include#include#includeusingnamespacestd;//prototypesvoidexterninput(int&,float&,

C++ 错误 - "member initializer expression list treated as compound expression"

我遇到了一个我不熟悉的C++编译器错误。可能是一个非常愚蠢的错误,但我不能完全指出它。错误:test.cpp:27:error:memberinitializerexpressionlisttreatedascompoundexpressiontest.cpp:27:warning:left-handoperandofcommahasnoeffecttest.cpp:27:error:invalidinitializationofreferenceoftype‘constBar&’fromexpressionoftype‘int’代码:1#include23classFoo{4publ

c++ - 为什么 as_const 禁止右值参数?

我想问为什么as_const禁止右值参数,accordingtocppreference.com(即为什么标准人员这样做,而不是为什么cppreference.com专门引用它们。而且也不是在哪里规范委员会的意图是编纂的,只是为了确保:)))。这个(人工)示例会产生错误(用户希望将其设为const以保持COW安静)QCharc=as_const(getQString())[0];Anotherquestion'sanswer指出,如果我们只是删除右值引用重载的删除,它会默默地将右值转换为左值。对,但为什么不优雅地处理右值,为右值输入返回constrvalues,为左值输入返回const

c++ - 如果不这样做,函数如何在新线程上运行 "as if"?

根据C++标准的[futures.async]/3项目符号1,当函数f使用std传递给std::async时::launch::async启动策略,f将“就像在新的执行线程中一样”运行。鉴于f可以做任何事情,包括无限循环和永远阻塞,实现如何提供f在其自己的线程上运行而不实际运行它的行为它自己的线程?也就是说,实现如何利用标准提供的“好像”摆动空间? 最佳答案 查看C++引用中出现的here和here,似乎“好像”的目标是让库实现者有一定程度的自由度。例如,它说asifspawnedbystd::thread(std::forward

c++ - "Default member initializer needed within definition of enclosing class outside of member functions"- 我的代码格式不正确吗?

#includestructfoo{intx{0};foo()noexcept=default;voidf()noexcept(noexcept(std::declval())){}};intmain(){}liveexampleongodbolt上面的代码可以用我测试过的任何版本的g++,以及3.6到3.9.1的clang++编译,但是不能用clang++4.0.0编译:test.cpp:6:5:error:defaultmemberinitializerfor'x'neededwithindefinitionofenclosingclass'foo'outsideofmemberf