error: no matching function for call to c++ enumeration

error: no matching function for call to c++ enumeration

我遇到以下问题已经很久了。关键是我已经阅读了一些关于 Whosebug 的内容并且我使用了 typedef,但我没有帮助我。

期待得到一些帮助:)

Align_vector.h :

#include <utils/vector_2d.h>
class Vector_2d;

namespace Utils {

class Align_vector : public Vector_2d {
protected:
    bool check_range(int x, int y);

public:

    typedef enum {left, right, up, down} Alignment;

    Align_vector(Alignment alignment);
    void set_alignment(Alignment alignment);
    Alignment get_alignment();

};

} /* namespace Utils */

Align_vector.cc :

#include <utils/align_vector.h>


namespace Utils {

Align_vector::Align_vector(Alignment alignment) {
    this->alignment = alignment;
}

void set_alignment(Alignment alignment) {
    Align_vector::alignment = alignment;
}

Alignment get_alignment() {
    return Align_vector::alignment ;
}

bool check_range(int x, int y) {
    switch ( Align_vector::alignment ) {
        case Align_vector::left:
            if (x == -1 && y == 0) {
                return true;
            } else {
                return false;
            }
            break;
        case Align_vector::right:
            if (x == 1 && y == 0) {
                return true;
            } else {
                return false;
            }
            break;
        case Align_vector::down:
            if (x == 0 && y == -1) {
                return true;
            } else {
                return false;
            }
            break;
        case Align_vector::up:
            if (x == 0 && y == 1) {
                return true;
            } else {
                return false;
            }
            break;
        default:
            return false;
            break;
    }

}


} /* namespace Utils */

这里是错误:

/utils/align_vector.cc:14:47: error: no matching function for call to ‘Utils::Vector_2d::Vector_2d()’ Align_vector::Align_vector(Alignment alignment) {

这段代码有很多问题。首先,你定义了一个 enum 调用了 Alignment,但您没有声明成员 那种类型的。为此,请在 enum:

的定义之后添加此行
Alignment alignment;

你的方法定义也不正确,对齐方式是 应该属于一个特定的对象,而你正在使用它 几个功能就好像它是 class 的静态成员一样。这里 是方法定义的固定版本:

namespace Utils {

Align_vector::Align_vector(Alignment alignment) {
    this->alignment = alignment;
}

void Align_vector::set_alignment(Alignment alignment) {
    this->alignment = alignment;
}

Align_vector::Alignment Align_vector::get_alignment() {
    return this->alignment;
}

bool Align_vector::check_range(int x, int y) {
    switch(this->alignment) {
    case Align_vector::left:
        if(x == -1 && y == 0) {
            return true;
        }
        else {
            return false;
        }
        break;
    case Align_vector::right:
        if(x == 1 && y == 0) {
            return true;
        }
        else {
            return false;
        }
        break;
    case Align_vector::down:
        if(x == 0 && y == -1) {
            return true;
        }
        else {
            return false;
        }
        break;
    case Align_vector::up:
        if(x == 0 && y == 1) {
            return true;
        }
        else {
            return false;
        }
        break;
    default:
        return false;
        break;
    }

}


} /* namespace Utils */

最后,您缺少 base 的默认构造函数的定义 class Vector_2d(您的评论表明您没有定义 class 根本上,您只是使用语句 class Vector_2d;) 声明了它的存在。 从您的整体实施来看,我认为用户@molbdnilo 是正确的 当建议您应该学习更多关于 C++ 编程的一般知识时。

希望对您有所帮助!