【发布时间】:2015-07-13 09:00:58
【问题描述】:
考虑下面的代码:
//header.h
#pragma once
class A
{
public:
A();
void f();
};
//header.cpp
#include "header.h"
A::A(){}
void A::f(){}
//main.cpp
#include "header.h"
int main()
{
A a;
a.f();
}
那么编译器如何知道constructor 和f 函数的声明究竟在哪里,因为只有header.h 包含在main.cpp 中?又为什么class A为模板时找不到相同的功能??
【问题讨论】:
-
函数,包括构造函数,可以用符号来引用。链接器查找所有符号并将它们替换为实际的代码地址。
-
关于您的最后一个问题,请参阅Why can templates only be implemented in the header file
标签: c++ templates compilation linker