‘->’的基操作数有非指针类型错误

base operand of ‘->’ has non-pointer type error

我收到错误消息

" src/Graphics.cpp:29:32: erreur: ‘->’ 的基操作数具有非指针类型‘std::vector’ "

关于以下代码:

构造函数:

Graphics::Graphics()
{
 this->app = new sf::RenderWindow(sf::VideoMode(800, 800, 32), "La Zapette !", 
               sf::Style::Close | sf::Style::Titlebar);

  sf::Image img;
  img.LoadFromFile("./res/grass.jpg");

  for (int i = 0; i != 16; i++)
   {
      this->map.push_back(new sf::Sprite());
      this->map.back()->SetImage(img);
      this->map.back()->SetPosition(sf::Vector2f(0, 50 * i));
      this->app->Draw(this->map->back());
   }
   this->app->Display();  
}

Class :

class                   Graphics
{
private:
  sf::RenderWindow          *app;
  std::vector<sf::Sprite*>      map;
public:
  Graphics();
  ~Graphics();
  Event                 getEvent();
};

当我在 .back() 方法后面放一个点而不是箭头时,它无法编译。

谢谢

这个:

this->app->Draw(this->map->back());

应该是:

this->app->Draw(*(this->map.back()));

map 是一个 vector,因此应该使用 . 而不是 ->.
访问其成员 Draw 需要一个 const Drawable&,所以 vector 中的指针应该被取消引用。

其他人可以在自己的机器上编译的完整错误消息和示例非常有用到post。

#include <string>
#include <vector>

namespace sf {
    struct Image {
        void LoadFromFile(std::string);
    };

    struct Vector2f {
        Vector2f(float, float);
    };

    struct VideoMode {
        VideoMode(unsigned, unsigned, unsigned);
    };

    struct Sprite {
        void SetImage(Image);
        void SetPosition(Vector2f);
    };

    struct Style {
        static const unsigned Close = 1;
        static const unsigned Titlebar = 2;
    };

    struct RenderWindow {
        RenderWindow(VideoMode, std::string, unsigned);
        void Draw(Sprite *);
        void Display();
    };
}

class Event {
};

class Graphics
{
    private:
        sf::RenderWindow *app;
        std::vector<sf::Sprite*> map;
    public:
        Graphics();
        ~Graphics();
        Event getEvent();
};

Graphics::Graphics()
{
    this->app = new sf::RenderWindow(sf::VideoMode(800, 800, 32), "La Zapette !", 
            sf::Style::Close | sf::Style::Titlebar);

    sf::Image img;
    img.LoadFromFile("./res/grass.jpg");

    for (int i = 0; i != 16; i++)
    {
        this->map.push_back(new sf::Sprite());
        this->map.back()->SetImage(img);
        this->map.back()->SetPosition(sf::Vector2f(0, 50 * i));
        this->app->Draw(this->map->back());
    }
    this->app->Display();  
}

此代码产生错误:

c++     foo.cc   -o foo
foo.cc:61:34: error: member reference type 'std::vector<sf::Sprite *>' is not a pointer; maybe you meant
      to use '.'?
        this->app->Draw(this->map->back());
                        ~~~~~~~~~^~
                                 .
1 error generated.
make: *** [foo] Error 1

请注意,错误消息已包含错误所在的行。这非常有帮助,因为您肯定没有 post 29 行代码。

根据 Draw() 的签名,此行应为以下之一:

this->app->Draw(this->map.back());
this->app->Draw(*(this->map.back()));