0

I am writing a small program, for a school project, that emulate a shared editor (like Google Docs)

I've written all the classes that I need but when I try to build the program I get this errors:

error: unknown type name 'NetworkServer' NetworkServer &_server;

error: use of undeclared identifier 'SharedEditor' std::vector<SharedEditor*> SEditors;

error: expected expression std::vector<SharedEditor*> SEditors;

error: unknown type name 'SharedEditor' int connect(SharedEditor* sharedEditor);

Here are my .h files

NetworkServer.h

#include <vector>
#include <queue>

#include "SharedEditor.h"
#include "Message.h"


class NetworkServer {                               
private:
    std::vector<SharedEditor*> SEditors;
    std::vector<int> idEditors;
    std::queue<Message> messages;

public:
    int connect(SharedEditor* sharedEditor);
    int disconnect(SharedEditor* sharedEditor);

    void send(const Message& m);
    void dispatchMessages();
};

SharedEditor.h

#include "NetworkServer.h"
#include "Message.h"
#include "Symbol.h"

class SharedEditor {
private:
    NetworkServer& _server;
    int _siteId{};
    std::vector<Symbol> _symbols;
    int _counter;

public:
    SharedEditor() = delete;
    SharedEditor(NetworkServer& server);

    void localInsert(int index, char value);
    void localErase(int index);

    void process(const Message& m);
    std::string to_string();

    int getSiteId() const;

};

Symbol.h

#include <vector>

class Symbol {
private:
    char value;
    int idClient;
    int num;
    std::vector<int> pos;

public:
    Symbol(char value, int client, int num, std::vector<int> pos);

    char getValue() const;
    int getIdClient() const;
    int getNum() const;
    const std::vector<int> &getPos() const;

};

Message.h

#include <vector>

class Message {
    bool insert;           // true --> insert || false --> delete
    int _siteId;
    int num;
    char value{};
    std::vector<int> pos;

public:
    Message(bool insert, int siteId, int num, char value, std::vector<int> pos);
    Message(bool insert, int siteId, int num);

    bool isInsert() const;
    int getSiteId() const;
    int getNum() const;
    char getValue() const;
    const std::vector<int> &getPos() const;
};

I don't understand where I am doing something wrong. BTW I am using CLion

Andrea Foderaro
  • 45
  • 1
  • 1
  • 12
  • 2
    What you have is a classical circular inclusion dependency. The file `NetworkServer.h` depends on `SharedEditor.h` which depends on `NetworkServer.h` which depend on... and so on. The usual solution to break such dependency circles is to don't include the header file and instead use *forward declarations*. For example in `NetworkServer.h` you don't really need to include `SharedEditor.h`, only forward declare the class as `class SharedEditor;`, then include the `SharedEditor.h` file in the *source* file. – Some programmer dude Sep 08 '19 at 09:48
  • Possible duplicate of [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – walnut Sep 08 '19 at 11:02

0 Answers0