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.
Re: Defining vector in header file
I think you should put some include guards in that "myCommon.h" file of yours.
Re: Defining vector in header file
Quote:
Originally Posted by
bar_ba_dos
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:
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
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.