Nested classes problem.

Started by
2 comments, last by snk_kid 19 years, 5 months ago
well this just makes no sense ... here is part of the code:

class CGUI: {
  private:
    std::vector < GLuint > glFonts;
    // ...
    class XText {
      private:
        int x, y;
        Color color;
        uint fontIndex;
        std::string text;
      public:
        void render();
        // ...
    };
    // ...
    public:
      // ...
};

void CGUI::XText::render() {
  drawText(x, y, &CGUI::glFonts[fontIndex], color, text);
}


the error is ... type CGUI is not a base type for type CGUI::XDialogue if i change this &CGUI::glFonts[fontIndex] to just &glFonts[fontIndex] (which im pretty sure it shoudnt be), i get this error: CGUI::glFonts is not a member of CGUI::XText ... that doesnt make sense since it says CGUI::glFonts therefor the compiler knoes that variables scope ... but why it givng me an error in the first example with CGUI::glFonts[fontIndex] ? i looked on msdn and foind an example of neste classes to make sure i was doing it correctly and i found no difference in what i was doing and what their example was. should i just inlike the render function? whats going on here?
Advertisement
inner classes are static, there just like individual classes, XText needs to refer to an instance of CGUI to access glFonts member e.g.:

#include <vector>#include <string>typedef unsigned int GLuint;struct Color {};class CGUI {   std::vector < GLuint > glFonts;   class XText {         int x, y;      Color color;      unsigned int fontIndex;      std::string text;      CGUI& cgui_ref;   public:        XText(CGUI& g)        : x(0), y(0), fontIndex(0), text(), color(), cgui_ref(g) {}        void render() {           drawText(x, y, &cgui_ref.glFonts[fontIndex], color, text);        }    };    XText tx;public:   CGUI(): glFonts(), tx(*this) {}};
ok i got it. the example on msdn is as followes:
class C {private:    static int i;public:   class inner {   public:       int f() {         return C::i;      }   };};

i did not realize that i needed an instance of the base class, so i can see why they declared i as static so it is shared in all classes so it can be refered to directly. although when i try and do this: static std::vector < GLuint > glFonts; and use it directly in XText i get a linker error saying undefined refrence. i beleive i have to do it the way you outlined, why wouldnt declaring glFonts work in this case?

[Edited by - ekrax on November 14, 2004 3:03:15 PM]
Quote:Original post by ekrax
although when i try and do this: static std::vector < GLuint > glFonts; and use it directly in XText i get a linker error saying undefined refrence. i beleive i have to do it the way you outlined, why wouldnt declaring glFonts work in this case?


its most likely you haven't defined it which you do outside of the class in the source file if there is one e.g.

int C::i(0); //initialize to zero or change it to what evers appropriate

This topic is closed to new replies.

Advertisement