问题
sorry for the long and confusing title, but I couldn't think of a better way to ask this. So, what I have is a class:
template <typename T>
class Set
{
public:
//random member functions here
private:
struct Node{
T key;
Node *right;
Node *left;
int height;
};
public:
Node* r_add(Node *temp);
};
Node* Set<T>::r_add(Node *temp)
{
return temp;
}
When I try to implement the function r_add, I keep getting the error that the return type of out-of-line definition differs from that in the declaration for the r_add function. I'm not exactly clear on how to declare the return type for when I try to call a private structure in a class member function.
回答1:
The syntax for that needs to be:
template <typename T>
Set<T>::Node* Set<T>::r_add(Node *temp)
{
return temp;
}
Please note that you don't have to use Set<T>::Node*
for the argument type since the scope Set<T>
will be used for Node
at that point.
Another option is to use trailing return type. That will allow you to avoid having to type Set<T>
any more than is necessary.
template <typename T>
auto Set<T>::r_add(Node *temp) -> Node*
{
return temp;
}
回答2:
template <typename T>
typename Set<T>::Node* Set<T>::r_add(typename Set<T>::Node* temp)
{
return temp;
}
here is my implementation for map, and you can find the solution to your problem
来源:https://stackoverflow.com/questions/50575836/how-to-set-the-return-type-of-a-class-member-function-as-the-object-of-a-private