我正在尝试学习有关在C ++中创建头文件的基础知识,但是当我尝试编写自己的头文件时却遇到编译器错误。我在macOS上使用clang ++编译器。
我尝试编译头文件,但这并不能解决问题。
Num.cpp
#include "Num.h"
Num::Num() : num(0) { }
Num::Num(int n): num(n) {}
int Num::getNum()
{
return num;
}
Num.h
class Num
{
private:
int num;
public:
Num();
Num(int n);
int getNum();
};
main.cpp
#include<iostream>
#include<vector>
#include<map>
#include"Num.h"
using namespace std;
int main(){
Num n = Num(3);
cout << n.getNum();
}
当我尝试编译main时,出现此错误,
Undefined symbols for architecture x86_64:
"Num::getNum()", referenced from:
_main in hello-4c818f.o
"Num::Num(int)", referenced from:
_main in hello-4c818f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)