-1

I have 3 files in my project, in the class Scene.h I declare a public static function which I want to use in the other file of my project.

  • main.cpp
#include "Scene.h"

int main(...){
      Scene scene("Chapter 3", 800, 600);
      ...
}
  • Scene.h
#include "GameObject.h"

class Scene
{
public:
      Scene(...)
      ~Scene()

    static void writeInLog(string str) 
    {
        //write str in log.txt
    }

    int mainLoop(){
        //a lot of thing
        GameObject a;
        //use this a
    }
};
  • GameObject.h
class GameObject
{
public:
      GameObject(...)
      ~GameObject()
     {
        Scene::writeInLog("GameObject has been destroy");
     }
     ...
};

But when I use the function in the class GameObject.h I have 2 errors :

  1. error c2653 Scene.h is not a class or namespace name.
  2. error C3861 identifier not found.

I think it's a problem of cyclic dependency, but I can't find where. What is my mistake ?

lufydad
  • 225
  • 2
  • 8
  • 4
    Voting to close as a typo. You have your includes backwards. `GameObject.h` needs to include `Scene.h`, not the other way around. – NathanOliver Oct 01 '19 at 14:09
  • why is `writeInLog` a member of `Scene` ? Every function that does not need to be a member makes the class better / easier to understand & maintain – 463035818_is_not_an_ai Oct 01 '19 at 14:09
  • If it possible, in `Scene.h` remove the include to `GameObject.h` and in `GameObject.h` add an include to `Scene.h`. If it doesn't, please post the complete files so we'll be able to help. – Coral Kashri Oct 01 '19 at 14:16
  • Sorry, I forget to precise that my class Scene use GameObject in his function. Therefore, I need to include GameObject.h in the class Scene. – lufydad Oct 01 '19 at 14:30
  • It should not even compile as its missing the `;` after the class declaration like `class bla { };` – Nidhoegger Oct 01 '19 at 14:40
  • @Nidhoegger My mistake, I just forget to wrote them here. (I edit it) – lufydad Oct 01 '19 at 15:02

1 Answers1

0

In this case, you will separate header file(.h) and implementation file(.cpp), Because the two classes use each other's methods.

Try this instead:


Scene.h

#pragma once
#include <string>

using namespace std;

class Scene
{
public:
    Scene(void);
    ~Scene(void);
    static void writeInLog(string str);
};

Scene.cpp

#include "StdAfx.h"
#include "Scene.h"

#include<iostream>


Scene::Scene(void)
{
}


Scene::~Scene(void)
{
}


void Scene::writeInLog(string str)
{
    cout<<str<<endl;
    //write str in log.txt

}

GameObject.h

#pragma once
class GameObject
{
public:
    GameObject(void);
    ~GameObject(void);
};

GameObject.cpp

#include "StdAfx.h"
#include "GameObject.h"

#include "Scene.h"


GameObject::GameObject(void)
{
}


GameObject::~GameObject(void)
{
    Scene::writeInLog("GameObject has been destroy");
}

I tested this by GameObject *g = new GameObject(); delete g; in main method.

I wish you success.

HSh
  • 29
  • 3
  • I'll try this thank you, but can you explain me the difference for a compiler between what you did, and what I did ? – lufydad Oct 01 '19 at 15:05
  • The flexibility of my code is to use the Scene class in the GameObject class as a member variable and vice versa, However, with a slight change in the code. It is better than my explanation to study this answer: https://stackoverflow.com/a/333964/9280076 – HSh Oct 01 '19 at 16:27
  • @KorelK If I didn't use `#include "Scene.h"` in GameObject.h file because it wouldn't be needed. If I want to use `Scene* m_scene;` in GameObject class as a member variable, I just need to use the `Class Scene;` code before defining the GameObject class(`class GameObject`) inside GameObject.h file. This can also be the case for using `#include "GameObject.h"` inside Scene.h file. If I put `#include "Scene.h"` inside GameObject.h file and also using `#include "GameObject.h"` inside Scene.h file therefore I would get an error message. – HSh Oct 01 '19 at 17:04