写了个示例程序,编译报错如下
g++ main.cpp test.cpp -o main.out <
Undefined symbols for architecture x86_64:
"Test::Test()", referenced from:
_main in main-2eb949.o
"Test::~Test()", referenced from:
_main in main-2eb949.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
//test.h
#ifndef __TEST__
#define __TEST__
class Test{
public:
Test() ;
~Test();
int calc(int a, int b);
};
#endif
//test.cpp
#include "test.h"
//只有定义没有实现,结果出现了报错
// Test::Test()
// {
// }
// Test::~Test()
// {
// }
int Test::calc(int a, int b)
{
return (a+b);
}
//main.cpp
#include <stdio.h>
#include "test.h"
int main(int argc, char const *argv[])
{
Test t;
printf("a+b=[%d]", t.calc(1,2));
return 0;
}