-
February 27th, 2014, 03:08 PM
#1
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?
-
February 27th, 2014, 03:29 PM
#2
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 27th, 2014, 04:49 PM
#3
Re: Initializing Vector of Objects with NULL
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?
-
February 27th, 2014, 05:23 PM
#4
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.
-
February 28th, 2014, 08:01 AM
#5
Re: Initializing Vector of Objects with NULL
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.
-
February 27th, 2014, 05:32 PM
#6
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.
Last edited by 2kaud; February 27th, 2014 at 05:35 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 27th, 2014, 06:06 PM
#7
Re: Initializing Vector of Objects with NULL
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|