未定义的引用 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6284720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:51:04  来源:igfitidea点击:

Undefined reference C++

c++undefined-reference

提问by Ryan Amos

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:

我正在尝试编译我从 Java 转换而来的第一个合法程序(我运行了一个测试 hello world 类型程序来检查我的编译器并且它工作正常)。共有三个文件:

main.cpp

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

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

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.

我在 Ubuntu 10.10 上使用 Code::Blocks。

回答by Mark Ransom

You may be compiling skewNormal.cpp to a .o file, but you're not including it when you compile main.cpp.

您可能正在将 skewNormal.cpp 编译为 .o 文件,但在编译 main.cpp 时并未包括它。

回答by ssube

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).

double getSkewNormal(double skewValue, double x);行只需要在标题中,而不是在 main.cpp 中(因为 main.cpp 包含 skewNormal.h)。让它在原型形式中出现两次似乎可能会混淆编译器。您只需要它对代码可见一次,通常应该在标题中(而不是代码文件)。

Try removing that line from main.cpp and recompile. :)

尝试从 main.cpp 中删除该行并重新编译。:)