0

你好stackoverflow的人,

我是 C++ 的新手。我试图创建一个类,但是当我编译我的程序时,我当然会收到错误消息。我在这里希望你们中的一些人能告诉我我的程序有什么问题以及我做错了什么!

PS:我使用的是 OpenFOAM,所以有些不同,例如“.C”扩展名(而不是“.cpp”)和 Foam 库的使用。

船只.H

#ifndef Vessels_H
#define Vessels_H

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

class Vessels
{
public:

Vessels(double xCoordinate, double yCoordinate, double zCoordinate, double vesselLength, double vesselRadius, double P_outside, double P_inside);
int pointIndex();
double flowRate();
double wellModel();

private:

double m_xCoordinate, m_yCoordinate, m_zCoordinate, m_vesselLength, m_vesselRadius, m_P_outside, m_P_inside;
double flow, meshVolume, meshSize, equivalentRadius;
int index;

};

#include "Vessels.C"
#endif

船只.C

#include "Vessels.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

// CONSTRUCTOR

Vessels::Vessels(double xCoordinate, double yCoordinate, double zCoordinate, double vesselLength, double vesselRadius, double P_outside, double P_inside) : m_xCoordinate(xCoordinate), m_yCoordinate(yCoordinate), m_zCoordinate(zCoordinate), m_vesselLength(vesselLength), m_vesselRadius(vesselRadius), m_P_outside(P_outside), m_P_inside(P_inside)
{

}

// METHODS 

// Corresponding index for point (xCoordinate,yCoordinate,zCoordinate)

int Vessels::pointIndex() 
{
    return mesh.findCell(point(m_xCoordinate,m_yCoordinate,m_zCoordinate));
}

// Calculation of flow rate in the vessel using the Hagen-Poiseuille equation

double Vessels::flowRate()
{
    return M_PI*Foam::pow(m_vesselRadius,4.0)/(8.0*mu.value*m_vesselLength)*(m_P_inside-m_P_outside);
}

// Numerical well model

double Vessels::wellModel()
{
    Vessels vessel(double xCoordinate, double yCoordinate, double zCoordinate, double vesselLength, double vesselRadius, double P_outside, double P_inside);
    index = vessel.pointIndex();
    flow = vessel.flowRate();
    meshVolume = mesh.V()[index];
    meshSize = Foam::pow(meshVolume,1.0/3);
    equivalentRadius = 3.0*meshSize/(3.0+2.0*M_PI*meshSize);

    m_P_inside = P[index] + mu.value*flow/(4.0*M_PI*K)*(1.0/equivalentRadius - 1.0/m_vesselRadius);
    return m_P_inside;
}

如果任何人都对它感兴趣,那么到目前为止,这是我的编译器错误:

Vessels.C: In member function ‘double Vessels::flowRate()’:
Vessels.C:65:52: erreur: invalid operands of types ‘double’ and ‘<unresolved overloaded function type>’ to binary ‘operator*’
Vessels.C: In member function ‘double Vessels::wellModel()’:
Vessels.C:73:18: erreur: request for member ‘pointIndex’ in ‘vessel’, which is of non-class type ‘Vessels(double, double, double, double, double, double, double)’
Vessels.C:74:17: erreur: request for member ‘flowRate’ in ‘vessel’, which is of non-class type ‘Vessels(double, double, double, double, double, double, double)’
Vessels.C:79:35: erreur: invalid operands of types ‘<unresolved overloaded function type>’ and ‘double’ to binary ‘operator*’
Vessels.C: In member function ‘double Vessels::flowRate()’:
Vessels.C:66:1: attention : contrôle a atteint la fin non void de la fonction [-Wreturn-type]

谢谢你!为了更好地理解如何使用 C++,我愿意接受所有可能的建议!

4

3 回答 3

1

您的第一个错误在该行中:

return M_PI*Foam::pow(m_vesselRadius,4.0)/(8.0*mu.value*m_vesselLength)*(m_P_inside-m_P_outside);

这条线太长太复杂了。

将此行拆分为四五行简单易读的行。

创建中间变量并赋予它们有意义的名称。

创建一个变量double result = ...来存储最终结果。

pow在每个参数处使用一个变量调用,而不是计算。

通过这样做,您最多可以更好地隔离您的编译错误,最多甚至可以修复一些错误。

于 2013-06-05T10:21:38.117 回答
1
Vessels vessel(double xCoordinate, double yCoordinate, double zCoordinate, double vesselLength, double vesselRadius, double P_outside, double P_inside);

这是函数的声明,而不是对象的声明。看起来您想创建对象的副本,但我认为没有理由。只需将其删除。

double Vessels::wellModel()
{
    index = pointIndex();
    flow = flowRate();
    meshVolume = mesh.V()[index];
    meshSize = Foam::pow(meshVolume,1.0/3);
    equivalentRadius = 3.0*meshSize/(3.0+2.0*M_PI*meshSize);

    m_P_inside = P[index] + mu.value*flow/(4.0*M_PI*K)*(1.0/equivalentRadius - 1.0/m_vesselRadius);
    return m_P_inside;
}

value 是一个提取尺寸标量值的函数。

在这种情况下,您会在此处忘记括号:

return M_PI*Foam::pow(m_vesselRadius,4.0)/(8.0*mu.value()*m_vesselLength)*(m_P_inside-m_P_outside);
//                                                     ^^
于 2013-06-05T10:20:57.103 回答
0

我认为您正在制作一个功能,而不是一个对象

容器容器(双 xCoordinate、双 yCoordinate、双 zCoordinate、双容器长度、双容器半径、双 P_outside、双 P_inside);

于 2013-06-05T10:27:07.497 回答