【问题标题】:Help with classes and member variables in C++帮助 C++ 中的类和成员变量
【发布时间】:2011-02-04 03:33:26
【问题描述】:

好的,我有两个类:图像和场景。现在在 Image 头文件中,我定义了三个私有变量:xcoord、ycoord 和 index(以及它们各自的公共 getter 方法)。

我有另一个名为 Scene 的类。 Scene 不是 Image 的子类。场景有两个成员变量:int maximumImage **images。现在在 Scene 中,我有一些方法试图访问 Image 类的成员变量。例如:

int beginX =this->images[i].getXcoord;
int beginY =this->images[i].getYcoord;

但是,我收到以下错误:

 error: request for member ‘getXcoord’ in ‘*(((Image**)((const Scene*)this)->Scene::images) + ((Image**)(((long unsigned int)i) * 8ul)))’, which is of non-class type ‘Image*’

scene.cpp:135: error: request for member ‘getYcoord’ in ‘*(((Image**)((const Scene*)this)->Scene::images) + ((Image**)(((long unsigned int)i) * 8ul)))’, which is of non-class type ‘Image*’

在我的 scene.cpp 文件中,我包含了 scene.h,其中包含 image.h,所以我很确定所有内容都已正确链接。我的问题是否显而易见,或者我必须提供更多信息吗?

【问题讨论】:

    标签: c++


    【解决方案1】:

    你想调用方法所以试试:

    int beginX = this->images[i]->getXcoord();
    int beginY = this->images[i]->getYcoord();
    

    否则编译器会寻找成员变量而不是 getter 方法

    【讨论】:

    • 你说得对。让我修复它并再次检查,但我很确定我会有更多错误。
    【解决方案2】:

    如果this->imagesImage**,那么this->images[i]Image*

    用箭头替换点。

    【讨论】:

      【解决方案3】:

      问题是图像数组包含指向类的指针

      试试 int beginX =this->images[i]->getXcoord;

      如果 getXcoord 是一个函数,你需要这样调用它

      int beginX =this->images[i]->getXcoord();

      最后你不需要this-> 它的暗示所以使用

      int beginX = images[i]->getXcoord();

      直流

      【讨论】:

        【解决方案4】:

        有两个问题。应该是:

         int beginX = this->images[i]->getXcoord();
        

        错误消息不允许使用 '.'不是类类型对象的 Image* 上的运算符。

        【讨论】:

          猜你喜欢
          • 2010-09-29
          • 1970-01-01
          • 2011-04-11
          • 2011-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-29
          相关资源
          最近更新 更多