不完整类型的无效使用/错误的前向声明。可能滥用抽象类? (C++)

发布于 2024-12-21 07:11:53 字数 1023 浏览 7 评论 0原文

现在我收到错误:

error: ‘oset<std::basic_string<char, std::char_traits<char>, std::allocator<char> >   >::Comparator’ is an inaccessible base of ‘CaseSensitive’

我花了几个小时试图解决这个问题,但无济于事,希望你们中的一个人可以帮助我。相关代码如下:

template <class T>
class oset;

template <class T>
class oset {

....

 public:
    class Comparator {
 public:
    virtual ~Comparator() {}
    virtual int compare(T, T) = 0;
};

 private:
    Comparator *comp;

....

 public:
    // new empty set:
    oset(Comparator*);
....

};

....

class CaseSensitive : oset<string>::Comparator
{
    public:
        virtual ~CaseSensitive(){}
        virtual int compare(string str1, string str2)
        {
            return strcmp(str1.c_str(), str2.c_str());
        }
};
....

int main() {
    oset<string>::Comparator *cs = new class CaseSensative();
    //error in line above

    ....

}

我想做的是创建一个抽象 Comparator 对象,以便我可以定义自定义类型顺序来对 oset 进行排序。

谢谢!

now i get the error:

error: ‘oset<std::basic_string<char, std::char_traits<char>, std::allocator<char> >   >::Comparator’ is an inaccessible base of ‘CaseSensitive’

I've been trying to figure out the issue for several hours to no avail, hopefully one of you guys can help me. relevant code is below:

template <class T>
class oset;

template <class T>
class oset {

....

 public:
    class Comparator {
 public:
    virtual ~Comparator() {}
    virtual int compare(T, T) = 0;
};

 private:
    Comparator *comp;

....

 public:
    // new empty set:
    oset(Comparator*);
....

};

....

class CaseSensitive : oset<string>::Comparator
{
    public:
        virtual ~CaseSensitive(){}
        virtual int compare(string str1, string str2)
        {
            return strcmp(str1.c_str(), str2.c_str());
        }
};
....

int main() {
    oset<string>::Comparator *cs = new class CaseSensative();
    //error in line above

    ....

}

What I am trying to do is make an abstract Comparator object so that I can define custom typological orders to sort oset with.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风无影 2024-12-28 07:11:53

您从Comparator 私有继承,因此继承关系在类的成员和友元之外不可用。特别是,这会阻止从 CaseSensitive*Comparator* 的转换。您可能希望公开继承:

class CaseSensitive : public oset<string>::Comparator // added "public"
{
    // members here
};

除非您另外指定,否则 class 的成员和基类都是 privatestruct 的那些是 public

You are inheriting privately from Comparator, so that the inheritance relationship is not available outside members and friends of the class. In particular, that prevents conversion from CaseSensitive* to Comparator*. You probably want to inherit publicly:

class CaseSensitive : public oset<string>::Comparator // added "public"
{
    // members here
};

Unless you specify otherwise, members and base classes of a class are private; and those of a struct are public.

落花浅忆 2024-12-28 07:11:53

它应该是 new CaseSensative()(顺便说一句,拼写为“sensitive”),并且该类的完整定义必须可用(您很可能没有包含定义它的标头)。

好吧,或者您定义了一个类并尝试使用不存在的类,在这种情况下它应该是 new IgnoreCase()

It should be new CaseSensative() (which is spelled 'sensitive', BTW), and the full definition of that class must be available (you most likely didn't include a header which defines it).

Well, or you defined one class and try to use non-existing one, in which case it should be new IgnoreCase().

征﹌骨岁月お 2024-12-28 07:11:53

您应该在问题中包含 CaseSensative。除非那是问题所在。

You should include CaseSensative in your question. Unless that's the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文