开发者

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

开发者 https://www.devze.com 2023-04-10 21:03 出处:网络
开发者_Go百科#include <stdio.h> class MyClass { void Foo(const int par);}; void MyClass::Foo(const int par) { }
开发者_Go百科#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


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

}; 


Methods are private by default. Use

public: void Foo(const int par);

0

精彩评论

暂无评论...
验证码 换一张
取 消