0

This is driving me insane, I have a class called Model, a class called View, and a header file for something GameCommand. I've included the right header guards and everything as far as I can tell but I keep getting an unknown type name error

Model.h

#ifndef MODEL_H
#define MODEL_H

class Model
{
public:
    Model(); //default constructor
};

#endif

Model.cpp

#include "Model.h"
#include <iostream>
using namespace std;

Model::Model() //default constructor
{
whatever
}

View.h

#ifndef VIEW_H
#define VIEW_H

class View
{
public:
    View(); 
};

#endif

View.cpp

#include "View.h"
#include <iostream>
using namespace std;

View::View()
{ whatever
}

GameCommand.h

#ifndef GAMECOMMAND_H
#define GAMECOMMAND_H
#include "Model.h"
#include "View.h"

void DoGoCommand(Model&, View&);
error: unknown type name 'Model'
void DoGoCommand(Model&, View&);
                 ^
error: unknown type name 'View'
void DoGoCommand(Model&, View&);
                         ^

I feel like I've tried everything, is there something I am just not seeing here?

Naq Z
  • 39
  • 1
  • 7
  • Is those errors you show the *full* and *complete* copy-pasted output you get when building? – Some programmer dude Nov 25 '19 at 13:38
  • There's multiple functions within GameCommand that use Model and View and the only errors I am getting are all unknown type name 'Model' or unknown type name 'View' erros for each function that uses them – Naq Z Nov 25 '19 at 13:39
  • 2
    Do either of `Model.h` or `View.h` include each other? – drescherjm Nov 25 '19 at 13:41
  • 2
    You aren't probably showing the whole code. At least I can't reproduce with what you gave. – AProgrammer Nov 25 '19 at 13:41
  • 1
    In `GameCommand.h` you could probably use forward declarations for `Model` and `View` and not include the headers. – drescherjm Nov 25 '19 at 13:43
  • Model.h does include View.h, this a small part of a very large code so I'm not sure how much more to include since this seems to be my only error when I try to compile – Naq Z Nov 25 '19 at 13:43
  • 1
    Wait -- Are these really *compiler* errors, or errors from some tool that analyzes the source code, like Intellisense or similar? – PaulMcKenzie Nov 25 '19 at 13:43
  • 2
    That means you have a circular include dependency. One usually solves it by *forward declarations* as mentioned. – Some programmer dude Nov 25 '19 at 13:44
  • [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Nov 25 '19 at 13:45
  • "this a small part of a very large code so I'm not sure how much more to include" try the opposite: include as little as possible, but make sure you can reproduce the error with the small example – 463035818_is_not_an_ai Nov 25 '19 at 13:45
  • 2
    @RaunaqZamal Well if `Model.h` includes `View.h`, then your initial post of the code is misleading. That's why my previous comment was given concerning a compiler error as opposed to an error generated by a tool that analyzes the source code. – PaulMcKenzie Nov 25 '19 at 13:46
  • What is this `whatever`? – Lightness Races in Orbit Dec 11 '19 at 11:31

1 Answers1

0

You can try to forward declare the classes.

class Model;

class View;

in your GameCommand.h

Siddharth
  • 321
  • 1
  • 5