I'm using CodeBlocks for C++ and it's probably a compiler issue. I am learning using vector pointer and tried to create function that return vector pointer. Below is my code. It has compiled and executed before without using pointer. Now I try to have a function that returns pointer, but somehow it doesn't work and I couldn't figure out the errors. Please help.
error:
main.cpp|8|undefined reference to `RandomNum::RandomNum(int, int, int, int)
main.cpp|9|undefined reference to `RandomNum::getVecPointer()
main.cpp
#include <iostream>
#include "RandomNum.h"
using namespace std;
int main()
{
RandomNum rand(5, 5, 100, 1000); <----error
vector<float>* p = rand.getVecPointer();
cout << (*p)[0] << endl;
return 0;
}
RandomNum.h
#include <vector>
#ifndef RANDOMNUM_H
#define RANDOMNUM_H
class RandomNum
{
private:
int M, x, y, z; //M is the number of cells
std::vector <float> aVector;
public:
//constructor
RandomNum(int, int, int, int);
//generate random float between 0 and 1;
float unif();
//build a vector of random points
std::vector<float>* getVecPointer();
};
#endif
RandomNum.cpp
#include "RandomNum.h"
#include <cmath> //for trunc()
RandomNum::RandomNum( int MM,int xx, int yy, int zz )
{
//x, y, z are seeds, M is the number of random numbers to be generated [0,1]
M = MM;
x = xx;
y = yy;
z = zz;
}
float RandomNum::unif()
{
float tmp;
...
return(tmp - trunc(tmp));
}
std::vector<float>* RandomNum::getVecPointer()
{
int i ;
for (i = 0 ; i < M; i++)
{
float x = unif();
aVector.push_back(x);
}
return &aVector;
}