首页 > 解决方案 > 返回值类型与函数类型不匹配(返回类型为嵌套类)

问题描述

我正在通过在 C++ 中实现一组数据结构来练习编程。我目前收到此编译器错误,该错误与函数的返回类型与其声明中指示的类型不同。如果我注释掉Set::findSmallestset.cpp 底部定义的内容,程序就会编译。

我尝试了许多在 hpp 和 cpp 文件中将 Node 称为 Set::Node 或仅 Node 的变体,但我似乎无法让它工作。

我可以通过不在 Set 类中嵌套 Node 类来解决这个问题,但我想练习使用嵌套类。

set.cpp:86:12: error: out-of-line definition of 'findSmallest' does not match any
      declaration in 'Set'
Node* Set::findSmallest(Node* node){
           ^~~~~~~~~~~~
./set.hpp:12:24: note: type of 1st parameter of member declaration does not match
      definition ('Node *' vs 'Set::Node *')
    Node* findSmallest(Node* node);
                       ^
set.cpp:90:20: error: cannot initialize return object of type 'Node *' with an lvalue of
      type 'Set::Node *'
            return nextNode;
                   ^~~~~~~~
2 errors generated.

设置.hpp

#pragma once

class Node;

class Set{
public:
    Set();
    Set(int x);
    void find(int x);
    void insert(int x);
    Node* findSmallest(Node* node);
private:
    class Node {
    public:
        int value;
        Node *smallerNode;
        Node *greaterNode;
        Node();
        Node(int x);
    };
    Node* rootNode;
    Node* nextNode;
};

设置.cpp

#include <iostream>
#include "set.hpp"

using std::cout;
using std::endl;

Set::Node::Node():value(0){
    smallerNode = nullptr;
    greaterNode = nullptr;
}
Set::Node::Node(int x):value(x){
    smallerNode = nullptr;
    greaterNode = nullptr;
}

Set::Set(){
    rootNode = nullptr;
    nextNode = nullptr;
}
Set::Set(int x){
    rootNode->value = x;
    nextNode = nullptr;
}

void Set::find(int x){
    nextNode = rootNode;
    while(1){
        if(x == nextNode->value){
            cout << x << " found!" << endl;
            return;
        }
        else if(x > nextNode->value){
            if(nextNode->greaterNode){
                nextNode = nextNode->greaterNode;
            }
            else {
                cout << x << " not found!" << endl;
                return;
            }
        }
        else {
            if(nextNode->smallerNode){
                nextNode = nextNode->smallerNode;
            }
            else {
                cout << x << " not found!" << endl;
                return;
            }
        }
    }
}
void Set::insert(int x){
    if(!rootNode){
        rootNode = new Node(x);
        return;
    }
    nextNode = rootNode;
    while(1){
        if(x == nextNode->value){
            cout << x << " already exists!" << endl;
            return;
        }
        else if(x > nextNode->value){
            if(nextNode->greaterNode){
                nextNode = nextNode->greaterNode;
            }
            else { 
                nextNode->greaterNode = new Node(x);
                cout << "inserted " << x << endl;
                return;
            }
        }
        else {
            if(nextNode->smallerNode){
                nextNode = nextNode->smallerNode;
            }
            else {
                nextNode->smallerNode = new Node(x);
                cout << "inserted " << x << endl;
                return;
            }
        }
    }
}

Node* Set::findSmallest(Node* node){
    Node* nextNode = node;
    while(1){
        if(!nextNode->smallerNode){
            return nextNode;
        }
        else {
            nextNode = nextNode->smallerNode;
        }
    } 
}

标签: c++classscopebinary-search-treedefinition

解决方案


这里的错误:

Node* Set::findSmallest(Node* node){

是因为Node当它被用作返回类型时,它还没有在范围内。你应该写

Set::Node* Set::findSmallest(Node* node){

反而。或者,使用现代功能,

auto Set::findSmallest(Node* node) -> Node* {


推荐阅读