尝试输入向量时未定义对序列的引用

Undefined reference to sequence when trying to input a vector

这看起来很简单,但不知何故,编译发送了这个我无法理解的错误消息,因此更正了我的代码。

这是我所做的简化版本,只是为了让您看到错误:

Main.cpp

include "myfunction.h"
int main(){
    std::vector<int> myVet = {1,4,3};
    sequence(1,2,1,myVet);
}

myfunction.h

#include <vector>
/*funtion creates a sequence*/
void sequence(int start, int end, 
              int step, std::vector<int> skip);

myfunction.cpp

#include "myfunction.h"

void sequence(int start, int end, 
              int step, std::vector<int> skip){
     auto x = 0;
};

这给了我一条错误消息,上面写着

In function 'main':
/home/machina/Documents/Grafos&Redes/Implementação/main.cpp:18: undefined reference to 'sequence(int, int, int, std::vector <int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status

你能解释一下为什么会出现吗?

这是我一直用于编译的以下命令

g++ -std=c++11 -g  -Wall -Wextra -Werror main.cpp -o main.out

您只是将 main.cpp 传递给 g++。

g++ 需要知道 myfunction.cpp 定义函数的位置,以便将其编译并 link 到您的程序中。

要使用的命令应该是:

g++ -std=c++11 -g -Wall -Wextra -Werror main.cpp myfunction.cpp -o main.out