c++ class help

Started by
8 comments, last by codder88 13 years, 5 months ago

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?
Advertisement
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++.
if i add "class class2;" at the top of the file the compiler give me again the errors
Quote:Original post by _fastcall
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++.


in fact i want to use the class1 in class2 and class2 in class1
Quote:Original post by codder88
if 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.
#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
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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;}
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.
ok... thank's guys :D

This topic is closed to new replies.

Advertisement