0

我有一个这样定义的类(在 wxWidgets 框架中):

class SomePanel : public wxPanel{
public:
   ...
   void SomeMethod(const std::string& id){
      pointer->UseId(id);
   } 

   const std::string id = "Text"; // still in public area
   ...
}

在 pogram 的其他地方,我创建了对该对象实例的引用...

mSomePanel = new SomePanel();

...然后我想这样做

mSomePanel->SomeMethod(mSomePanel->id); // Compiler gives an error saying that
                                         // there is no element named id.

在(的)类中,我可以使用这个成员变量调用相同的方法。问题出在哪里?

4

1 回答 1

1

忽略我之前的胡说八道。Classname::id 应该为您提供 id。

mSomePanel->SomeMethod(SomePanel::id);  // this should work.

编辑添加更完整的代码:

这在您的 .h 中:

class SomePanel {
 public:
  static const std::string id;  // no need to have an id for each SomePanel object...
};

这在您的实现文件中(例如,SomePanel.cpp):

const std::string SomePanel::id = "Text";

现在引用id:

SomePanel::id

此外,另一个问题可能是方法具有与成员变量同名的参数这一事实。当您调用 UseId(id) 时,编译器如何知道您指的是您的成员变量而不是函数的参数。尝试在 SomeMethod() 中更改参数的名称。

于 2013-11-09T10:26:43.180 回答