Home > Backend Development > C++ > body text

Solve the problem of 'error: 'class' has no member named 'function'' in C++ code

王林
Release: 2023-08-26 19:52:45
Original
1594 people have browsed it

解决C++代码中出现的“error: \'class\' has no member named \'function\'”问题

Solve the "error: 'class' has no member named 'function'" problem in C code

When programming in C, you often encounter Various compilation errors. One of the common errors is "error: 'class' has no member named 'function'". This error indicates that the called member function was not found in a class. Below we will introduce several common situations and solutions.

  1. Incorrect declaration of member functions

In this case, the problem usually arises from the inconsistency between the definition and implementation of the class. This error occurs when we declare a member function in the definition of a class, but forget to actually define this function in the implementation.

The sample code is as follows:

class MyClass {
public:
  void myFunction(); // 声明成员函数
};

int main() {
  MyClass obj;
  obj.myFunction(); // 调用成员函数
  return 0;
}
Copy after login

Solution: Add the corresponding member function definition in the implementation of the class.

void MyClass::myFunction() {
  // 实现代码
}
Copy after login
  1. Member function is defined as private or protected

If a member function is defined as private or protected, it is called elsewhere outside the class An error will be reported when this function is used.

The sample code is as follows:

class MyClass {
private:
  void myFunction(); // 私有成员函数
};

int main() {
  MyClass obj;
  obj.myFunction(); // 调用私有成员函数
  return 0;
}
Copy after login

Solution: Change the private member function to public, or add the corresponding interface function to the class to call the private member function.

class MyClass {
public:
  void myInterface() {
    myFunction(); // 调用私有成员函数
  }
  
private:
  void myFunction();
};

int main() {
  MyClass obj;
  obj.myInterface(); // 调用接口函数
  return 0;
}

void MyClass::myFunction() {
  // 实现代码
}
Copy after login
  1. Member function is defined in the wrong scope

If a member function is defined in the wrong scope, the compiler will not be able to find the member function to report an error.

The sample code is as follows:

class MyClass {
public:
  void myFunction();
};

void myFunction() {
  // 实现代码
}

int main() {
  MyClass obj;
  obj.myFunction(); // 调用成员函数
  return 0;
}
Copy after login

Solution: Define the member function in the correct class scope.

class MyClass {
public:
  void myFunction();
};

void MyClass::myFunction() {
  // 实现代码
}

int main() {
  MyClass obj;
  obj.myFunction(); // 调用成员函数
  return 0;
}
Copy after login

Summary

When encountering the "error: 'class' has no member named 'function'" error in C code, we need to check whether the above situations exist in the code. Properly declaring member functions, changing private member functions to public or adding interface functions, and defining member functions in the correct scope are all effective ways to solve this problem. By carefully checking the code and making corrections according to the above solutions, we can successfully resolve this error and compile and run our C program smoothly.

The above is the detailed content of Solve the problem of 'error: 'class' has no member named 'function'' in C++ code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template