0

I have a Data class that contains a VectorXf object from the Eigen Template Library. This vector object has a constructor that accepts an int defining how much memory to allocate. However, this number is not known at compile time.

Anyway, what I am trying to do is declare the VectorXf object in my Data class header file, then construct that VectorXf object during construction of my Data object. I'm new to c++, so I am a little confused on how to do this. I keep getting an error that says undefined reference to Data::vector(int) What am I doing wrong?

Here is the Header file Data.h for my Data class:

#ifndef DATA_H
#define DATA_H
#include <Eigen/Eigen>
using namespace Eigen;
using namespace std;

class Data
{
    public:
        Data(string file);
        virtual ~Data();
        void readFile(string file);

    private:
        VectorXf vector(int n);

};

#endif // DATA_H

So, as you can see from the header file above, a data object contains a vector object that takes an integer as a parameter.

Here is the Data.cpp file:

#include "Data.h"
#include <iostream>
#include <string>
#include <Eigen\Eigen>
#include<fstream>

using namespace Eigen;
using namespace std;


Data::Data(string file)
{
    readFile(file);

}

void Data :: readFile(string file){

    int n = ... // read file length using fstream
    vector(n); //construct the VectorXd object that was declared in Data.h

}   

Data::~Data()
{
    //dtor
}

For clarity, what needs to happen in my main function:

   Data data("myfile.txt")

which creates a Data object. Then a file length is read since the Data constructor calls readFile(), which determines the appropriate length to construct the VectorXf object that is a Data class member. What is the proper way of referring to and constructing my class member objects - VectorXf vector in this case.

Mike James Johnson
  • 724
  • 2
  • 8
  • 29
  • Sorry to dupe hammer your question with this link. I know you didn't specifically ask that. But however, the answers in the duplicate should give you every information necessary to get your code working. – πάντα ῥεῖ Apr 19 '16 at 17:04
  • @πάνταῥεῖ Thanks for the link, I can use the information there to solve my problem in an ALTERNATIVE way. However, I am not seeing an answer to my question. I am not passing the `int n` parameter through the constructor of the `data` object. Therefore I can not use the member initialization list. I require a method to be called before that `int n` can even be determined. So, is this not possible? – Mike James Johnson Apr 19 '16 at 17:30
  • @πάνταῥεῖ Do you mean to say that I must pass `n` through the constructor so that I can use member initialization? If that is true, then I would have to read the file to determine the length before I even create my Data object. I am trying to implement the file read within the Data class THEN construct the member `VectorXf` object – Mike James Johnson Apr 19 '16 at 17:30
  • found my answer here : http://stackoverflow.com/questions/24702484/is-there-a-way-to-late-initialize-a-member-variable-a-class-in-c – Mike James Johnson Apr 19 '16 at 17:49
  • 1
    Well, I've reopened your question (maybe to let it get closed as a dupe of the question you've been researching yourself). – πάντα ῥεῖ Apr 19 '16 at 17:51

0 Answers0