简体   繁体   English

如何将一个 cpp 文件中的 void 函数的值传递给另一个 cpp 文件中的另一个 void 函数?

[英]How can I pass the value from void function in one cpp file to an other void function in an other cpp file?

I am very new to programming but i have to do this for my project.我对编程很陌生,但我必须为我的项目这样做。

In Visual C++ 6.0, I am trying to send the calculated value from one function to the other in a different cpp file.在 Visual C++ 6.0 中,我试图将计算值从一个函数发送到另一个 cpp 文件中的另一个函数。 however, when I try to compile I get the following error :但是,当我尝试编译时,出现以下错误:

Creating library Debug/usradd.lib and object Debug/usradd.exp addrxn.obj : error LNK2001: unresolved external symbol "float * Gamma" (?Gamma@@3PAMA) ..\\debug\\usradd.dll : fatal error LNK1120: 1 unresolved externals创建库 Debug/usradd.lib 和对象 Debug/usradd.exp addrxn.obj : error LNK2001: unresolved external symbol "float * Gamma" (?Gamma@@3PAMA) ..\\debug\\usradd.dll:fatal error LNK1120: 1未解决的外部

How can I fix this problem?我该如何解决这个问题? Thank you.谢谢你。

This is simple version of code that I tried.这是我尝试过的简单代码版本。 I like to pass Gamma matrix from addk.cpp to addrxn.cpp as shown below.我喜欢将 Gamma 矩阵从 addk.cpp 传递到 addrxn.cpp,如下所示。 Thank you.谢谢你。

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

The extern declaration requires a type: extern声明需要一个类型:

extern float Gamma[23];

Your sample code:您的示例代码:

float Gamma[1] = 2000*t;

makes no sense.没有意义。 Are you declaring a float array of ONE element here?您是否在这里声明了一个包含 ONE 元素的浮点数组? You probably meant:你可能的意思是:

Gamma[1] = 2000 * t;

Anyway, your link error tells you that Gamma needs to be defined somewhere, probably in another file.无论如何,您的链接错误告诉您Gamma需要在某处定义,可能在另一个文件中。 Something like就像是

float Gamma[23];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM