Sign in to follow this  

Forward Declaration

This topic is 4745 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

why this code gives error C2079: 'A' uses undefined class 'a' ??
#ifndef B_H
#define B_H

#include"1.h"

class a;

class b{
	b(){ }
	a A;//this line
};

#endif

//second file

#ifndef A_H
#define A_H

#include"2.h"

class a{
public:
	a(){ }
private:
	b B;//and sometimes this line 

};

#endif

Share this post


Link to post
Share on other sites
You can't define a class that has a member that isn't defined yet. Among other things, the compiler needs to know exactly how big that member is, before it knows how to define the whole class.

And seriously, look at what you're trying to do. Class "a" includes an object "b". That object "b" includes an object "a". That object "a" includes an object "b". Etc etc etc.

The answer: make those member variables pointers. It IS okay for you to define a class that contains a pointer to something you haven't defined yet.

Share this post


Link to post
Share on other sites
You can't do that. If A has a B member variable then B can't have an A member variable; it would require A and B to be both infinite in size. Also, if all you have is a forward declaration then you can only use pointers or references to the type.

Share this post


Link to post
Share on other sites
then why it keeps telling me that class bSet Doesnt have a constructor ??!!

#ifndef ELEMENT_H
#define ELEMENT_H
#include<iostream>
using namespace std;

class bSet;
class element{
enum Type { Number = 10 , Set = 20 };
public:

Type type;
bSet * bSetElement;
int number;


element(const int & num):number(num),type(Number) { }
element(const bSet & s):type(Set)
{
bSetElement=new bSet(s);//this line !!!

}



};


#endif

///////////////////////////////////////////////


ifndef bSet_H
#define bSet_H

#include<vector>
using namespace std;

#include"element.h"
typedef vector<element> elementVector;

class bSet{
public:
bSet(){ }
bSet(const bSet & s );
const int size() { return elements.size();}
element operator[](const int & subScript);
void insert(const element & e);
private:
elementVector elements;
};


#endif




Share this post


Link to post
Share on other sites
That's because at that point, the compiler has no idea what a bSet even is, so how can it generate the code to construct it? The key is that your H file element.h can not include bSet.h, because that would be circular. However, element.cpp could easily include both h files. Thus that is the way to do it: Put the code for your functions in a cpp file.

Share this post


Link to post
Share on other sites
Forward declarations are useful if your useing hte type as a pointer as a pointer is a known size for the compiler. The moment the compiler has to instaniate that class then it needs to know about the class and you'll need to include your .h for that class.

Cheers
Chris

Share this post


Link to post
Share on other sites

This topic is 4745 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