Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


c++ class help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 10 November 2010 - 08:09 AM


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?

Sponsor:

#2 fastcall22   Crossbones+   -  Reputation: 5319

Like
0Likes
Like

Posted 10 November 2010 - 08:10 AM

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

#3 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 10 November 2010 - 08:24 AM

if i add "class class2;" at the top of the file the compiler give me again the errors

#4 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 10 November 2010 - 08:26 AM

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

#5 rip-off   Moderators   -  Reputation: 9563

Like
0Likes
Like

Posted 10 November 2010 - 08:58 AM

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.

#6 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 10 November 2010 - 10:06 AM


#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

#7 Buckeye   GDNet+   -  Reputation: 9703

Like
0Likes
Like

Posted 10 November 2010 - 10:12 AM

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.


#8 CaspianB   Members   -  Reputation: 308

Like
0Likes
Like

Posted 10 November 2010 - 10:16 AM

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;
}






#9 rip-off   Moderators   -  Reputation: 9563

Like
0Likes
Like

Posted 10 November 2010 - 10:17 AM

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.

#10 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 10 November 2010 - 10:23 AM

ok... thank's guys :D




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS