Jump to content
  • Advertisement
Sign in to follow this  

Help with this forward declaration problem-ish

This topic is 3050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I trying to learn the visitor pattern. So I am trying to implement a rather simple and stupid example, so no need to point that out. Instead of separating the classes into different file, its rather a small class so I just put everything into one file. Here is the main.cpp :
#include <iostream>
#include "Print.h"

using namespace Print; //template print functions
using namespace std;

class PrintVisitor;
class Char;
class Integer;
class Float;
struct VisitElem;

struct VisitElem {	
	virtual void visit(const PrintVisitor& pv)const = 0;

class Char : public VisitElem{
	char ch;
	Char(): ch(0){}
	Char(char c) : ch(c){}
	char elem(){ return ch; }
	void visit(const PrintVisitor& pv)const{ pv.visit(*this); }
class Integer{
	int i;
	Integer() : i(0){}
	Integer(int in) : i(in){}
	int elem(){ return i; }
	void visit(const PrintVisitor& pv)const{ pv.visit(*this); }
class Float{
	float fl;
	Float() : fl(0.0f){}
	Float(float f) : fl(f){};
	float elem(){ return fl; }
	void visit(const PrintVisitor& pv)const{ pv.visit(*this); }
class PrintVisitor{
	void visit(Char ch)const{ print(ch.elem());}
	void visit(Integer i)const{ print(i.elem());}
	void visit(Float fl)const{ print(fl.elem());}
int main()
	return 0;

It gives me some errors because of the visit(...) method for each class. Here is the errors :
error C2027: use of undefined type 'PrintVisitor'
error C2228: left of '.visit' must have class/struct/union
I see why the error msg is. Its because the compiler does not know yet that PrintVisitor has a visit method. But I am not sure how to solve this problem. Any help is appreciated.

Share this post

Link to post
Share on other sites
Don't define the functions inline in the class definitions. Define them after class definitions.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!