【问题标题】:base operand of ‘->’ has non-pointer type error‘->’的基操作数有非指针类型错误
【发布时间】:2015-04-28 14:55:04
【问题描述】:

我收到了错误

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

谢谢

【问题讨论】:

  • 下次遇到编译器错误时,请指出您发布的代码中哪一行与错误相对应。例如,我猜第 29 行对应于this-&gt;app-&gt;Draw(this-&gt;map-&gt;back());?但是,既然你已经知道了,我为什么还要猜测呢?

标签: c++ pointers dereference


【解决方案1】:

这个:

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

应该是:

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

map 是一个vector,因此应该使用. 而不是-&gt; 访问它的成员。
Draw 使用const Drawable&amp;,所以vector 中的指针应该是取消引用。

【讨论】:

  • 其实:this->app->Draw(*(this->map.back()));
  • Roman Grout:您在原始代码中传递了一个指针,所以我也这样做了。我用更正的电话更新了我的答案。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多