Victor Oliveira
Victor Oliveira

Reputation: 3713

Error: "Function" is not a Non static data member or base class of "Class"

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.

Upvotes: 2

Views: 12227

Answers (3)

Armen Babakanian
Armen Babakanian

Reputation: 2305

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.

Upvotes: 0

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

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

};

Upvotes: 7

bruvel
bruvel

Reputation: 396

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;

Upvotes: 0

Related Questions