相关疑难解决方法(0)

C++访问存储在向量中的对象的元素

比如说你有:

class Plant {
    public:
    ...public accessors, etc ...

}
Run Code Online (Sandbox Code Playgroud)

然后你有一个特定的植物类:

class Broccoli : public Plant
{
    public:
        Broccoli(int i);
        virtual ~Broccoli();

        inline BroccoliSprite* getPlantSprite() { return _plantSprite; };

    private:
         BroccoliSprite* _plantSprite;

};
Run Code Online (Sandbox Code Playgroud)

在某些时候,Broccoli对象存储在向量中:std::vector<Plant> vPlants;

访问我使用的向量及其元素: inline std::vector<Plant>& getPlants() { return vPlants; };

访问:

 for (int f=0; f < _f.getPlants().size(); f++)
 {
    Plant _p1 = _f.getPlants().at(f);

    cocos2d::CCSprite* s = _p1.getPlantSprite();
    ....


 }
Run Code Online (Sandbox Code Playgroud)

我希望能够用getPlantSprite()它来显示给用户,但编译器告诉我:No member named 'getPlantSprite' in 'Plant'

这是真的,但是向量中的对象是类型Plant并且包含一个成员变量,它是我需要的精灵,getter返回它.

我不明白的是如何获得访问权限 getPlantSprite()

我想有一点是我可以改变 …

c++ class vector c++11

0
推荐指数
1
解决办法
1635
查看次数

C++中的虚函数问题

AoA,我正在制作一个国际象棋的控制台游戏,但我坚持多态,下面是类和函数定义/*旧的部分//基类

class Piece         /*Parent class */
{
protected:
    Position* pCoord;
    std::string color;
    char symbol;
public:
    Piece(Position* Coord,std::string Color,char symbol);
    Position GetCurrentPos();
    std::string GetColor();
    void SetColor(std::string color);
    void Draw();
    virtual bool SetPos(Position* newPos){MessageBox(NULL,L"Virtual Running",L"Error",MB_OK); return true;};
    virtual ~Piece();
};

