Initializing Vector of Objects with NULL
class A; //forward defined in class B.h
class B
{
private:
vector<B> v1;
vector<B> v2[3];
public:
void Display()
{
cout<<v1[0].display()
}
}
I get error message forward Declaration of class A;
What is going on here. Is there a way I could initialize all the vectors of type B to be set to NULL in the constructor?
Re: Initializing Vector of Objects with NULL
When posting code, please format the code properly first and use code tags. Go advanced, select the code and click '#'.
In the code snippet shown, class A is not used and class B has vectors of type class B? Please post the smallest complete program that shows the problem rather than just a small part.
Re: Initializing Vector of Objects with NULL
Quote:
Originally Posted by
2kaud
When posting code, please format the code properly first and use code tags. Go advanced, select the code and click '#'.
In the code snippet shown, class A is not used and class B has vectors of type class B? Please post the smallest complete program that shows the problem rather than just a small part.
I am new to this and not sure what exactly you mean.
Anyways, All I want is to create a vector of objects of a particular class. What is the difference between
vector<B> *v1
and
vector<B*> v1?
Re: Initializing Vector of Objects with NULL
Okay here I go again:
I have:
Code:
#include "A.h"
class A; //Forward Declared
class B
{
vector <A> v1;
public:
B(A &a)
{ ...
}
v1.push_back(obj);
cout<<*v1[0].display();
};
I am getting an error of forward Declaration of 'class A'
What is the best way to resolve this problem.
Re: Initializing Vector of Objects with NULL
v1 is a pointer to a vector of type B
v1 is a vector of type pointer to B
If class A is defined in A.h and is included before any reference to class A as in your code snipped, then the forward declaration of class A is not needed so just remove it.
Re: Initializing Vector of Objects with NULL
Quote:
Originally Posted by
2kaud
v1 is a pointer to a vector of type B
v1 is a vector of type pointer to B
If class A is defined in A.h and is included before any reference to class A as in your code snipped, then the forward declaration of class A is not needed so just remove it.
Thanks. However, I need this forward declaration because I have a class B using class A as well. It's like a cyclic relationship
Re: Initializing Vector of Objects with NULL
Quote:
Originally Posted by
apb210
Code:
#include "A.h"
class A; //Forward Declared
class B
{
vector <A> v1; //<<<--need definition of A
public:
B(A &a) // <<<--this will compile, but you can't do much with variable a since you don't have it's definition
// you can only use a as an anonyous definition (you can take it's pointer that's about it).
{ ...
}
v1.push_back(obj);
cout<<*v1[0].display();
};
If you do forward defines:
you can ONLY declare pointers and references of forwarded classes/structs.