stringList and string

How Qt library solve problem with forward declaration of QStringList and QString classes. Both class have ability to manipulate with outer, if u call QString::split() it will return QStringList object, and in QStringList i have operator << for adding QString objects... When I try to make my own class who will do same work i have error with forward declarations...

My header is: stringList.h:

#ifndef STRINGLIST_H
#define STRINGLIST_H
#include "monString.h"
class stringList
{
    public:
        stringList();
        virtual ~stringList();
        void insert(monString newString);
        void prikazi();
        int size();
        monString & operator[](int offset);
    protected:
    private:
        stringList *slNext;
        monString *szString;
        int iOffset;
        static int iStringSize;
        stringList *lastNext;
};
#endif // STRINGLIST_H

and monString.h:

#ifndef MONSTRING_H
#define MONSTRING_H
#include <iostream>

using namespace std;
class stringList;

class monString{
private:
    char *dString;
    int dStringLongeur;
public:
    monString();
    monString(char* newString);
    ~monString();
    operator char*();
    int sizeOfString()const;
    monString(const monString &rhs);
    char operator[](int offset)const;
    char & operator[](int offset);
    monString &operator=(const monString &rhs);
    monString operator=(const char * const newString);
    monString operator+(monString &rhs);
    void operator+=(monString &rhs);
    void splitString(char);
    void split(const char &ch,stringList*);
    void isprazni();
};

In function split(const char&,stringList*) i have part of code stringList::insert, and on that line i have error:

monString.cpp|142|error: invalid use of incomplete type 'class stringList'

How can I solve this problem, and how Qt solved this...

Upvotes: 1

Views: 3471

Answers (1)

aschepler
aschepler

Reputation: 72271

monString.cpp needs to #include "stringList.h".

You only get into trouble with circular dependencies if you try to have both header files include each other. But in source files be sure to include all the class definitions you need.

Upvotes: 1

Related Questions