Defining vector in header file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Defining vector in header file

  1. #1
    Join Date
    Mar 2007
    Posts
    20

    Defining vector in header file

    Suppose I have the following sample header file test.h

    Code:
    #include "myCommon.h"
    
    class Test
    {
    public:
      Test();
    
      vector<vector<vector<double>>> vI3(dim1, vector<vector<double>> (dim2, vector<double> (dim2, 0.0f)));
    
    private:
      fillVector();
    }
    In above test.h dim1 and dim2 are defined in a different header file, i.e. myCommon.h

    Code:
    const long dim1 = 40;
    
    enum dimVector {
      RED,
      GREEN,
      dim2
    };
    However, it gives the errors when I compile: variable "dim1" is not a type name and for variable "dim2" it complains about a duplicate parameter name. Any solutions?

    The declarations of dim1 and dim2 should stay in myCommon.h. They can also be defined in myCommon.cpp if needed, but can't go into test.h.

    Thanks.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: Defining vector in header file

    I think you should put some include guards in that "myCommon.h" file of yours.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Defining vector in header file

    Quote Originally Posted by bar_ba_dos View Post
    Suppose I have the following sample header file test.h
    You have so many things wrong.
    Code:
    #include "myCommon.h"
    
    class Test
    {
    public:
      Test();
    
      vector<vector<vector<double>>> vI3(dim1, vector<vector<double>> (dim2, vector<double> (dim2, 0.0f)));
    
    private:
      fillVector();
    }
    So how is the compiler supposed to know what "vector" is? You never state #include <vector> anywhere.

    Second, this is not a declaration:
    Code:
      vector<vector<vector<double>>> vI3(dim1, vector<vector<double>> (dim2, vector<double> (dim2, 0.0f)));
    What is that code supposed to do when you place it in the class declaration? That line of code belongs in a function.

    The bottom line is this:

    When you create a header file, you're supposed to #include anything it needs to compile successfully without any "help" from outside. For example, this one line CPP file must compile without error:
    Code:
    #include "test.h"
    Try to compile that one line of code. If it doesn't compile, you go into test.h and fix the errors.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Dec 2009
    Posts
    145

    Re: Defining vector in header file

    Use typdef for better readability
    typedef std::vector<double> V_DOUBLE;
    typedef std::vector<V_DOUBLE> VV_DOUBLE;
    typedef std::vector<VV_DOUBLE> VVV_DOUBLE;
    Code:
    class Common
    {
      long dim1 = 40;
      
      enum dimVector {
      RED,
      GREEN,
      dim2
      };
      //provide public get/set for dim
    };
    Code:
    class Vec
    { 
       void Init()
       {//do something;
          //  initialize your vector
       }
    public: 
       Vec()
      {
          //call Init
       }
    };
    I might probably do something like that if I were in your current situation.
    Last edited by Ledidas; February 17th, 2012 at 06:33 AM.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center