我的头文件中的错误 - 错误:"QSMatrix"没有命名类型,如何解决此问题?
Error in my header file - error: 'QSMatrix' does not name a type, how do I fix this issue?
我有两个类,其中一个被声明为朋友类。我正在尝试在我的第一堂课中编写一个使用我的第二类的函数,但我收到此错误:
错误:"QSMatrix"未命名类型
这是我的.h文件:
#include <vector>
#ifndef LSM_H
#define LSM_H
class LSM {
public:
// Overload Constructor
LSM(const double, const double, const double, const int, const int, const double, const double, const int, const int);
// Destructor
~LSM();
// Print contents of vector
void print(std::vector<double>);
// Generate the Laguerre Polynomials
QSMatrix<double> Laguerre(int, std::vector<double>);
// Generate M paths of stock prices (Geometric Brownian Motion)
std::vector<double> GBM(const int, const int, const double, const double, const double, const double, const double);
// Payoff of call option
std::vector<double> callPayoff(std::vector<double>, const double);
private:
// Member variables
double new_r;
double new_q;
double new_sigma;
int new_T;
int new_N;
double new_K;
double new_S0;
int new_M;
int new_R;
};
//////////////////////////////// Used Matrix Class From Quant Start /////////////////////////////////////////////////////////////////////////////
template <typename T> class QSMatrix {
friend class LSM;
private:
std::vector<std::vector<T> > mat;
unsigned rows;
unsigned cols;
public:
QSMatrix(unsigned _rows, unsigned _cols, const T& _initial);
QSMatrix(const QSMatrix<T>& rhs);
virtual ~QSMatrix();
// Operator overloading, for "standard" mathematical matrix operations
QSMatrix<T>& operator=(const QSMatrix<T>& rhs);
// Matrix mathematical operations
QSMatrix<T> operator+(const QSMatrix<T>& rhs);
QSMatrix<T>& operator+=(const QSMatrix<T>& rhs);
QSMatrix<T> operator-(const QSMatrix<T>& rhs);
QSMatrix<T>& operator-=(const QSMatrix<T>& rhs);
QSMatrix<T> operator*(const QSMatrix<T>& rhs);
QSMatrix<T>& operator*=(const QSMatrix<T>& rhs);
QSMatrix<T> transpose();
// Matrix/scalar operations
QSMatrix<T> operator+(const T& rhs);
QSMatrix<T> operator-(const T& rhs);
QSMatrix<T> operator*(const T& rhs);
QSMatrix<T> operator/(const T& rhs);
// Matrix/vector operations
std::vector<T> operator*(const std::vector<T>& rhs);
std::vector<T> diag_vec();
// Access the individual elements
T& operator()(const unsigned& row, const unsigned& col);
const T& operator()(const unsigned& row, const unsigned& col) const;
// Access the row and column sizes
unsigned get_rows() const;
unsigned get_cols() const;
// Print the matrix
void print() const;
};
#endif
以下是关联.cpp文件:
#include <iostream>
#include <vector>
#include <random>
#include <time.h>
#include <math.h>
#include "LSM.h"
LSM::LSM( const double r, const double q, const double sigma, const int T, const int N, const double K, const double S0, const int M, const int R){
new_r = r;
new_q = q;
new_sigma = sigma;
new_T = T;
new_N = N;
new_K = K;
new_S0 = S0;
new_M = M;
new_R = R;
/*
std::vector<double> v;
v = GBM(new_M, new_N, new_T, new_r, new_q, new_sigma, new_S0);
QSMatrix<double> A(2,2,0);
for (int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
A(i,j) = 1;
}
}
A.print();
*/
std::vector<double> V;
for(int i = 0; i < 4; i++){
V.push_back(rand());
std::cout << V[i] << std::endl;
}
}
LSM::~LSM(){
}
void LSM::print(std::vector<double> vector){
for (unsigned int i = 0; i < vector.size(); i++){
std::cout << vector[i] << std::endl;
}
}
std::vector<double> LSM::callPayoff(std::vector<double> S, const double K){
std::vector<double> V;
for(unsigned int i = 0; i < S.size(); i++){
if(S[i] - K > 0){
V.push_back(S[i] - K);
} else {
V.push_back(0.0);
}
}
return V;
}
std::vector<double> LSM::GBM(const int M, const int N, const double T, const double r, const double q, const double sigma, const double S0){
double dt = T/N;
std::vector<double> Z;
std::vector<double> S;
S.push_back(S0);
std::mt19937 e2(time(0));
std::normal_distribution<double> dist(0.0, 1.0);
for(int i = 0; i < M; i++){
Z.push_back(dist(e2));
}
double drift = exp(dt*(r-0.5*sigma*sigma));
double vol = sqrt(sigma*sigma*dt);
for(int i = 1; i < M; i++){
S.push_back(S[i-1] * drift * exp(vol*Z[i]));
}
return S;
}
QSMatrix<double> LSM::Laguerre(const int R, std::vector<double> X){
int n = X.size();
int m = R + 1;
QSMatrix<double> poly(n,m,0);
for (int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if (R == 1){
poly(i,0) = 1.0;
poly(i,1) = -X[i] + 1.0;
}
else if (R == 2){
poly(i,0) = 1.0;
poly(i,1) = -X[i] + 1;
poly(i,2) = 0.5 * (2.0 - 4.0*X[i] + X[i]*X[i]);
}
else if (R == 3){
poly(i,0) = 1.0;
poly(i,1) = -X[i] + 1;
poly(i,2) = 0.5 * (2.0 - 4.0*X[i] + X[i]*X[i]);
poly(i,3) = 1.0/6.0 * (6.0 - 18.0*X[i] + 9.0*X[i]*X[i] - X[i]*X[i]*X[i]);
}
}
}
return poly;
}
//////////////////////////////// Used Matrix Class From Quant Start /////////////////////////////////////////////////////////////////////////////
// Parameter Constructor
template<typename T>
QSMatrix<T>::QSMatrix(unsigned _rows, unsigned _cols, const T& _initial) {
mat.resize(_rows);
for (unsigned i=0; i<mat.size(); i++) {
mat[i].resize(_cols, _initial);
}
rows = _rows;
cols = _cols;
}
// Copy Constructor
template<typename T>
QSMatrix<T>::QSMatrix(const QSMatrix<T>& rhs) {
mat = rhs.mat;
rows = rhs.get_rows();
cols = rhs.get_cols();
}
// (Virtual) Destructor
template<typename T>
QSMatrix<T>::~QSMatrix() {}
// Assignment Operator
template<typename T>
QSMatrix<T>& QSMatrix<T>::operator=(const QSMatrix<T>& rhs) {
if (&rhs == this)
return *this;
unsigned new_rows = rhs.get_rows();
unsigned new_cols = rhs.get_cols();
mat.resize(new_rows);
for (unsigned i=0; i<mat.size(); i++) {
mat[i].resize(new_cols);
}
for (unsigned i=0; i<new_rows; i++) {
for (unsigned j=0; j<new_cols; j++) {
mat[i][j] = rhs(i, j);
}
}
rows = new_rows;
cols = new_cols;
return *this;
}
// Addition of two matrices
template<typename T>
QSMatrix<T> QSMatrix<T>::operator+(const QSMatrix<T>& rhs) {
QSMatrix result(rows, cols, 0.0);
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
result(i,j) = this->mat[i][j] + rhs(i,j);
}
}
return result;
}
// Cumulative addition of this matrix and another
template<typename T>
QSMatrix<T>& QSMatrix<T>::operator+=(const QSMatrix<T>& rhs) {
unsigned rows = rhs.get_rows();
unsigned cols = rhs.get_cols();
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
this->mat[i][j] += rhs(i,j);
}
}
return *this;
}
// Subtraction of this matrix and another
template<typename T>
QSMatrix<T> QSMatrix<T>::operator-(const QSMatrix<T>& rhs) {
unsigned rows = rhs.get_rows();
unsigned cols = rhs.get_cols();
QSMatrix result(rows, cols, 0.0);
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
result(i,j) = this->mat[i][j] - rhs(i,j);
}
}
return result;
}
// Cumulative subtraction of this matrix and another
template<typename T>
QSMatrix<T>& QSMatrix<T>::operator-=(const QSMatrix<T>& rhs) {
unsigned rows = rhs.get_rows();
unsigned cols = rhs.get_cols();
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
this->mat[i][j] -= rhs(i,j);
}
}
return *this;
}
// Left multiplication of this matrix and another
template<typename T>
QSMatrix<T> QSMatrix<T>::operator*(const QSMatrix<T>& rhs) {
QSMatrix result(rows, rhs.get_cols(), 0.0);
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<rhs.get_cols(); j++) {
for (unsigned k=0; k<cols; k++) {
result(i,j) += this->mat[i][k] * rhs(k,j);
}
}
}
return result;
}
// Cumulative left multiplication of this matrix and another
template<typename T>
QSMatrix<T>& QSMatrix<T>::operator*=(const QSMatrix<T>& rhs) {
QSMatrix result = (*this) * rhs;
(*this) = result;
return *this;
}
// Calculate a transpose of this matrix
template<typename T>
QSMatrix<T> QSMatrix<T>::transpose() {
QSMatrix result(cols, rows, 0.0);
for (unsigned i=0; i<cols; i++) {
for (unsigned j=0; j<rows; j++) {
result(i,j) = this->mat[j][i];
}
}
return result;
}
// Matrix/scalar addition
template<typename T>
QSMatrix<T> QSMatrix<T>::operator+(const T& rhs) {
QSMatrix result(rows, cols, 0.0);
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
result(i,j) = this->mat[i][j] + rhs;
}
}
return result;
}
// Matrix/scalar subtraction
template<typename T>
QSMatrix<T> QSMatrix<T>::operator-(const T& rhs) {
QSMatrix result(rows, cols, 0.0);
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
result(i,j) = this->mat[i][j] - rhs;
}
}
return result;
}
// Matrix/scalar multiplication
template<typename T>
QSMatrix<T> QSMatrix<T>::operator*(const T& rhs) {
QSMatrix result(rows, cols, 0.0);
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
result(i,j) = this->mat[i][j] * rhs;
}
}
return result;
}
// Matrix/scalar division
template<typename T>
QSMatrix<T> QSMatrix<T>::operator/(const T& rhs) {
QSMatrix result(rows, cols, 0.0);
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
result(i,j) = this->mat[i][j] / rhs;
}
}
return result;
}
// Multiply a matrix with a vector
template<typename T>
std::vector<T> QSMatrix<T>::operator*(const std::vector<T>& rhs) {
std::vector<T> result(rhs.size(), 0.0);
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
result[i] = this->mat[i][j] * rhs[j];
}
}
return result;
}
// Obtain a vector of the diagonal elements
template<typename T>
std::vector<T> QSMatrix<T>::diag_vec() {
std::vector<T> result(rows, 0.0);
for (unsigned i=0; i<rows; i++) {
result[i] = this->mat[i][i];
}
return result;
}
// Access the individual elements
template<typename T>
T& QSMatrix<T>::operator()(const unsigned& row, const unsigned& col) {
return this->mat[row][col];
}
// Access the individual elements (const)
template<typename T>
const T& QSMatrix<T>::operator()(const unsigned& row, const unsigned& col) const {
return this->mat[row][col];
}
// Get the number of rows of the matrix
template<typename T>
unsigned QSMatrix<T>::get_rows() const {
return this->rows;
}
// Get the number of columns of the matrix
template<typename T>
unsigned QSMatrix<T>::get_cols() const {
return this->cols;
}
// Print the content of the matrix
template<typename T>
void QSMatrix<T>::print() const {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
std::cout << this->mat[i][j] << " ";
}
std::cout << std::endl;
}
}
什么是正确或最好的方法?
正如 Algirdas Preidžius 所提到的:定义模板时,将所有定义写入头文件中。
相关文章:
- Project Euler问题4的错误解决方案
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 退出分段错误 ¿如何解决此问题?
- OpenCV 错误的解决方案:不支持的格式或格式组合 - 当将 ORB 功能与 FlannBasedMatcher 匹配
- OMNeT++ 错误:无法解决
- OpenVDB链接器错误:未解决的外部符号
- 分段故障(核心倾倒)错误未解决
- c++如何修复std::cin错误或解决它
- C 11:此不完整类型错误的解决方法
- 我的C++代码中有一个棘手的错误;如何解决它
- C++许多错误.如何解决
- 链接器错误:未解决的外部符号
- 未解决的外部符号错误:无法解决
- 在c++中关闭mysql连接时出现分段错误.如何解决
- CURL ilink32错误:未解决的外部
- c++错误:如何解决对象的malloc: ***错误.未分配被释放的指针
- 当链接到/clr DLL时,c++链接器错误(未解决的外部)
- 提升线程错误<未解决的重载函数类型>
- 链接器错误:未解决的外部符号问题
- XE5 Ansi/Unicode字符串链接器错误(未解决的外部)