Anders Lind
Anders Lind

Reputation: 4840

C++. member function cannot be called. error : "foo is private"

#include <stdio.h>

class MyClass { 

  void Foo(const int par);  }; 


void MyClass::Foo(const int par) { } 

main()  {    MyClass A;    A.Foo(1);  }

Anyone can help me? What is wrong with my code? This is the error I get when compiling with gcc:

error: ‘void MyClass::Foo(int)’ is private

Upvotes: 1

Views: 1881

Answers (2)

Pubby
Pubby

Reputation: 53097

Class members and class member functions are by default private, meaning they can only be accesed by methods of the same class and friends.

class MyClass {  

  // members declared here will be private

public: 

  // members declared here will be public
  void Foo(const int par); 

private:

  // private

}; 

Upvotes: 2

CyberDude
CyberDude

Reputation: 8989

Methods are private by default. Use

public: void Foo(const int par);

Upvotes: 0

Related Questions