"->"的基本操作数具有非指针类型错误



>我收到错误

" 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                   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中的指针应该被取消引用。

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

#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

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

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

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

最新更新