不完整类型的无效使用/错误的前向声明。可能滥用抽象类? (C++)
现在我收到错误:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您从
Comparator
私有继承,因此继承关系在类的成员和友元之外不可用。特别是,这会阻止从CaseSensitive*
到Comparator*
的转换。您可能希望公开继承:除非您另外指定,否则
class
的成员和基类都是private
;struct
的那些是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 fromCaseSensitive*
toComparator*
. You probably want to inherit publicly:Unless you specify otherwise, members and base classes of a
class
areprivate
; and those of astruct
arepublic
.它应该是 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()
.您应该在问题中包含 CaseSensative。除非那是问题所在。
You should include CaseSensative in your question. Unless that's the problem.