2

I'm trying to understand the syntax of C++ since I'm almost fresh to the language, but I dont know what error exactly I'm facing..

I have the Component class implemented on my code and works fine

namespace GUI
{
class Component : public sf::Drawable
                , public sf::Transformable
                , private sf::NonCopyable
{
    public:
            //Variables
};
}

and also the book Im studying asks me to implement another class called Container in the GUI namespace

Container::Container()
: mChildren()
, mSelectedChild(-1)
{
}
void Container::pack(Component::Ptr component)
{
    mChildren.push_back(component);
    if (!hasSelection() && component->isSelectable())
        select(mChildren.size() - 1);
}
bool Container::isSelectable() const
{
    return false;
}

What I don't get is the way he is implementing the class, which is giving me the syntax error in the title of the post.. "Error: "mChildren" is not a Non static data member or base class of class "GUI::Container"".

I tryed the futher code:

class Container:

{

Container::Container()
: mChildren()
, mSelectedChild(-1)
{
}
void Container::pack(Component::Ptr component)
{
    mChildren.push_back(component);
    if (!hasSelection() && component->isSelectable())
        select(mChildren.size() - 1);
}
bool Container::isSelectable() const
{
    return false;
}
};

But I'm still getting syntax error =/ what exactly is going wrong and what shoul I read regarding this subject? (I also read C++ guidelines books but I didnt find the answer there cos I probably dont know how to refer to this problem) thanks in advance

Victor R. Oliveira
  • 2,669
  • 6
  • 42
  • 70

3 Answers3

7

When you define your methods inside your class declaration, you can't use the :: scope resolution operator.

Also your methods should probably in public. And finally you have to be sure that your mChildren member is correctly define.

class Container
{
    // ...

public:
    Container()
//  ^^
       : mChildren()
       , mSelectedChild(-1)
    {
    }
    void pack(Component::Ptr component)
//       ^^
    {
         // ...
    }
    bool isSelectable() const
//       ^^
    {
        // ...
    }

private:
    std::vector<Component::Ptr> mChildren;   // Example of a definition of mChildren
    //          ^^^^^^^^^^^^^^ replace with the good type

};
Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,042
  • 1
  • 36
  • 59
  • So, components hasnt no methods at all, its calling from namespace sf - thats why it needs the colons right? – Victor R. Oliveira Sep 10 '13 at 21:05
  • @PierreFourgeaud in fact ou **can't** use the scope resolution. – brunocodutra Sep 10 '13 at 21:06
  • @VictorOliveira `Component` seems to inherits some methods from `sf::Drawable`, `sf::Transformable` and `sf::NonCopyable`. The `::` are used here to indicate some classes in the namespace `sf`. Your class `Component` seems correct. – Pierre Fourgeaud Sep 10 '13 at 21:09
  • @brunocodutra You are right, I changed my first lines. Thanks for your comment ! – Pierre Fourgeaud Sep 10 '13 at 21:10
  • @PierreFourgeaud its correct, works fine, but I couldnt solve the problem yet from the class Container..I cant see on the book any declaration from mChildren, Im completely lost...the syntax u provide me now recognize its as a class Container, but I still confront the problem about the mChildren =/ – Victor R. Oliveira Sep 10 '13 at 21:12
  • @VictorOliveira Look at [**this article**](http://stackoverflow.com/questions/9338217/why-does-c-need-the-scope-resolution-operator) to understand the use of `::`. – Pierre Fourgeaud Sep 10 '13 at 21:12
  • @VictorOliveira Is `Container` inheriting from another `class` in your book ? It seems this is the error here. – Pierre Fourgeaud Sep 10 '13 at 21:13
  • @PierreFourgeaud so mchildren is a method from container? – Victor R. Oliveira Sep 10 '13 at 21:18
  • 1
    @VictorOliveira No, I think that `mChildren` could be an attribute (do not mix method and attribute) of an inheriting `class`. But in the sample code I gave you, `mChildren` is an attribute of `Container`. – Pierre Fourgeaud Sep 10 '13 at 21:23
  • @PierreFourgeaud Yes, I entered with the code you gave
    private:
        std::vector mChildren; but its weird cos on the book he doenst speak a **** about this mChildren as an Attribute, how did you guess this?
    – Victor R. Oliveira Sep 10 '13 at 21:26
  • 1
    @VictorOliveira Some people uses `m` as a prefix for member. Also you are using it in the [*member-initialization-list*](http://en.cppreference.com/w/cpp/language/initializer_list) used to initialized members(but not only). What is the book where you found this class ? – Pierre Fourgeaud Sep 10 '13 at 21:31
  • @PierreFourgeaud It comes from SFML Game Development - Chapter 6 [Menus]; Im trying to learn C++ mean time I learn about game dev..I have some knowledge in C++ to move on, but no to specific, If I try to learn only C++ without a purpose I get bored, with Java wasnt so hard =/ – Victor R. Oliveira Sep 10 '13 at 21:34
  • 1
    @VictorOliveira Actually it seems the class declaration looks like this: https://github.com/LaurentGomila/SFML-Game-Development-Book/blob/master/06_Menus/Include/Book/Container.hpp. [Here](http://www.cplusplus.com/doc/tutorial/) are some resources about C++. (My guess about the `mChildren` member was right :) ) – Pierre Fourgeaud Sep 10 '13 at 21:38
0

From this code you are using mChildren, but it is not defined in your Container class. What is mChildren supposed to be?

If it is a vector of Component::Ptr you need to define it in your class.

std::vector<Component::Ptr>mChildren;
bruvel
  • 396
  • 2
  • 6
  • I'm not sure about what should be, 'cause in futher pages or previous I can't see references about it, but I will look for cos this is really weird – Victor R. Oliveira Sep 10 '13 at 21:07
0

Why are you initializing mChildren in the constructor initialization list? More specifically what is this call mChildren() doing? Try removing that call and see what happens.

ArmenB
  • 1,897
  • 2
  • 20
  • 44