草庐IT

MAX_NUM_IMAGES_PER_CLASS

全部标签

c++ - 我如何让 main 成为我类(class)的 friend ?

这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭11年前。我认为这是可能的,但编译器提示它无法访问我的类的protected/私有(private)成员。我试过四处移动东西并更改签名,但找不到有效的组合。我基本上有:classMyClass{public:friendintmain(intargc,char**argv);private:voidtest(){cout

c++ - 错误 : no type named ‘value_type’ in ‘class

我收到以下编译器错误:(graph_algorithms.h:59:111:error:notypenamed‘value_type’in‘classGraph::graph_algorithms::vertex_comparer’)这是什么意思?下一行给了我一大堆编译器错误std::priority_queue::vertex,typenameGraph::graph_algorithms::vertex_comparer>pri_que;#ifndefGRAPH_ALGORIUHMS_H_#defineGRAPH_ALGORIUHMS_H_#include#include"grap

c++ - 从 vector C++ 中的父类(super class)调用子类方法

我正在尝试将一些子类元素添加到一个vector中,遍历它们调用一个被覆盖的方法,并希望它在可能的情况下调用被覆盖的方法。但是我发现它似乎只调用父类(superclass)方法。我学过Java,但不确定为什么要在C++中这样做。我尝试使用父类(superclass)的指针vector重写代码并将子类的指针转换到父类(superclass)。然后通过指针访问它。理想情况下,我不想将指针列表放入vector中,因为我必须手动删除每个指针(我相信?)以阻止内存泄漏,因为我将使用new创建对象,以便它们持续到方法调用以将它们添加到vector中。有没有更好的方法来做到这一点,或者我是否坚持使用指

c++ - 构造函数执行顺序/顺序 : dependent initialization of static variable (class instance) in a function

对于以下代码段:classBar{public:intx;inty;Bar(int_x,int_y){/*somecodeshere*/...}};classFoo{public:intx;inty;intz;Foo(Barb):x(b.x),y(b.y){z=someFunction(x,y);}};voidf(intx,inty){Barb(x,y);staticFoox(b);}intmain(){f(2,3);}在我看来,函数内的静态变量甚至应该在main()之前初始化。但是,Foo类型的静态变量x依赖于Bar类型的局部变量b。问题是:1)x的构造函数什么时候执行?即x是在第一

c++ - 由于父类(super class)(按值传递)导致的重载构造函数调用不明确

我围绕GSL的某些部分编写了一些C++包装器并遇到了以下难题(对我来说)。代码(精简到最基本的部分)如下:#includestructgsl_vector_view{};classVector:protectedgsl_vector_view{public:Vector(constVector&original);Vector(constgsl_vector_viewview);};classAutoVector:publicVector{public:explicitAutoVector(constsize_tdims);};voiduseVector(constVectorb){}

c++ - 错误 : Invalid base class C++

谁能解释一下是什么导致了这个错误?Error:Invalidbaseclass我有两个类,其中一个派生自第二个:#if!defined(_CGROUND_H)#define_CGROUND_H#include"stdafx.h"#include"CGameObject.h"classCGround:publicCGameObject//CGameObjectissaidtobe"invalidbaseclass"{private:boolm_bBlocked;boolm_bFluid;boolm_bWalkable;public:booldraw();CGround();CGround

c++ - 将子类对象传递给采用父类(super class)对象的函数

假设以下代码:classEvent{public:virtualvoidexecute(){std::cout执行时,程序输出“Eventexecuted.”,但我想执行SubEvent。我该怎么做? 最佳答案 您正在按值传递Event。该函数获取自己的参数拷贝,这是一个Event对象,而不是SubEvent。您可以通过传递引用来解决此问题:voidexecuteEvent(Event&e){//^e.execute();}这叫做objectslicing.这相当于:SubEventse;Evente{se};e.execute()

c++ - `template <class> friend class Foo` 是什么意思?

我正在探索boost::iterator_facade并遇到了这段代码:friendclassboost::iterator_core_access;templatefriendclassIterator;第二行是什么意思?我熟悉friend类,但我想我没见过template在任何事情之前。这里是上下文:templateclassnode_iter:publicboost::iterator_facade,Value,boost::forward_traversal_tag>{public:node_iter():m_node(0){}explicitnode_iter(Value*p

c++ - 错误 : class template partial specialization contains a template parameter that cannot be deduced

我很感激帮助弄清楚我的代码中出现的这个问题是怎么回事,我已将其简化为以下内容:typedefunsignedshortushort;templatestructFoo{};//Specialization--workswhennotaspecializationtemplateclassContainer,templateclass>classMetaFunction>structFoo::Type>>{//typedefContainer::Type>TestType;//OK};intmain(){}在编译(gcc5.4.0)时出现错误:Test.cpp:14:8:error:te

c++ - 需要 : C++ class for maintaining a 1-dimensional list of extents

我正在寻找可以维护一维范围列表的C++类。每个范围都定义为一个(start,len)对。我希望能够向列表中添加额外的范围并自动合并它们。也就是说,如果我们在列表中有(0,5)和(10,5),并且添加了(5,5),新列表应仅包含(0,15)。范围永远不会从列表中删除。有这样的东西吗?谢谢。 最佳答案 您正在寻找Boost.Icl。它完全符合您的描述。http://www.boost.org/doc/libs/1_52_0/libs/icl/doc/html/index.html 关于c++