【问题标题】:How can I pass the value from void function in one cpp file to an other void function in an other cpp file?如何将一个 cpp 文件中的 void 函数的值传递给另一个 cpp 文件中的另一个 void 函数?
【发布时间】:2020-03-21 18:44:31
【问题描述】:

我对编程很陌生,但我必须为我的项目这样做。

在 Visual C++ 6.0 中,我试图将计算值从一个函数发送到另一个 cpp 文件中的另一个函数。 但是,当我尝试编译时,出现以下错误:

创建库 Debug/usradd.lib 和对象 Debug/usradd.exp addrxn.obj:错误 LNK2001:未解析的外部符号“float * Gamma” (?Gamma@@3PAMA) ..\debug\usradd.dll : 致命错误 LNK1120: 1 未解决的外部问题

我该如何解决这个问题?谢谢。

这是我尝试过的简单版本的代码。 我喜欢将 Gamma 矩阵从 addk.cpp 传递到 addrxn.cpp,如下所示。 谢谢。

//callccx.h
//declare "Gamma" array
extern Gamma[23];

//addk.cpp
void addk(float *y, float *x, double t, double p, float *xkv)
{
float Gamma[1] = 2000*t;
.
.
.
float Gamma[23] = 2300*t;

for(int i=0;int<23;i++)
{
xkv[i] = 200*t/p*Gamma[i];
}
return;
}

//addrxn.cpp

int addrxn0(const int nUopID, const int nRxnID, const int nComponents, 
    const double fTemperature,  const double fPressure, const double fRPM, 
    const double fBetaFac, const double fFreqFac, const double fExpActE, 
    const double *C, const double *Pi, const double *fStoich, 
    const double *fExponent, const double *fAdsFac, const double *fAdsE, 
    const double *fAdsExp, double *pRateForm)
{
    //Arhenius
    double rate=fExpActE*fFreqFac*Gamma[1]/Gamma[2];
    int iComp;
    for(iComp=0;iComp<nComponents;iComp++)
    {
        if(fStoich[iComp] < 0)
        {   //This is a reactant.
            if(fExponent[iComp] == 0.0)
                rate*=pow(C[iComp], -fStoich[iComp]);   //Use stoich as exponent
            else
                rate*=pow(C[iComp], fExponent[iComp]);  //Use exponent as given
        }
    }

    //Final rate of formation
    (*pRateForm)=rate*
    return 0;
}

【问题讨论】:

    标签: c++ c visual-c++ void extern


    【解决方案1】:

    extern 声明需要一个类型:

    extern float Gamma[23];
    

    您的示例代码:

    float Gamma[1] = 2000*t;
    

    没有意义。你是在这里声明一个 ONE 元素的浮点数组吗?您可能的意思是:

    Gamma[1] = 2000 * t;
    

    无论如何,您的链接错误告诉您Gamma 需要在某个地方定义,可能在另一个文件中。像

    float Gamma[23];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多