简体   繁体   中英

invalid use of incomplete type / forward declaration of errors. possible missuse of abstract class? (C++)

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!

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 .

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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM