Home  >  Article  >  Backend Development  >  C++ syntax error: Friends cannot be declared in class template definition, how to fix it?

C++ syntax error: Friends cannot be declared in class template definition, how to fix it?

WBOY
WBOYOriginal
2023-08-22 10:30:441327browse

C is a powerful programming language with rich syntax and template mechanisms, allowing developers to build various complex data structures and algorithms. But when using C, we often encounter various problems, one of which is friend declaration error.

Friends are an important concept in C language, which allow one class or function to access private members in another class. In actual development, we often need to use the friend mechanism to access private members of other classes, such as implementing operator overloading, testing class private functions, etc. However, using friends in a class template is more difficult because the instantiation type in the class template is unknown.

The cause of the problem is that friends cannot be declared in the class template, because the member information in the class is not known before instantiation. Once a friend is declared, the specific members to be accessed must be determined. This leads to a syntax error in which it is illegal to declare friends in a class template definition. For example:

template<class T>
class MyClass {
    friend class FriendClass; // 错误!
    T member;
};

At this time, the compiler will give the following error message:

error: template argument required for 'class FriendClass'
     friend class FriendClass;
                            ^

So, how to fix this error?

The solution is to move the friend definition outside the class. When declaring a friend outside a class, you need to use the specific instantiation type to determine the members to be accessed. For example:

template<class T>
class MyClass;
 
template<class T>
class FriendClass {
public:
    void Print(MyClass<T>* myClass) {
        std::cout << myClass->member << std::endl;
    }
};
 
template<class T>
class MyClass {
    friend class FriendClass<T>; // 正确
public:
    T member;
};

In the above example, we put the declaration of FriendClass in front of MyClass and explained the instantiation type of T. In this way, FriendClass can access the members in MyClass. At the same time, we also need to declare FriendClass as a friend in MyClass, so that MyClass can be accessed in FriendClass.

To summarize, it is illegal to declare friends in a class template because the type in the class template is unknown. If you need to use the friend mechanism to access private members of other classes, you should move the friend definition outside the class and determine the members to be accessed by instantiating the type. This method can avoid syntax errors and also achieve the purpose of the friend mechanism.

The above is the detailed content of C++ syntax error: Friends cannot be declared in class template definition, how to fix it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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