Objects for Classes Not Recognized As A "Type"

I'm running into a fairly annoying and somewhat inconsistent(at least as far as I know) bug involving a program where I have to declare an object for one class in another. This is done on three different occasions in the program in question and is only able to compile without error for the first instance of this. Every other instance is met with an error that states "[class name] does not name a type" (In case there's any confusion, it doesn't actually say "[class name]". It says the title of the class that I tried to declare an object for on the line where there is an error). Below is an example of what exactly I'm trying to do and am not succeeding at.

Main.cpp
1
2
3
4
5
6
7
8
9
10
  #include "Loop.h"

  int main(){

  Loop loop;

  loop.programLoop();

  return 0;
}


Header file for the main class. Essentially just loops through the functions to be used. (Loop.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef LOOP_H
#define LOOP_H
#include "Class1.h"
#include "Class2.h"

class Loop{
    public:
        Loop();
        void progLoop();

    private:
        void conf();

        bool _run = true;
        char _endLoop;

        Class1 _c1;
        Class2 _c2;
};

#endif 


Loop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "Loop.h"
#include <cstdio>
#include <conio.h>

Loop::Loop(){
}

void Loop::progLoop(){
    ...

    while(_run == true){

        ...

        conf();
    }

}

void Loop::conf(){
    printf("Would you like to continue?\n");
    _endLoop = getch();

    switch(_endLoop){

case 'y':
    _run = true;
    break;
case 'n':
    _run = false;
    break;
default:
    printf("invalid input\n");
    break;
    }
}


First class header file (Class1.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CLASS1_H
#define CLASS1_H

... 
#include "Class2.h"

class Class1
{
    public:
        Class1();

        ...

    private:

        ...

        Class2 _c2;

};

#endif 


Class1.cpp
1
2
3
4
5
6
7
8
9
10
#include "Class1.h"

...

Class1::Class1(){
}

...
//some functions in original code set the values of variables in Class2 and use the return values of those variables


Header for the second class(Class2.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CLASS2_H
#define CLASS2_H
#include "Class1.h"

class Class2
{
    public:
        P1();

    ...

    private:

    ...

    Class1 _c1;
};

#endif 


Class2.cpp
1
2
3
4
5
6
7
8
#include "Class2.h"

...

Class2::Class2(){
}

...


I can provide the full source code if my example doesn't provide enough information. However, since my code most likely exceeds the limit of the length of a post on this site, they will be shown via links to a site where the files are being stored.
idk initialize _run in the constructor? :?
Last edited on
An instance of Class1 has an instance of Class2 which has an instance of Class1 which has an instance of Class2 which has an instance of Class1 which has an instance of Class2...
I think I see where the problem is now, cire. The problem is that I need an instance in the scope of both classes in order for this to work. How could I do this?
Pointers.
Topic archived. No new replies allowed.