-
February 17th, 2012, 05:02 AM
#1
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.
-
February 17th, 2012, 05:29 AM
#2
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 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.
-
February 17th, 2012, 05:33 AM
#3
Re: Defining vector in header file
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
-
February 17th, 2012, 07:24 AM
#4
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 07: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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|