如何使用其参数化构造函数初始化成员

时间:2022-04-01 11:38:40

It's hard to describe without code so here goes: I'm trying to prototype an object (b) in the header file of another(a), then in the constructor of (a) call (b)'s constructor and pass it values, so i can then use the methods of b which depend on its constructor and the values passed to it, but the way im doing gives: red underlined in the open bracket of pricing's constructor says: "no default constructor exists for monteCarlo" and then on the next line m is underlined red saying: "call of object of a class type without appropriate operator() or conversion functionsto pointer-to-function type". Any other critic of my program is very welcome, i am trying to learn to program, and well.

没有代码很难描述,所以这里是:我试图在另一个(a)的头文件中原型化一个对象(b),然后在(a)调用(b)的构造函数的构造函数中对其进行原型化并传递它的值,那么我可以使用依赖于它的构造函数和传递给它的值的b的方法,但是我做的方式给出了:在定价的构造函数的开括号中用红色下划线说:“monteCarlo没有默认构造函数”然后在下一行m下划线红色说:“没有适当的操作符(或转换函数指向函数类型)的类类型的对象调用”。任何其他批评我的程序是非常受欢迎的,我正在努力学习编程,并且很好。

in the file pricing.cpp i have:

在文件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;
}

in pricing.h i have:

在pricing.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;

};

then montecarlo.cpp looks like:

然后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)
{
}

and finally montecarlo.h is:

最后montecarlo.h是:

#pragma once
#include <vector>

class monteCarlo
{
public:
monteCarlo(int, double, double, int);
~monteCarlo(void);
std::vector< std::vector<double> > samplePaths;
};

2 个解决方案

#1


3  

pricing::pricing(void)
{
 m(10,0.0,0.01,50);
}

This attempts to call m as though it were a function (if it had overloaded operator(), you would be able to do this, which is what the error is talking about). To initialise m instead, use the member initialization list:

这会尝试调用m,好像它是一个函数(如果它有重载的operator(),你就可以这样做,这就是错误所说的)。要初始化m,请使用成员初始化列表:

pricing::pricing(void)
 : m(10,0.0,0.01,50)
{ }

This colon syntax is used to initialise members of an object in the constructor. You simply list members by their names and initialize them with either ( expression-list ) or { initializer-list } syntax.

此冒号语法用于初始化构造函数中对象的成员。您只需按名称列出成员,并使用(expression-list)或{initializer-list}语法对其进行初始化。

#2


0  

pricing.cpp

pricing.cpp

#include "pricing.h"

pricing::pricing() 
  : m(10,0.0,0.01,50) 
{
}

double pricing::expectedValue()
{
    return m.samplePaths[2][3]; 
}

pricing.h

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 looks like:

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;
    }
}

and finally montecarlo.h is:

最后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

I would use some very basic rules:

我会使用一些非常基本的规则:

  1. use include guards
  2. 使用包括警卫
  3. Include only headers that are really needed in headers
  4. 仅包含标头中真正需要的标头
  5. Include the header as the first file in the implementation.
  6. 将标头包含为实现中的第一个文件。
  7. Do not use "using namespace" in a header
  8. 不要在标头中使用“using namespace”
  9. If possible, use forward declarations instead of includes
  10. 如果可能,使用前向声明而不是包含

3) makes sure, that the header contains all necessary include files

3)确保标题包含所有必需的包含文件

#1


3  

pricing::pricing(void)
{
 m(10,0.0,0.01,50);
}

This attempts to call m as though it were a function (if it had overloaded operator(), you would be able to do this, which is what the error is talking about). To initialise m instead, use the member initialization list:

这会尝试调用m,好像它是一个函数(如果它有重载的operator(),你就可以这样做,这就是错误所说的)。要初始化m,请使用成员初始化列表:

pricing::pricing(void)
 : m(10,0.0,0.01,50)
{ }

This colon syntax is used to initialise members of an object in the constructor. You simply list members by their names and initialize them with either ( expression-list ) or { initializer-list } syntax.

此冒号语法用于初始化构造函数中对象的成员。您只需按名称列出成员,并使用(expression-list)或{initializer-list}语法对其进行初始化。

#2


0  

pricing.cpp

pricing.cpp

#include "pricing.h"

pricing::pricing() 
  : m(10,0.0,0.01,50) 
{
}

double pricing::expectedValue()
{
    return m.samplePaths[2][3]; 
}

pricing.h

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 looks like:

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;
    }
}

and finally montecarlo.h is:

最后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

I would use some very basic rules:

我会使用一些非常基本的规则:

  1. use include guards
  2. 使用包括警卫
  3. Include only headers that are really needed in headers
  4. 仅包含标头中真正需要的标头
  5. Include the header as the first file in the implementation.
  6. 将标头包含为实现中的第一个文件。
  7. Do not use "using namespace" in a header
  8. 不要在标头中使用“using namespace”
  9. If possible, use forward declarations instead of includes
  10. 如果可能,使用前向声明而不是包含

3) makes sure, that the header contains all necessary include files

3)确保标题包含所有必需的包含文件