在类中的if语句中需要澄清

时间:2013-12-28 08:16:48

标签: c++

这是一个openfoam程序,我已对其进行了一些更改。我编译时遇到错误

phaseModel/lnInclude/phaseModel.H:61:2: error: expected unqualified-id before ‘if’
  if (strcmp(word name_, "phase1") == 0)
  ^
phaseModel/lnInclude/phaseModel.H:75:1: error: expected unqualified-id before ‘else’
 else
 ^
phaseModel/lnInclude/phaseModel.H:136:1: error: expected unqualified-id before ‘if’
 if (strcmp(phaseName, "phase1") == 0)
 ^

我的主要需求是如果阶段名称是阶段1,它应该占用某些属性集。对于其他人,它应该采取另一组属性。我是初学者,如果犯了错误,请纠正我。

namespace Foam
{ 
  class phaseModel


  {
    // Private data

        dictionary dict_;

        //- Name of phase
        word name_;


 if (strcmp(word name_, "phase1") == 0)
 {


        //- Velocity
        volVectorField U_;

     dimensionedScalar lambda_;
     dimensionedScalar cp_;
     dimensionedScalar T_;
     dimensionedScalar h_;
     dimensionedScalar beta_;

  }
  else
  {

        //- Characteristic diameter of phase
        dimensionedScalar d_;

        //- kinematic viscosity
        dimensionedScalar nu_;

        //- density
        dimensionedScalar rho_;

        //- Velocity
        volVectorField U_;

     dimensionedScalar lambda_;
     dimensionedScalar cp_;
     dimensionedScalar T_;
     dimensionedScalar h_;
     dimensionedScalar beta_;

        //- Fluxes
        autoPtr<surfaceScalarField> phiPtr_;
  }



  public:

    // Constructors

        phaseModel
        (
            const fvMesh& mesh,
            const dictionary& transportProperties,
            const word& phaseName
        );


    // Selectors

        //- Return a reference to the selected turbulence model
        static autoPtr<phaseModel> New
        (
            const fvMesh& mesh,
            const dictionary& transportProperties,
            const word& phaseName
        );


    //- Destructor
    virtual ~phaseModel();


    // Member Functions

        const word& name() const
        {
            return name_;
        }

  if (strcmp(phaseName, "phase1") == 0)
  {


        const dimensionedScalar& T() const
        {
            return T_;
        }
    const dimensionedScalar& lambda() const 
        {
            return lambda_;
        }
        const dimensionedScalar& h() const 
        {
            return h_;
        }
        const dimensionedScalar& cp() const
        {
            return cp_;
        }
        const dimensionedScalar& beta() const
        {
            return beta_;
        }

        const volVectorField& U() const
        {
            return U_;
        }

        volVectorField& U()
        {
            return U_;
        }

  }
  else
  {
    const dimensionedScalar& d() const
        {
            return d_;
        }

        const dimensionedScalar& T() const
        {
            return T_;
        }
    const dimensionedScalar& lambda() const 
        {
            return lambda_;
        }
        const dimensionedScalar& h() const 
        {
            return h_;
        }
        const dimensionedScalar& cp() const
        {
            return cp_;
        }
        const dimensionedScalar& beta() const
        {
            return beta_;
        }
        const dimensionedScalar& nu() const
        {
            return nu_;
        }

        const dimensionedScalar& rho() const
        {
            return rho_;
        }

        const volVectorField& U() const
        {
            return U_;
        }

        volVectorField& U()
        {
            return U_;
        }

        const surfaceScalarField& phi() const
        {
            return phiPtr_();
        }

        surfaceScalarField& phi()
        {
            return phiPtr_();
        }
  }
}


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

} // End namespace Foam

1 个答案:

答案 0 :(得分:0)

要解决这个问题,您(可能,因为问题中没有100%明确确切的问题)需要声明两个派生自同一个基类的类,并使用虚函数来定义成员函数。然后使用工厂函数确定_name包含的内容并创建具有该功能的正确类。

或者将if (strcmp(...) ... )添加到每个函数中,但这几乎肯定是一个更糟糕的解决方案。您的基类可以包含在两个变体之间不会更改的所有“常用”功能和通用成员。