草庐IT

member-variables

全部标签

c++ - 错误 : ISO C++ forbids in-class initialization of non-const static member

这是头文件:employee.h#ifndefEMPLOYEE_H#defineEMPLOYEE_H#include#includeusingnamespacestd;classEmployee{public:Employee(conststring&first,conststring&last)重载的构造函数:firstName(first),firstName重载构造函数lastName(last)lastName重载构造函数{//Theconstructorstart++counter;它为每个创建的对象加一;cout析构函数cout返回每个对象的名字和姓氏--counter;计

c++ - 错误 : 'sort' is not a member of 'std'

我只是想问一下这个错误是什么意思以及如何解决它谢谢!error:'sort'isnotamemberof'std'vectorresult;for(auto&i:numbers)result.push_back(std::stoi(i));std::sort(result.begin(),result.end()); 最佳答案 包括算法头。这就是出现错误的原因。#include 关于c++-错误:'sort'isnotamemberof'std',我们在StackOverflow上找到一

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++ - 这个 "if e is a pack, then get a template name, otherwise get a variable name"是否有效?

我尝试构建一个不需要typename或template的案例,但仍会根据给定名称t生成变量或模板是否为函数参数包templatestructA{templatestaticvoidf(int){}};templatestructA{staticconstintf=0;};templateusingtype=int;templatevoidf(Tt){A...)>::f(1);}intmain(){f(1);}以上将引用staticconstint,并进行比较。以下刚好有Tt变成了一个包并制作f引用模板,但GCC也不喜欢templatevoidf(T...t){A...)>::f(1);

c++ - 嵌套类 : Access to protected member of enclosing class from a nested protected class

此代码在msvc/g++上编译:classA{protected:inti;classB{public:A*a;B(A*a_):a(a_){}voiddoSomething(){if(a)a->i=0;//如您所见,B可以访问封闭类的“protected”部分,尽管它没有被声明为友元。这是一种标准(符合标准的)行为吗?我有时会使用此功能,但我不记得有一条规则说嵌套的protected类应该自动访问封闭类的所有protected数据。 最佳答案 在C++03标准中,11.8p1说:Themembersofanestedclasshav

c++ - 智能感知 : the object has type qualifiers that are not compatible with the member function

我有一个名为Person的类:classPerson{stringname;longscore;public:Person(stringname="",longscore=0);voidsetName(stringname);voidsetScore(longscore);stringgetName();longgetScore();};在另一个类(class),我有这个方法:voidprint()const{for(inti=0;i这是人的声明:staticconstintsize=8;Personpeople[size];当我尝试编译它时,我得到了这个错误:IntelliSense

c++ - 初始化引用 - 警告 C4355 : 'this' : used in base member initializer list

classA;classB{public:B(A&a):a(a){}private:A&a;};/*Method1*//*warningC4355:'this':usedinbasememberinitializerlist*//*classA{public:A():b(*this){}private:Bb;};*//*Method2*//*ButIneedtomanuallyperformmemorydellocation.*/classA{public:A(){b=newB(*this);}~A(){deleteb;}private:B*b;};intmain(){}目前,当我尝试

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

c++ - 警告 C4996 : This function or variable may be unsafe -- compared to GCC on POSIX

我注意到MS编译器会为cstdlib函数(如getenv)发出“已弃用”警告。MS发明了自己的标准,例如_dupenv_s。问题1AFAIK主要的“不安全”事情是关于重入*。既然MS的CRT被标记为“多线程”(/MT),他们为什么不直接将getenv替换为可重入的线程安全版本呢?是否有人会依赖不安全的行为?问题2我用GCCg++-Wall-Wextra-Weff++-pedanticfoo.cpp编译了相同的代码,它不会产生任何警告。所以我想这在POSIX上不是问题吗?这是如何解决的?(好吧,也许他们只是改变了getenv的行为,很高兴能得到确认。*说它只是关于可重入性是一种过度概括。

c++ - 编译时出现错误 "fopen' : This function or variable may be unsafe.

这个问题在这里已经有了答案:fopendeprecatedwarning(10个回答)关闭8年前。编译时收到此错误:'fopen':Thisfunctionorvariablemaybeunsafe.Considerusingfopen_sinstead.Todisabledeprecation,use_CRT_SECURE_NO_WARNINGS.我是C++新手并打开CV,因此请帮助我摆脱这个错误。谢谢void_setDestination(constchar*name){if(name==NULL){stream=stdout;}else{stream=fopen(name,"w"