草庐IT

work_base

全部标签

c++ - 如何在C++中实现继承并解决错误 "parent class is not accessible base of child class"?

我是C++新手。我喜欢探索C++中继承的概念。每当我尝试编译以下代码时,我都会收到错误消息:forC++includes,orinsteadofthedeprecatedheader.Todisablethiswarninguse-Wno-deprecated.D:\CPracticeFiles\Vehicle.cpp:Infunction`intmain()':D:\CPracticeFiles\Vehicle.cpp:26:error:`voidVehicle::setStationary_state(bool)'isinaccessibleD:\CPracticeFiles\Ve

c++: vector<Base> 可以包含 Derived 类型的对象吗?

标题几乎说明了一切。基本上,这样做是否合法:classBase{//stuff}classDerived:publicBase{//morestuff}vectorfoo;Derivedbar;foo.push_back(bar);根据我看过的其他帖子,下面是可以的,但我不想在这种情况下使用指针,因为很难使其线程安全。vectorfoo;Derived*bar=newDerived;foo.push_back(bar); 最佳答案 不,Derived对象将是sliced:所有额外的成员都将被丢弃。使用std::vector>而不是原

C++ 继承 : Calling Base Class Constructor In Header

假设类Child是类Parent的派生类。在一个五文件程序中,我如何在Child.h中指定我想调用Parent的构造函数?我认为header中的以下内容不合法:Child(intParam,intParamTwo):Parent(Param);在这种情况下,Child.cpp的构造函数语法应该是什么样的? 最佳答案 在Child.h中,您只需声明:Child(intParam,intParamTwo);在Child.cpp中,您将拥有:Child::Child(intParam,intParamTwo):Parent(Param){

c++ - 编写编译器 : how to get simple templates to work?

我有一种语法非常类似于C++的语言。词法分析器和解析器就位并产生正确的AST。大部分后端也已完成。编译器用来创建类型的基本系统非常简单:所有类型都被认为是内置的,所有实例都是全局的。所以只有一个简单的映射,它将类型名称与创建Variable的方法相匹配,该Variable基本上是像boost::any这样的通用类型。另一个以变量名作为键,变量作为值的映射作为全局范围:std::maptypeList;//registersometypestypeList["X"]=Variable::Create;typeList["Y"]=CreateInstanceOfY;....当编译器获取用于

c++ - 为什么在异常掩码未设置为 eofbit 时 getline() 抛出 'std::ios_base::failure'?

考虑以下代码:ifstreamin;try{in.exceptions(ifstream::failbit|ifstream::badbit);in.open(pConfLocation);}catch(ifstream::failuree){throwstd::runtime_error("Can'topenconfigurationfile\n");}vectorlns;strings;in.clear();while(!in.eof()){getline(in,s);boost::algorithm::trim(s);lns.push_back(s+='\n');}所以:我根据t

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++ - 为什么 std::ofstream 在没有 std::ios_base::trunc 的情况下截断?

这个问题在这里已经有了答案:Doesstd::ofstreamtruncateorappendbydefault?(1个回答)关闭2年前。根据此C++引用:http://www.cplusplus.com/reference/fstream/ofstream/ofstream/,std::ofstream的默认打开模式是ios_base::out并且它没有提到隐含的其他模式。因此,我希望如果我用一个小文件覆盖一个大文件,大文件的“超出”部分应该保持不变,只有文件的第一部分应该被新的、更短的数据替换。另一方面,ApacheC++标准库用户指南(http://stdcxx.apache.o

C++ 在迭代 : standard solution is not working? 时删除列表成员

这是我的问题。我已经阅读了许多以前关于如何在迭代列表成员时删除列表成员的问题,并且我尝试了答案提出的各种解决方案。碰巧他们似乎不起作用。我有一个此类的列表:classWalker{public:Walker(int);~Walker();double*x;double*y;double*z;doubleweight;intmolteplicity;};构造函数和析构函数如下Walker::Walker(intparticle_num){x=newdouble[particle_num];y=newdouble[particle_num];z=newdouble[particle_num

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

C++ Lambda、捕获、智能 Ptr 和堆栈 : Why Does this Work?

我一直在研究C++11中的一些新特性,并尝试编写以下程序,但预计它不会运行。令我惊讶的是,它确实如此(在带有'std=c++0x'标志的Linuxx86上的GCC4.6.1上):#include#include#includestd::functioncount_up_in_2s(constintfrom){std::shared_ptrfrom_ref(newint(from));return[from_ref](){return*from_ref+=2;};}intmain(){autoiter_1=count_up_in_2s(5);autoiter_2=count_up_in_