Sign in to follow this  

c++ class help

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.h

class 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.h

class 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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this