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

时间:2015-04-28 14:55:05

标签: c++ pointers dereference

我收到了错误

" 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()方法之后放一个点而不是箭头时,它不会编译。

由于

2 个答案:

答案 0 :(得分:2)

此:

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

应该是:

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

mapvector,因此应使用.代替->访问其成员。
Draw需要const Drawable&,因此vector中的指针应取消引用。

答案 1 :(得分:1)

非常有帮助发布完整的错误消息以及其他人可以在自己的计算机上编译的示例。

#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()));