problem with 'static' in class

Started by
22 comments, last by XTAL256 16 years ago
The '::' isn't for resolving namespaces it is for resolving scope issues, of which namespaces are one possible application. Classes are another. Scope Resolution Operator.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement
Yes but why can i say
using namespace gui;void Button::draw() {}

instead of
using namespace gui;void gui::Button::draw() {}

but i have to say
using namespace graphics;void graphics::drawImage(...) {}

I understand that drawImage needs to be defined within the 'graphics' scope but what about my gui example, i specify Button scope but it doesn't seem to need gui scope.
[Window Detective] - Windows UI spy utility for programmers
Because the only valid way that
void Button::draw() {}


can make sense is if it refers to an existing function named draw.

If you have:
namespace Button {  void draw();};namespace gui {  namespace Button {    void draw();  }}using namespace gui;void Button::draw() {}

what happens? :)

Using namespace tells the code to look there after looking locally. namespace foo { ... } does what you are describing.
I have another problem. Everything compiles ok but my static objects (static Image *upImg, *downImg, *hoverImg;) don't seem to be created.
I read them from an XML file:
void Button::loadImages() {  // Defined in Button as 'static void loadImages()'    XMLNode* node = readXML(UI_XML);    node = node->getChild("button");  // Get <button> element    XMLNode* child;    for (int i = 0; child = node->getChild(i); i++) {        if (!strcmp(child->getType(), "img")) {            getImageFromXML(child, Button::upImg, "up");            getImageFromXML(child, Button::hoverImg, "hover");            getImageFromXML(child, Button::downImg, "down");        }        if (!strcmp(child->getType(), "div")) {            getDivFromXML(child, Button::divLeft, "left");            getDivFromXML(child, Button::divRight, "right");        }    }}// In gui.hinline void getImageFromXML(XMLNode* node, Image* img, char* id) {    if (!strcmp(node->getAttribute("id"), id))        img = new Image(node->getAttribute("file"));}

Then create an OpenGL display list from the images:
void Button::update() {   // 'void update()', virtual in Component    // Create display lists to draw each image    createComponentDL(dlUp,    Button::upImg,    x, y,             width, height, Button::divLeft, Button::divRight);    createComponentDL(dlHover, Button::hoverImg, x, y,             width, height, Button::divLeft, Button::divRight);    createComponentDL(dlDown,  Button::downImg,  x, y,             width, height, Button::divLeft, Button::divRight);}// In gui.hinline void createComponentDL(GLuint &id, Image* img, int x, int y,                int width, int height, int divLeft, int divRight) {    float ratio = height/img->height;// Ratio of img to component size    int posL = divLeft*ratio;        // Position of divLeft on component    int posR = divRight*ratio;       // Position of divRight on component    createList(id);    // Draw left edge of image    drawImage(img, x, y, posL, height, 0, 0, divLeft, img->height);    // Draw middle section of image    drawImage(img, x + posL, y, width - posL - posR, height,              divLeft, 0, img->width - divLeft - divRight, img->height);    // Draw right edge of image    drawImage(img, x + width - posR, y, posR, height,              img->width - divRight, 0, divRight, img->height);    endList();}

The problem is that it crashes on the line "float ratio = height/img->height" saying that 'img' is not defined. When i click 'Add Watch' it says that the variable is out of scope. I reference it as Button::upImg and i get no compile errors so i don't get it. And the images are being created in getImageFromXML because they are not NULL.

EDIT: i just found out that if i directly substitute the code in getImageFromXML to where it is used, it works! But why? Am i not passing the Image pointer right?
[Window Detective] - Windows UI spy utility for programmers

This topic is closed to new replies.

Advertisement