错误:c++枚举调用没有匹配的函数

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

本文关键字:函数 c++ 枚举 调用 错误      更新时间:2023-10-16

我有以下问题已经很久了。点列表,我已经读了一点关于stackoverflow,我已经使用了一个类型定义,但我没有帮助我。

期待得到一些帮助:)

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。答:

#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 */

错误如下:

//align_vector跑龙套。cc:14:47:错误:调用' Utils::Vector_2d::Vector_2d() '没有匹配的函数Align_vector::Align_vector(对齐对齐){

这段代码有很多问题。首先,你已经定义了anenum调用Alignment,但是没有声明成员那种类型的。为此,在enum的定义之后添加这一行:

Alignment alignment;

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

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的默认构造函数的定义类Vector_2d(你的评论表明你没有定义这个类您只是使用语句class Vector_2d;声明了它的存在。从你的整体实现来看,我认为用户@molbdnilo是正确的当你建议你应该学习更多关于c++编程的知识时,

希望这对你有帮助!