• 12
• 12
• 9
• 10
• 13

# c++ class help

This topic is 2688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i have 2 classes

class class1{public:class1(const class2 &c){}};class class2{public:class2(){}};

the compiler give me error because the class2 isn't defined, how to fix that?

##### Share on other sites
Use a forward declaration for B or move the definition for class B above class A. See also: Organizing Code Files in C and C++.

##### Share on other sites
if i add "class class2;" at the top of the file the compiler give me again the errors

##### Share on other sites
Quote:
 Original post by _fastcallUse a forward declaration for B or move the definition for class B above class A. See also: Organizing Code Files in C and C++.

in fact i want to use the class1 in class2 and class2 in class1

##### Share on other sites
Quote:
 Original post by codder88if i add "class class2;" at the top of the file the compiler give me again the errors

What errors?

Quote:
 in fact i want to use the class1 in class2 and class2 in class1

Then post code like that, which demonstrates the problem.

##### Share on other sites
#pragma once#include "main.h"class CClass2;class CClass1{	public:		CClass1(const CClass2 &Class2) { this->SetVar(Class2.GetVar()); }		void SetVar(int val) { var=val; }		int GetVar() const { return var; }	private:		int var;};class CClass2{	public:		CClass2(const CClass1 &Class1) { var = 5; }		int GetVar() const { return var; }	private:		int var;};

i'm using visual c++ 2010 express and the compiler give me error C2027 and C2228

##### Share on other sites
Quote:
 CClass2(const CClass1 &Class1) { var = 5; }

You don't really use CClass1 in the constructor. Maybe if you explain a bit more about what you're trying to do.

##### Share on other sites
There's a few problems with that code.

First, you haven't fully defined CClass2 before using it in CClass1. Thus in the constructor where you call Class2.GetVar() you will get an error.

Second, you really should read the link on organizing code files. Otherwise you're going to end up with other difficulties going down this route where your class and method definitions are stepping on each others toes.

You should have a CClass1.h and a CClass2.h which do nothing more than this:

class CClass1{	public:		CClass1(const CClass2 &Class2);		void SetVar(int val);		int GetVar() const;	private:		int var;};

And then you should have a CClass1.cpp file which includes both CClass1.h and CClass2.h and actually defines the methods. You would use a forward declaration in one or both of your .h files as needed.

#include "CClass1.h"#include "CClass2.h"CClass1::CClass1(const CClass2 &Class2){    this->SetVar(Class2.GetVar());}void CClass1::SetVar(int val){    var=val;}int CClass1::GetVar(){    return var;}

##### Share on other sites
You have a circular reference. The only way to construct Class1 is to have a reference to Class2. The only way to construct Class2 is to have an existing Class1. This can never happen.

Tell us what your actual code is trying to do. Usually there is some kind of natural parent/child relationship as to which class should come first.

You can get the code to compile by moving the constructor definition into an implementation file. Essentially, the compiler will see this (once the preprocessor is finished):
// contents of main.h// contents of class2.hclass CClass2;class CClass1{	public:		CClass1(const CClass2 &Class2); // OK, forward declaration		void SetVar(int val) { var=val; }		int GetVar() const { return var; }	private:		int var;};// contents of class2.hclass CClass2{	public:		CClass2(const CClass1 &Class1) { var = 5; }		int GetVar() const { return var; }	private:		int var;};// implementation file class1.cpp// Couldn't do this earlier, requires a full class definition.CClass1::CClass1(const CClass2 &Class2){     this->SetVar(Class2.GetVar());}

Note that main.h is a sign of potential problems, depending on what you include in it. If you are only including external headers, then it shouldn't matter (but it might be faster if you consider making this a precompiled header). If it includes some of your own headers, then you can get additional compiler errors if you are not careful.

I recommend not having such headers reference your own files. Each header file should try to minimise the headers it references, using forward declarations where possible.