Reputation: 5064
I am trying to allocate space for a boost vector type in a class definition. I am not a good c++ programmer, but shown below is my best attempt. There are no error messages, but when I try to access the vector from my main function it believes that the vector has zero elements. I know this is because I did not tell the compiler how much space to allot when I declared the vector in the class definition, but I do not know how to do this without getting an error. I tried to circumvent this by telling it how big I wanted it in the constructor, but I know the compiler treats this as a redefinition that does not exist outside of the scope of the constructor. Can someone lead me in the right direction? Thanks in advance.
namespace ublas = boost::numeric::ublas;
class Phase
{
ublas::vector<cdouble> lam;
public:
// Constructor:
Phase()
{
ublas::vector<cdouble> lam(2);
for(int i = 0; i < 2; i++)
{
lam(i) = 1.0;
}
}
// Destructor:
~Phase() {}
// Accessor Function:
ublas::vector<cdouble> get_lam() { return lam; }
};
Upvotes: 1
Views: 635
Reputation: 33390
In your constructor you are creating a local variable lam
that shadows the class variable lam
. You want to initialize the vector in the constructor's initialization list:
Phase() : lam(2)
{
for(int i = 0; i < 2; i++)
{
lam(i) = 1.0;
}
}
This calls the vector
constructor you want as the class is being initialized, instead of the default constructor for the class.
Upvotes: 7