C++。无法调用成员函数。错误 : "foo is private"

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

本文关键字:foo is private 错误 函数 调用 成员 C++      更新时间:2023-10-16
#include <stdio.h>
class MyClass { 
  void Foo(const int par);  }; 

void MyClass::Foo(const int par) { } 
main()  {    MyClass A;    A.Foo(1);  }

有人能帮我吗?我的代码有什么问题?这是我用gcc编译时得到的错误:

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

类成员和类成员函数在默认情况下是私有的,这意味着它们只能被相同类和友元的方法访问。

class MyClass {  
  // members declared here will be private
public: 
  // members declared here will be public
  void Foo(const int par); 
private:
  // private
}; 

方法默认为private。使用

public: void Foo(const int par);