CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2014
    Posts
    5

    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?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  3. #3
    Join Date
    Feb 2014
    Posts
    5

    Re: Initializing Vector of Objects with NULL

    Quote Originally Posted by 2kaud View Post
    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?

  4. #4
    Join Date
    Feb 2014
    Posts
    5

    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.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Initializing Vector of Objects with NULL

    Code:
    vector<B> *v1;
    v1 is a pointer to a vector of type B

    Code:
    vector<B*> v1;
    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)

  6. #6
    Join Date
    Feb 2014
    Posts
    5

    Re: Initializing Vector of Objects with NULL

    Quote Originally Posted by 2kaud View Post
    Code:
    vector<B> *v1;
    v1 is a pointer to a vector of type B

    Code:
    vector<B*> v1;
    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

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Initializing Vector of Objects with NULL

    Quote Originally Posted by apb210 View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured