2

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 I'm 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 Nonstatic data member or a base class of class GUI::Container.

I tried the further 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 errors. What exactly is wrong and what should I read regarding this subject?

Note: I also read C++ guideline books but I didn't find the answer there cause I probably don't know how to refer to this problem.

Victor Oliveira
  • 3,293
  • 7
  • 47
  • 77

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,290
  • 1
  • 38
  • 62
  • So, components hasnt no methods at all, its calling from namespace sf - thats why it needs the colons right? – Victor 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 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 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 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 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 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
  • 2,125
  • 3
  • 23
  • 47