/* Inherited classes */
//Child classes
class Pawn: public Piece
{
private:
    std::vector<Position>* allowPos;
public:
    Pawn(Position* Coord,std::string Color,char symbol);
    ~Pawn();
    std::vector<Position>* GetThreatendFields();
    bool isValidMove(Position* newPos);
    bool SetPos(Position* newPos);
};
//Child classes
class Bishops: public Piece
{
private:
    std::vector<Position>* allowPos;
public:
    Bishops(Position* Coord,std::string …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance

0
推荐指数
1
解决办法
200
查看次数

具有自动存储持续时间的虚拟功能似乎不起作用

我在使用自动存储持续时间声明的对象使用虚函数时遇到问题.这是一个可重现的场景:

#include <iostream>

class A {
    public:
        A() {}
        virtual ~A() {}
        virtual void printClassName() {
            std::cout << "A" << std::endl;
        }
};

class B : public A {
    public:
        B() : A() {}
        ~B() {}
        void printClassName() {
            std::cout << "B" << std::endl;
        }
};

class Test {
    private:
        A item;

    public:
        Test() {}
        ~Test() {}
        void setItem(A item) {
            this->item = item;
        }
        A getItem() {
            return this->item;
        }
};

int main() {
    Test t;
    B item;

    t.setItem(item); …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism virtual-functions

0
推荐指数
1
解决办法
45
查看次数

C++如何返回未知的派生类?

我有一个基类Shape,派生类和EllipseRectangle.

在一个函数中,我有一个变量:

Shape activeShape(black, black, {0,0}, {0,0}, false);

后来在那个函数中:

activeShape = updateShape(isButton, activeShape, true);

updateShape 看起来像这样:

Shape updateShape(int button, Shape active, bool leftClick)
{
    switch(button)
    {
        case 1:
            return active;
        case 2:
            return Line(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
            break;
        case 3:
            return Rectangle(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
            break;
        case 4:
            return FilledRectangle(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
            break;
        case 5:
            return Ellipse(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
            break;
        case 6:
            return FilledEllipse(active.getFillColor(), active.getBorderColor(), active.getP1(), active.getP2(),false);
            break;
        default:
            if(leftClick)
            {
                active.setColor(getEnumColor(button), …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism inheritance

0
推荐指数
1
解决办法
330
查看次数

为什么继承的C++析构函数被调用了2次

我试过这段代码:

#include <iostream>
using namespace std;

class A {
public:
A(){cout<<"A();";}
 ~A(){cout<<"~A();";}
};

class B : public A {
public:
B(){cout<<"B();";}
 ~B(){cout<<"~B();";}
};

int main() {
A a =B();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:A(); B();〜B();〜A();〜A();

为什么A的析构函数被调用了2次?

c++ constructor destructor

0
推荐指数
1
解决办法
71
查看次数

无法使用dynamic_cast从Base转换为Derived

当我尝试将基类转换为派生类时,我收到错误.我想访问我放在组件向量中的派生类.

//基础和派生

class Component
{
public:
    Component();
    virtual ~Component();
private:
};

class Material:public Component{...};
Run Code Online (Sandbox Code Playgroud)

//在主要

int textureID = gameScene.gameObjects[0].getComponent<Material>()->texture;
Run Code Online (Sandbox Code Playgroud)

//游戏对象

#pragma once
#include <vector>
#include "Component.h"

class GameObject:public Component
{
public:
    GameObject();
    GameObject(int otherAssetID);
    ~GameObject();

    int assetID;
    std::vector<Component> components;

    void addComponent(Component otherComponent);
    void deleteComponent(Component otherComponent);

    template <class T>
    T* getComponent() {
        for (int i = 0; i < components.size(); i++)
        {
            if (dynamic_cast<T*>(components[i]) != nullptr)
            {
                T *test = dynamic_cast<T*>(components[i]);
                return test;
            }
        }

        return nullptr;
    }
private:

};
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism dynamic-cast game-engine

0
推荐指数
1
解决办法
398
查看次数

根据底层对象类型获取成员的值

我有来自第三方库的两个结构,所以我不能改变它们的布局.但是我有两个我自己编写的结构符合它们的签名,所以我可以为它们添加函数(再次,但不是它们的布局,因此也没有虚函数).结构被称为Foo1Foo2.两者都有相同的成员调用_b,但是在不同的偏移量.

struct FooBase { };

struct Foo1 : FooBase
{
    int _a = 2;
    int _b = 1;
};

struct Foo2 : FooBase
{
    int _b = 2;
};

struct Wrap
{
    Wrap(const FooBase& x) : _x(x) { }
    FooBase& _x;
    int GetValue() { return /* MISSING */; }
};
Run Code Online (Sandbox Code Playgroud)

我有一个名为的包装类Wrap.我正在寻找一种方法来返回_bfoo类中不使用虚函数的值,因为我不能再改变它们的大小了.

Foo1 f1 = Foo1();
Wrap x = f1;
int a = x.GetValue(); // should return 1

Foo2 …
Run Code Online (Sandbox Code Playgroud)

c++ data-structures c++11

0
推荐指数
1
解决办法
71
查看次数

如何将子类向量传递给需要基类向量的函数

我有一个类和一个类:

class Base{
  public:
    Base(){}
    // ...
};

class Sub: public Base{
    int param;
  public:
    Sub(){}
    // ...
};
Run Code Online (Sandbox Code Playgroud)

我还有一个需要Base向量的函数,如下所示:

void doSomeThing(vector<Base> vectorOfBases){
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我需要这样调用该函数:

vector<Sub> myVectorOfSubs;
doSomeThing(myVectorOfSubs);
Run Code Online (Sandbox Code Playgroud)

编译器告诉我:

没有从向量 < Sub > 到向量 < Base > 的适当转换

那么如何将子类向量传递给需要基类向量的函数呢?

c++ stdvector

0
推荐指数
1
解决办法
980
查看次数

让成员函数返回类型成为要返回的实际对象的超类的正确方法是什么?

问题

让成员函数返回类型成为您要返回的实际对象的超类的正确方法是什么?

问题

Clang tidy 警告我返回subClassAsubClassB丢弃我对myMethod. 我也没有得到任何输出 - 大概是因为我不小心丢弃了覆盖的myMethod.

编码

#include <iostream>

using namespace std;

class SuperClass {
public :
    SuperClass() {
        cout << "I'm the superclass" << endl;
    };

    virtual std::string myMethod();
};

class SubClassA : public SuperClass {
public:
    SubClassA() : SuperClass() {
        cout << "I'm subclass A" << endl;
    }

    std::string myMethod() override {
        return "A";
    }
};

class SubClassB : public SuperClass {
public:
    SubClassB() : SuperClass() { …
Run Code Online (Sandbox Code Playgroud)

c++ oop polymorphism

0
推荐指数
1
解决办法
63
查看次数

我如何制作虚拟变量?

我知道有虚函数,但没有虚变量。没有添加它可能是因为大部分时间不需要它,但我最近遇到了一个问题:

class A
{
public:
    int bar;
};

class B : public A
{
public:
    float foo;
};

class C : public A
{
public:
    double foo;
};

int main()
{
    std::vector<A*> v;
    v.push_back(new B());
    v.push_back(new C());

    std::cout << v[1]->bar << v[0]->foo; //error because it will not find 'foo' in A
}
Run Code Online (Sandbox Code Playgroud)

无论如何,我可以阻止这个错误吗?

c++

0
推荐指数
1
解决办法
100
查看次数