未定义的参考 C++
我正在尝试编译我从 Java 转换的第一个合法程序(我运行了一个测试 hello world 类型程序来检查我的编译器并且它有效)。 共有三个文件:
main.cpp
#include <iostream>
#include "skewNormal.h"
using namespace std;
double getSkewNormal(double, double);
int main(int argc, char **argv) {
cout << getSkewNormal(10.0, 0.5) << endl;
}
skewNormal.cpp
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
using namespace std;
#include <skewNormal.h>
double SkewNormalEvalutatable::evaluate(double x)
{
return 1 / sqrt(2 * M_PI) * pow(M_E, -x * x / 2);
}
SkewNormalEvalutatable::SkewNormalEvalutatable()
{
}
double sum (double start, double stop,
double stepSize,
Evaluatable evalObj)
{
double sum = 0.0, current = start;
while (current <= stop) {
sum += evalObj.evaluate(current);
current += stepSize;
}
return(sum);
}
double integrate (double start, double stop,
int numSteps,
Evaluatable evalObj)
{
double stepSize = (stop - start) / (double)numSteps;
start = start + stepSize / 2.0;
return (stepSize * sum(start, stop, stepSize, evalObj));
}
double getSkewNormal(double skewValue, double x)
{
SkewNormalEvalutatable e;
return 2 / sqrt(2 * M_PI) * pow(M_E, -x * x / 2) * integrate(-1000, skewValue * x, 10000, e);
}
skewNormal.h
#ifndef SKEWNORMAL_H_INCLUDED
#define SKEWNORMAL_H_INCLUDED
class Evaluatable {
public:
virtual double evaluate(double x);
};
class SkewNormalEvalutatable : Evaluatable{
public:
SkewNormalEvalutatable();
double evaluate(double x);
};
double getSkewNormal(double skewValue, double x);
double integrate (double start, double stop, int numSteps, Evaluatable evalObj);
double sum (double start, double stop, double stepSize, Evaluatable evalObj);
#endif // SKEWNORMAL_H_INCLUDED
编译产生以下错误:
main.cpp:9: undefined reference to `getSkewNormal(double, double)'
我正在使用 Code::Blocks乌班图10.10。
I'm trying to compile my first legit program that I'm converting from Java (I ran a test hello world type program to check my compiler and it works).
There are three files:
main.cpp
#include <iostream>
#include "skewNormal.h"
using namespace std;
double getSkewNormal(double, double);
int main(int argc, char **argv) {
cout << getSkewNormal(10.0, 0.5) << endl;
}
skewNormal.cpp
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
using namespace std;
#include <skewNormal.h>
double SkewNormalEvalutatable::evaluate(double x)
{
return 1 / sqrt(2 * M_PI) * pow(M_E, -x * x / 2);
}
SkewNormalEvalutatable::SkewNormalEvalutatable()
{
}
double sum (double start, double stop,
double stepSize,
Evaluatable evalObj)
{
double sum = 0.0, current = start;
while (current <= stop) {
sum += evalObj.evaluate(current);
current += stepSize;
}
return(sum);
}
double integrate (double start, double stop,
int numSteps,
Evaluatable evalObj)
{
double stepSize = (stop - start) / (double)numSteps;
start = start + stepSize / 2.0;
return (stepSize * sum(start, stop, stepSize, evalObj));
}
double getSkewNormal(double skewValue, double x)
{
SkewNormalEvalutatable e;
return 2 / sqrt(2 * M_PI) * pow(M_E, -x * x / 2) * integrate(-1000, skewValue * x, 10000, e);
}
skewNormal.h
#ifndef SKEWNORMAL_H_INCLUDED
#define SKEWNORMAL_H_INCLUDED
class Evaluatable {
public:
virtual double evaluate(double x);
};
class SkewNormalEvalutatable : Evaluatable{
public:
SkewNormalEvalutatable();
double evaluate(double x);
};
double getSkewNormal(double skewValue, double x);
double integrate (double start, double stop, int numSteps, Evaluatable evalObj);
double sum (double start, double stop, double stepSize, Evaluatable evalObj);
#endif // SKEWNORMAL_H_INCLUDED
Compiling yielded the following error:
main.cpp:9: undefined reference to `getSkewNormal(double, double)'
I'm using Code::Blocks on Ubuntu 10.10.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能将 skewNormal.cpp 编译为 .o 文件,但在编译 main.cpp 时并未包含它。
You may be compiling skewNormal.cpp to a .o file, but you're not including it when you compile main.cpp.
不确定这是否是问题所在,但看来您正在对该函数进行两次原型设计。
double getSkewNormal(double skewValue, double x);
行只需位于标头中,而不是位于 main.cpp 中(因为 main.cpp 包含 skewNormal.h)。让它以原型形式出现两次似乎可能会让编译器感到困惑。您只需要它对代码可见一次,通常应该位于标头(而不是代码文件)中。尝试从 main.cpp 中删除该行并重新编译。 :)
Not sure if this is the problem, but it appears you're prototyping the function twice.
The
double getSkewNormal(double skewValue, double x);
line need only be in the header, not in main.cpp as well (since main.cpp includes skewNormal.h). Having it appear twice in a prototype form seems likely to confuse the compiler. You only need it to be visible to the code once, and usually that should be in a header (not a code file).Try removing that line from main.cpp and recompile. :)