0

I have a class (in the wxWidgets framework) defined like that:

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

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

Somewhere else in the pogram I create a reference to an instance of this object...

mSomePanel = new SomePanel();

... then I want to do this

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

In the (ctor of the) class I am able to call the same method with this member variable. Where does the problem lie?

fotinsky
  • 972
  • 2
  • 10
  • 25

1 Answers1

1

Ignore my previous ramblings. Classname::id should get you the id.

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

Edited to add more complete code:

This goes in your .h:

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

This goes in your implementation file (e.g., SomePanel.cpp):

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

Now to reference the id:

SomePanel::id

Also, another issue might be the fact that a method has an argument with the same name as a member variable. When you call UseId(id) how does the compiler know that you are referring to your member variable versus the function's argument. Try changing the argument's name in your SomeMethod().

It'sPete
  • 5,083
  • 8
  • 39
  • 72
  • 5
    `id` is neither static nor private. – Thomas Nov 09 '13 at 10:28
  • I tried it out, and now I get on the same spot the compiler error: mSomePanel is not a class or namespace. – fotinsky just now edit – fotinsky Nov 09 '13 at 10:35
  • Look again, its not mSomePanel::id, it's SomePanel::id. You give the name of the class where it is found. – It'sPete Nov 09 '13 at 10:35
  • Tried it, too. Now it's: SomePanel has not been declared. – fotinsky Nov 09 '13 at 10:38
  • Try making it static, no need to have each const for each object, and the above syntax should work. Otherwise, I think your syntax that you just posted is correct. – It'sPete Nov 09 '13 at 10:41
  • I'm sure there's someway to do it otherwise, but I've always done such things by declaring/defining the variable as static since there's no reason to have an independent copy of ID for each object you create. Examples are found here: http://stackoverflow.com/questions/1563897/c-static-constant-string-class-member – It'sPete Nov 09 '13 at 10:44
  • Tried both possibilities with a static const string. Still each the same error. – fotinsky Nov 09 '13 at 10:45
  • See here: http://stackoverflow.com/questions/1241099/c-access-const-member-vars-through-class-or-an-instance The code is valid, and how you would access the constant, not sure what you are doing wrong. – It'sPete Nov 09 '13 at 10:49
  • I try now to define the static const string member outside the class, but neither const std::string mSomePanel->id = "bla"; ("expected initializer ->") , nor with the "->" operator ("it is not a class or namespace") it is working. – fotinsky Nov 09 '13 at 11:07
  • I have no idea what you are trying to do! mSomePanel->id = "bla" makes absolutely no sense. Why would an instance of SomePanel try to change the const value of the class? See the code in my answer AND all the code in the various SO threads. Plenty of examples. – It'sPete Nov 09 '13 at 11:09
  • Because of this [link](http://stackoverflow.com/questions/1241099/c-access-const-member-vars-through-class-or-an-instance). Read the 2nd part of the 1st post. According to that, I have to define the const outside the class (no changing). – fotinsky Nov 09 '13 at 11:14
  • But you don't define the const as a part of an instance of the class... you define it as const std::string SomePanel::id = "bla"; Like I showed in my answer. This goes in your .cpp file for the SomePanel class. – It'sPete Nov 09 '13 at 11:16
  • Ok, did it now exactly like you described it in you post Pete, and it's still: "class somePanel has no element named id". Actually the names are different, id ist called root_identifier. It's ridiculous. My sample code withoud the static definition should logically have worked. Includes are set properly. Will consider @billz comment, and redesign the method as a workaround. The software is too big and complex to post more code. – fotinsky Nov 09 '13 at 11:34
  • Really don't know what you are doing wrong. This is a standard way of doing things. Can't help you without more code being posted. – It'sPete Nov 09 '13 at 11:37
  • Works with the workaround. Good hint for using a static member, and how to define it, but the main root of the problem lies somewhere else. My initial code should normally also work if it has not to be a static member. Because I cannot provide any additional information, I will mark this question as answered. – fotinsky Nov 09 '13 at 18:46