草庐IT

global_variables_initializer

全部标签

c++ - 如何编写正确的 std::initializer_list 构造函数

考虑以下代码:#include#includestructC{std::vectora;std::stringb;boolc;};voidprintC(constC&c){//...}intmain(){printC({{1,2,3},"ehlo",false});}这行得通,因为编译器可以为我生成合适的构造函数。但是,如果我将结构C更改为:structC{std::vectora;std::stringb;boolc;C(){c=false;}};printC调用停止工作,因为编译器停止生成适当的构造函数。我尝试使用std::initializer_list为自己编写一个构造函数,但

C++ : Initializing base class constant static variable with different value in derived class?

我有一个带有常量静态变量a的基类A。我需要类B的实例对静态变量a具有不同的值。这怎么能实现,最好是静态初始化?classA{public:staticconstinta;};constintA::a=1;classB:publicA{//???//Howtoset*a*toavaluespecifictoinstancesofclassB?}; 最佳答案 你不能。所有派生类共享一个静态变量实例。 关于C++:Initializingbaseclassconstantstaticvaria

将实例方法作为callback in Class Initializer中的回调

考虑以下代码:classBar{letcallback:()->()init(callback:@escaping()->()){self.callback=callback}funcevent(){self.callback()}}classFoo{letbar:Barinit(){self.bar=Bar(callback:self.handler)}funchandler(){print("Handled")}}基本想法是我们想要每个Foo有一个Bar,当event()被称为Foo'bar,将会通知Foo的处理程序方法。但是,上面的设置警告Foo自初始化,因为我们正在使用self在初始化

Qt : has initializer but incomplete type 中的 C++ 错误

voidFindWords::getTextFile(){QFilemyFile(":/FindingWords2.txt");myFile.open(QIODevice::ReadOnly);QTextStreamtextStream(&myFile);QStringline=textStream.readAll();myFile.close();ui->textEdit->setPlainText(line);QTextCursortextCursor=ui->textEdit->textCursor();textCursor.movePosition(QTextCursor::S

c++ - 错误 : initializer fails to determine size of ‘K’

我在行中收到错误“初始化器无法确定‘K’的大小”intK[]=newint[Vertices->total];如何解决? 最佳答案 改变intK[]=newint[Vertices->total];到int*K=newint[Vertices->total];第一个是Java创建数组的方法,其中K是对整数数组的引用。但是在C++中,我们需要让K成为一个指向整数类型的指针。 关于c++-错误:initializerfailstodeterminesizeof‘K’,我们在StackOver

c++ - 错误 : Variable length array of Non-POD element type 'string'

在开始之前,我必须首先声明,我已经研究过针对此错误的可能解决方案。不幸的是,它们都与不使用数组有关,这是我项目的要求。另外,我目前正在学习CS入门类(class),所以我的经验几乎没有。数组的用途是从文件中收集名称。因此,为了初始化数组,我计算了名称的数量并将其用作大小。问题是标题中所述的错误,但我仍然使用一维数组时看不到解决方法。主要.cpp#include#include#include#include#include#include"HomeworkGradeAnalysis.h"usingnamespacestd;intmain(){ifstreaminfile;ofstrea

c++ - 在 C++ 中声明 "static"变量时, "global"到底是什么意思?

这是对previousquestionofmine范围的扩展.“static”到底是什么,如何使用,在处理C++时使用“static”的目的是什么?谢谢。 最佳答案 这意味着该变量是翻译单元的本地变量(简单地说,是单个源文件),无法从外部访问。static的这种使用实际上在当前的C++标准中已被弃用-相反,您应该使用匿名namespace:staticintx=0;应该是:namespace{intx=0;} 关于c++-在C++中声明"static"变量时,"global"到底是什么意

c++ - std::condition_variable::wait_until 相对于 std::this_thread::sleep_for 有什么优势吗?

在时间等待场景中:oursoftwareworksinthebackground,andsynchronizesdatawiththeserverinevery20-30minutes.我想用std::this_thread::sleep_for但我的上级强烈反对任何形式的sleep功能。他推荐std::condition_variable::wait_until(lock,timeout-time,pred)不知道在这种情况下sleep_for有什么缺点吗? 最佳答案 正如评论中已经指出的那样,这仅取决于您的用例。两者之间的主要区

c++ - 在 vector 图上使用 initializer_list

我一直在尝试初始化>的map使用新的0X标准,但我似乎无法获得正确的语法。我想制作一个带有key:value=1:的单个条目的map#include#include#includeusingnamespacestd;map>A={1,{3,4}};....它在使用gcc4.4.3时出现以下错误:error:nomatchingfunctionforcalltostd::map>,std::less,std::allocator>>>>::map()编辑按照Cogwheel的建议并添加额外的大括号,它现在编译时带有警告,可以使用-fno-deduce-init-list标志消除该警告。这

c++ - constexpr(gcc) 错误 - 错误 : a brace-enclosed initializer is not allowed here before '{' token

structX{constexprstaticchara1[]="hello";//Okayconstexprstaticconstchar*a2[]={"hello"};//Error};intmain(){}用gcc编译报错:error:abrace-enclosedinitializerisnotallowedherebefore'{'token这是对constexpr的非法使用吗?编辑我尝试了3个不同版本的gcc,它是在我拥有的最新4.7.0上编译的(我刚刚下载了它,我使用的是mingw-w64),所以它看起来是一个固定的错误(链接到bug会很好!)。4.7.020120311