没有代码很难描述,所以这里是: 我试图在另一个(a)的头文件中原型化一个对象(b),然后在(a)调用(b)的构造函数的构造函数中并传递它的值,所以我可以使用b的方法这依赖于它的构造函数和传递给它的值,但是我做的方式给出了:在定价的构造函数的开放括号中用红色加下划线说:“monteCarlo没有默认构造函数”,然后在下一行m用红色加下划线说: “没有适当的operator()或转换函数指向类函数类型的类的对象的调用”。对我的节目的任何其他批评者都非常欢迎,我正在努力学习编程,并且很好。
在文件pricing.cpp中我有:
#include "pricing.h"
#include <math.h>
#include <vector>
pricing::pricing(void)
{
m(10,0.0,0.01,50);
}
double pricing::expectedValue(void)
{
expectedExValue = m.samplePaths[2][3]; //yes this isn't an expected value,
// its just for illustration purposes/making it compile.
return 0;
}
在Price.h中,我有:
#pragma once
#include "pricing.h"
#include "monteCarlo.h"
class pricing
{
public:
pricing(void);
~pricing(void);
double euroCall();
std::vector<double> samplePathing;
double expectedValue();
monteCarlo m;
};
然后montecarlo.cpp看起来像:
#include "monteCarlo.h"
#include "randomWalk.h"
#include <vector>
#include <iostream>
monteCarlo::monteCarlo(int trails, double drift, double volidatity, int density)
{
for (int i = 0; i < trails; i++)
{
std::cout << "Trail number " << i+1 << std::endl;
randomWalk r(drift,volidatity,density);
r.seed();
samplePaths.emplace_back(r.samplePath);
std::cout << std::endl << std::endl;
}
}
monteCarlo::~monteCarlo(void)
{
}
最后montecarlo.h是:
#pragma once
#include <vector>
class monteCarlo
{
public:
monteCarlo(int, double, double, int);
~monteCarlo(void);
std::vector< std::vector<double> > samplePaths;
};
答案 0 :(得分:3)
pricing::pricing(void)
{
m(10,0.0,0.01,50);
}
这会尝试调用 m
,就像它是一个函数一样(如果它已经重载operator()
,你就能做到这一点,这就是错误所在谈论)。要初始化m
,请使用成员初始化列表:
pricing::pricing(void)
: m(10,0.0,0.01,50)
{ }
此冒号语法用于初始化构造函数中对象的成员。您只需按名称列出成员,并使用( expression-list )
或{ initializer-list }
语法对其进行初始化。
答案 1 :(得分:0)
pricing.cpp
#include "pricing.h"
pricing::pricing()
: m(10,0.0,0.01,50)
{
}
double pricing::expectedValue()
{
return m.samplePaths[2][3];
}
pricing.h
#ifndef PRICING_H
#define PRICING_H
#include "monteCarlo.h"
#include <vector>
class pricing
{
public:
pricing();
double euroCall();
std::vector<double> samplePathing;
double expectedValue();
private:
monteCarlo m;
};
#endif
montecarlo.cpp看起来像:
#include "monteCarlo.h"
#include "randomWalk.h"
#include <iostream>
monteCarlo::monteCarlo(int trails, double drift, double volidatity, int density)
{
for (int i = 0; i < trails; i++)
{
std::cout << "Trail number " << i+1 << std::endl;
randomWalk r(drift,volidatity,density);
r.seed();
samplePaths.emplace_back(r.samplePath);
std::cout << "\n" << std::endl;
}
}
最后montecarlo.h是:
#ifndef MONTECARLO_H
#define MONTECARLO_H
#include <vector>
class monteCarlo
{
public:
monteCarlo(int, double, double, int);
std::vector< std::vector<double> > samplePaths;
};
#endif
我会使用一些非常基本的规则:
3)确保标题包含所有必要的包含文件