I am trying to grasp the use of the object-oriented features of C++ in financial models and decided to practice by copying and running the source codes in a forthcoming book. One of the source codes includes a header file where "typedef" is used as follows:
When I attempt to compile the source code, I get an error message which I interpret as saying that the second use of typedef is incorrect. The message refers to the allocator for the vector template.
My question is "Is the second use of typedef invalid?". If so, what ought to be used?
I'm copying the error message below; I've edited out some portions which I believe was unnecessary. The references to other header files are part of the project. The message seems to refer to a problem with the use of vector<double> in the typedef of Matrix. The C++ IDE that I'm using is wxDev-C++.
Thank you once again.
Here is the error message:
In file included from c:\program files (x86)\dev-cpp\mingw32\bin\../lib/gcc/mingw32/4.6.1/include/c++/vector:65:0, from Matrix.h
from BSModel02.h from Matrix.h:4, from BSModel02.h
from BSModel02.h from Matrix.h:4, from BSModel02.h
from PathDepOption05.h from BSModel02.h:4, from PathDepOption05.h
from main23.cpp from PathDepOption05.h:4, from main23.cpp
from main23.cpp
In member function 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int, _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >]':
instantiated from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = int, _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector<_Tp, _Alloc>::allocator_type = s
instantiated from here no matching function for call to 'std::vector<std::vector<double> >::_M_fill_initialize(std::vector<std::vector<double> >::size_type, int&)'
candidate is:
void std::vector<_Tp, _Alloc>::_M_fill_initialize(std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = std::vector<double>, _Alloc = std::allocator<std::vector<double> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp,
no known conversion for argument 2 from 'int' to 'const value_type& {aka const std::vector<double>&}'
can you plz show us the complete code? I think I know what the error might be about but i'm not sure, and I don't even know if that's possible, so please show us the COMPLETE code and the COMPLETE error message
As suggested, I'm including to this reply the complete code that contains the header files, along with the corresponding cpp files.
Also the complete error message follows. The message is in the format of a csv file so that it is easier to read in a spreadsheet program. There are 3 headings: Line, File, Message.
BSModel::BSModel(Vector S0_, double r_, Matrix C_)
{
S0 = S0_; r = r_; C = C_; srand(time(NULL));
int d = S0.size();
sigma.resize(d);
for (int j = 0; j < d; j++) sigma[j] = sqrt(C[j]^C[j]);
}
void BSModel::GenerateSamplePath( double T, int m, SamplePath &S)
{
Vector St = S0;
int d = S0.size();
for( int k = 0; k < m; k++)
{
S[k] = St*exp((T/m)*(r + (-0.5)*sigma*sigma) + sqrt(T/m)*(C*Gauss(d)));
St = S[k];
}
}
Matrix is just a typedef of vector<vector<double>> so that line is equivalent to vector<vector<double>> C(d,d);
vector<vector<double>> doesn't have a constructor that takes 2 integers as argument. I think this is what you tried to do: vector<vector<double>> C(d,vector<double>(d));
==> Matrix C(d,Vector(d));
Yeeees, that's what I tought. As peter said, you used an int where a Vector(aka vector<double>) was expected! just do what peter said in the last line of his reply
I thank you all for all your time and effort to resolve this programming error. I'm surprised that the authors of the textbook were not aware of this flaw in their code.
I again thank you, the experts.
In defense of the authors, they do say that they have taken some liberty with the source codes so that they would be easier to follow for the student. They admit that there ought to be more use of "private", but again for the sake of clarity and shorter source codes, they've omitted the proper use of "private" and of "protected".