I am studying visiting patterns. However, the following error occurs. not defined. The cat class only has its own name and age. agevisitor and namevisitor, which inherit Visitor interface, output age and name.
I tried forward declaration, but the error of C2027 appears as it is. help me :(
that's my code
#include <iostream>
using namespace std;
class Visitor;
class Cat
{
public:
Cat(string name, int age) : name_(name), age_(age) {}
void Speak() {
cout << "meow" << endl;
}
void Accept(Visitor* visitor) {
cout << "use implementation of visitor" << endl;
visitor->visit(this); // #undefined type error
}
public:
string name_;
int age_;
public:
friend class AgeVisitor;
friend class NameVisitor;
};
class Visitor
{
public:
virtual void visit(Cat* cat) {}
};
class AgeVisitor : public Visitor
{
public:
virtual void visit(Cat* cat) {
cout << "cat is " << cat->age_ << "years old" << endl;
}
};
class NameVisitor : public Visitor
{
public:
virtual void visit(Cat* cat) {
cout << "cat's name is " << cat->name_ << endl;
}
};
int main()
{
Cat* first_cat = new Cat{ "kitty", 5 };
Visitor* age_visitor = new AgeVisitor();
first_cat->Accept(age_visitor);
}
Severity Code Description Project File Line Suppression State Error C2027 use of undefined type 'Visitor' DesignPattern C:\Users\sueng\source\repos\DesignPattern\DesignPattern\DesignPattern.cpp 18
Cat::acceptafter the definition ofVisitorVisitorclass is available when you use its members. Otherwise the compiler will only know that theVisitorclass exists, but not anything about its members or its size.