0

Well I have 2 classes in 2 header and 2 cpp files

#class1.h
#ifndef class1_h
#define class1_h
class myclass;
#endif

#class2.h
#ifndef class2_h
#define class2_h
class anotherclass;
#endif

#class1.cpp

class myclass
{
    anotherclass test1;
}

#class2.cpp

class anotherclass
{
    myclass test2;
}

And of course a simple main Well I think it's obvious why this thing wont even compile my question here is how to make it compile?
Having the classes in different files is mandatory..

brunohdaniel
  • 612
  • 5
  • 18
Chris Skan
  • 11
  • 4
  • What is the third class you mention in the text? The code you've shown contains only two classes. – mkrieger1 Dec 06 '17 at 23:15
  • Possible duplicate of [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – mkrieger1 Dec 06 '17 at 23:18
  • Well didnt write it well just tell me how to make this code work ignore the user-defined type thing – Chris Skan Dec 06 '17 at 23:19
  • As currently defined, Your classes would be of an infinite size (`myclass` instance contains `anotherclass` instance, which contains `myclass` instance, which contains `anotherclass` instance, ad infinitum..). Can you see a problem in that? Solution? Pointers. – Algirdas Preidžius Dec 06 '17 at 23:28
  • Not directly related to the question but you may want to use "#pragma once" at the top of the file rather than the old c style #ifndef #define #endif – Max Rahm Dec 07 '17 at 00:16

2 Answers2

1

If the class definitions (the bodies that is) must be in separate files then the members of the declared type can only be a pointer (or a reference but that is unusual). You would then need a function in the source file where the class is actually defined that returns a pointer-to-instance of the type.

MyClass.h:

#if !defined(MYCLASS_H)
#define MYCLASS_H
class MyClass;
MyClass * CreateMyClass();
#endif

OtherClass.h:

#if !defined(OTHERCLASS_H)
#define OTHERCLASS_H
class OtherClass;
OtherClass * CreateOther();
#endif

myclass.cpp

#include "otherclass.h"
class MyClass
{
OtherClass * ptrOther;
public:
MyClass()
: ptrOther(CreateOther())
{}
};

otherclass.cpp:

#include "myclass.h"
class OtherClass
{
MyClass * ptrMyClass;
public:
OtherClass()
: ptrMyClass(CreateMyClass())
{}
};

Note that doing things this way you would not actually be able to do anything with ptrOther or ptrMyClass ((possibly not even free them correctly), It would be much more typical to place the class definitions in the header and separate out the member definitions (for functions and statics), like the following:

MyClass2.h:

#if !defined(MYCLASS2_H)
#define MYCLASS2_H
#include "OtherClass.h"
class MyClass2
{
OtherClass * ptrOther;
public:
MyClass2();
};
#endif

cMyClass.cpp:

#include "MyClass2.h"
MyClass2::MyClass2()
: ptrOther(CreateOther())
{}
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
-1

Header files define class interfaces. Source code '.cpp' files are using for the implementation. Your source code has the implementation and the class interface.

DannyK
  • 1,342
  • 16
  • 23
  • (1) You've misinterpreted the code in the question: the .cpp files *do* only contain the class definitions and the .h files only contain the declarations; (2) .cpp and .h can contain whatever they want, using them the way you describe is just a convention. – mkrieger1 Dec 07 '17 at 12:53