0

I'm having a problem with pointers. My background is Java and I've still not got the hand of using pointers. My error is:

syntax error: identifier 'StockController'

from this line:

virtual void visit(StockController* stock) = 0;

I've implemented my singleton. But now i'm looking to add the visitor pattern. I have a series of operations ill be applying to the collection of objects.

Visitor abstract class:

#ifndef VISITOR_H
#define VISITOR_H

#include "StockController.h"

class Visitor
{
public:
    virtual void visit(StockController* stock) = 0;
};

#endif

Singleton class:

#ifndef StockController_H
#define StockController_H

#include <iostream>
#include "Visitable.h"

using namespace std;

class StockController : public Visitable {
public:
    /*Returns an instance of the class */
    static StockController* getInstance();

protected:
    /* Prevent initialisation unless through get method */
    StockController();
    StockController(const StockController&);
    StockController& operator= (const StockController&);

private:

    /* Class instance */
    static StockController* _instance;

};

#endif

Visitable.h:

#ifndef VISITABLE_H
#define VISITABLE_H

#include "Visitor.h"

class Visitable {
public:
    virtual void accept(Visitor &visitor) = 0;

};

#endif
  • Where is Visitable.h and what line is giving you the error? – kcraigie Nov 01 '15 at 15:28
  • You didn't actually ask a question. For which file did the compiler report the error you mentioned? – tweej Nov 01 '15 at 15:29
  • Make sure `Visitable.h` does not include `Visitor.h` otherwise you have a circular include which there are 1000s of duplicates on this site. – drescherjm Nov 01 '15 at 15:31
  • Your code is pointer-infested. That's precisely because of your Java background, where pointers are necessary for every instance of a custom type. In C++, you are supposed to use *references* when you can and pointers when you have to. – Christian Hackl Nov 01 '15 at 15:34
  • Possible duplicate of [Resolve circular dependencies in c++](http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c) – drescherjm Nov 01 '15 at 15:43
  • Removing `Visitor.h` from `Visitor.h` generated additional errors. Since i'm using a `Visitor` object as a parameter of the accept method. –  Nov 01 '15 at 15:51
  • Use a forward declaration to remove the circular dependency. Please read and understand what a circular dependency is. – drescherjm Nov 01 '15 at 15:53
  • In `Visitable.h` put `class Visitor;` where you have `#include "Visitor.h"` – drescherjm Nov 01 '15 at 15:56
  • Thank you. So I was looking in the wrong place. –  Nov 01 '15 at 16:13

0 Answers0