草庐IT

c++ - 错误 : 'object' was not declared in this scope

我是C++的新手,正在尝试制作大富翁游戏。不幸的是,它仍然显示两个类之间的声明错误。我已经尝试了所有方法,但真的不知道问题出在哪里。错误:“玩家”未在此范围内声明。引擎.h#ifndefENGINE_H#defineENGINE_H#include"Player.h"#includeusingnamespacestd;classEngine{public:Engine();//methodthatstartswithgame,takerandomnumberforgettingnumberofplayers,setplayerstovectorvoidplay();//methodwh

c++ - 为什么 "this"指针用于调用派生成员函数?

在this上阅读关于虚函数的教程(与本例无关)链接,我找到了这段代码。classWeapon{public:voidfeatures(){coutWeapon::features();cout类Weapon派生自类Bomb,其中调用了Weapon的成员函数。为什么使用“this”指针调用函数Weapon::features()?这不是已经隐式给出了吗? 最佳答案 this是隐式给出的,是否显式编写通常是风格问题。在您的情况下,我会说它不会提高可读性。然而,在其他情况下,显式写入this是有意义的,甚至是必要的为了避免局部变量和数据成

c++ - BOOST_FOREACH : What is the error on using this of a STL container?

有谁知道为什么以下会在VC9上产生错误?classElem;classElemVec:publicvector{public:voidfoo();};voidElemVec::foo(){BOOST_FOREACH(Elem&elem,*this){//Dosomethingwithelem}return;}我得到的错误是:errorC2355:'this':canonlybereferencedinsidenon-staticmemberfunctions我现在拥有的唯一(hack)解决方案是:voidElemVec::foo(){ElemVec*This=this;BOOST_FO

c++ - 自定义 C++ 类上的错误

我需要帮助来找到使用自定义C++类来管理3D位置的问题。这是类中的相关代码Puntooperator+(Puntop){returnPunto(this->x+p.x,this->y+p.y,this->z+p.z);}Puntooperator+(Punto*p){returnPunto(this->x+p->x,this->y+p->y,this->z+p->z);}Puntooperator-(Puntop){returnPunto(this->x-p.x,this->y-p.y,this->z-p.z);}Puntooperator-(Punto*p){returnPunto(

c++ - 模板 :Name resolution:Point of instantiation: -->can any one tell some more examples for this statement?

这是来自ISOC++标准14.6.4.1实例化点的声明Forafunctiontemplatespecialization,amemberfunctiontemplatespecialization,oraspecializationforamemberfunctionorstaticdatamemberofaclasstemplate,ifthespecializationisimplicitlyinstantiatedbecauseitisreferencedfromwithinanothertemplatespecializationandthecontextfromwhichi

c++ - 具有固定实现的虚函数不使用 (*this) 的大多数派生类

假设我有以下代码:structZ;structA{virtualvoidDo(Z&z)const;};structB:publicA{};structZ{voiduse(Aconst&a){}voiduse(Bconst&b){}};voidA::Do(Z&z)const{z.use(*this);}现在,当我调用B.do,this的类型是A,这是有道理的,因为do的实现在A中定义.有什么方法可以调用B.do使用use(Bconst&)无需为do复制粘贴相同的代码来自A进入B?在我的实际代码中,我有大约15个(并且还在不断增加)派生自某个基类的类,必须为do复制粘贴相同的代码似乎很浪费

c++ - 在基类中调用 shared_from_this() 时的 bad_weak_ptr

我有一个SuperParent类,一个Parent类(派生自SuperParent)并且都包含一个shared_ptr到一个Child类(它包含一个weak_ptr到一个SuperParent)。不幸的是,我在尝试设置Child的指针时遇到了bad_weak_ptr异常。代码如下:#include#include#include#includeusingnamespaceboost;classSuperParent;classChild{public:voidSetParent(shared_ptrparent){parent_=parent;}private:weak_ptrpare

c++ - 在 C++ 中沿着二维 vector (塔防)移动

我正在写一个简单的塔防,我卡在了我的塔必须射击敌人的地方。使用这段代码:voidBullet::move(intx,inty,intspeed){Punktdelta={(x+speed)-this->x,(y+speed)-this->y};if(abs(delta.x)>1&&abs(delta.y)>1){this->x+=delta.x/this->speed;this->y+=delta.y/this->speed;}else{this->dead=true;}}其中方法参数是目标位置和速度。它应该沿着vector移动子弹直到它到达目标但是vector改变因为目标在移动。现在

c++ - 无法在 C++ 中初始化指针数组

我有一个指向名为“表”的结构(该结构称为节点)的指针数组。我在类中这样声明数组:Node*table;然后,在另一种方法中,我初始化表:this->table=newNode[this->length];一切正常。this->length是一个有效条目,this->table指向正确的数组,等等。但是,然后我尝试更改元素的值:for(inti=0;ilength;i++){this->table[i]=newNode;}甚至for(inti=0;ilength;i++){this->table[i]=0;}一切都开始出现问题。为什么我不能将这些指针设置为任何内容?这是我得到的错误:(其

c++ - 异常 : bad_weak_ptr while shared_from_this

当我这样做时出现异常:std::bad_weak_ptr->shared_from_this()templateclasspainter_record_t{.......private:std::shared_ptr_owner;}这里我想在构造函数中设置“问题”对象:templateclassstream_record_t:publicpainter_record_t{public:stream_record_t(std::shared_ptrowner):painter_record_t(owner){//...}}我有基类:classi_painter_t{public:virt