C++:包括头文件失败编译但包括源cpp文件编译

Cha*_*hie 0 c++ gcc header-files

这可能非常简单,但它在我走下c ++路的过程中阻碍了我.我目前正在通过加速c ++阅读,我决定过度使用其中一个练习.这一切都运行良好,我的代码运行良好,直到我将它分成一个标题和单独的源文件.当我导入包含我写的一些函数的.cpp源文件时,一切运行正常.但是,当我尝试通过头文件导入函数时,它失败可怕,我得到以下错误.我正在使用Geany的gcc进行编译,直到现在它都运行良好.谢谢你的帮助.

错误:

g++ -Wall -o "quartile" "quartile.cpp" (in directory: /home/charles/Temp)
Compilation failed.
/tmp/ccJrQoI9.o: In function `main':
quartile.cpp:(.text+0xfd): undefined reference to `quartile(std::vector<double, std::allocator<double> >)'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

"stats.h":

#ifndef GUARD_stats_h
#define GUARD_stats_h

#include <vector>

std::vector<double> quartile(std::vector<double>);

#endif
Run Code Online (Sandbox Code Playgroud)

"stats.cpp":

#include <vector>
#include <algorithm>
#include "stats.h"

using std::vector;    using std::sort;

double median(vector<double> vec){
     //code...
}

vector<double> quartile(vector<double> vec){
     //code and I also reference median from here.
}
Run Code Online (Sandbox Code Playgroud)

"quartile.cpp":

#include <iostream>
#include <vector>
#include "stats.h" //if I change this to "stats.cpp" it works

using std::cin;       using std::cout;
using std::vector;

int main(){
    //code and reference to quartile function in here.
}
Run Code Online (Sandbox Code Playgroud)

Cat*_*lus 7

编译失败,因为您只声明了此函数.它的定义是在不同的编译单元中,你没有将这两者连接在一起.

g++ -Wall -o quartile quartile.cpp stats.cpp它会起作用.