C++ C++;函子编译错误

C++ C++;函子编译错误,c++,templates,functor,C++,Templates,Functor,我有以下主程序,编译时会出现此错误: 双平方器(double,SineFunctor&)的未定义符号 为什么链接器找不到函数平方器 include <iostream> #include "SineFunctor.h" #include "squarer.h" int main() { const double PI = 3.141592L; double x = 1.0; double y; // initialize function param

我有以下主程序,编译时会出现此错误: 双平方器(double,SineFunctor&)的未定义符号 为什么链接器找不到函数平方器

include <iostream>
#include "SineFunctor.h"
#include "squarer.h"
int main()
{
    const double PI = 3.141592L;
    double x = 1.0;
    double y;
    //  initialize function parameters
    SineFunctor sine(2., 10.*PI, 0.);    
    // pass functions sine to function squarer
    y = squarer(x, sine);
    return 0;
}
包括
#包括“SineFunctor.h”
#包括“平方h”
int main()
{
常数双PI=3.141592L;
双x=1.0;
双y;
//初始化函数参数
正弦函子正弦(2,10.*PI,0.);
//传递函数正弦到函数平方器
y=平方器(x,正弦);
返回0;
}
这是SineFunctor的文件

//  SineFunctor.h
#ifndef __functor_example__SineFunctor__
#define __functor_example__SineFunctor__

#include <iostream>
class SineFunctor {    
public:
//  constructor to set function parameters
SineFunctor(const double amp, const double freq, const double phase);

//  function evaluation using overloaded operator()
double operator() (const double t);
private:
    double _amp;
    double _freq;
    double _phase;
};
#endif /* defined(__functor_example__SineFunctor__) */

//  SineFunctor.cpp
#include <cmath>
#include "SineFunctor.h"

//  constructor to set function parameters
SineFunctor::SineFunctor(const double amp, const double freq, const double phase) {
    _amp = amp;
    _freq = freq;
    _phase = phase;
}
//  function evaluation using overloaded operator()
double SineFunctor::operator() (const double t) {
    return _amp * sin(_freq * t + _phase);
}
//SineFunctor.h
#ifndef uu函子u示例uu函子__
#定义\uuuu函子\u示例\uuuu函子__
#包括
类SineFunctor{
公众:
//构造函数来设置函数参数
正弦函子(恒双放大器、恒双频、恒双相位);
//使用重载运算符()进行函数求值
双运算符()(常数双t);
私人:
双放大器;
双频;
双相;
};
#endif/*已定义(uuu函子_uu示例_uuusinefunctor_uuu)*/
//SineFunctor.cpp
#包括
#包括“SineFunctor.h”
//构造函数来设置函数参数
正弦函子::正弦函子(恒双放大器、恒双频、恒双相位){
_amp=amp;
_频率=频率;
_相位=相位;
}
//使用重载运算符()进行函数求值
双SineFunctor::operator()(常数双t){
返回正弦波(_-freq*t+_-phase);
}
和平方器>

//  squarer.h
#ifndef __functor_example__squarer__
#define __functor_example__squarer__

#include <iostream>
template <class F>
double squarer(const double x, F& func);

#endif /* defined(__functor_example__squarer__) */

//  squarer.cpp
#include "squarer.h"

template <class F>
double squarer(const double x, F& func) {
    double y = func(x);
    return y * y;
}
//square.h
#ifndef uu函子u例子uu平方子__
#定义_函子_示例_平方器__
#包括
模板
双平方器(常数双x,F&func);
#endif/*已定义(uuu函子_uu示例_uuu平方器_uuuu)*/
//平方器
#包括“平方h”
模板
双平方器(常数双x,F&func){
双y=func(x);
返回y*y;
}

必须在头文件中给出模板定义,因为编译器需要确切地知道它应该实例化什么。将
squarer
的定义移动到头文件中:

// squarer.h
#ifndef functor_example__squarer
#define functor_example__squarer

template <class F>
double squarer(const double x, F& func) {
    double y = func(x);
    return y * y;
}

#endif
//square.h
#ifndef函子
#定义函子
模板
双平方器(常数双x,F&func){
双y=func(x);
返回y*y;
}
#恩迪夫

请注意,我已从您的
#define
中删除了双下划线。带有双下划线的名称保留供实现使用。

必须在头文件中给出模板定义,因为编译器需要确切地知道它应该实例化什么。将
squarer
的定义移动到头文件中:

// squarer.h
#ifndef functor_example__squarer
#define functor_example__squarer

template <class F>
double squarer(const double x, F& func) {
    double y = func(x);
    return y * y;
}

#endif
//square.h
#ifndef函子
#定义函子
模板
双平方器(常数双x,F&func){
双y=func(x);
返回y*y;
}
#恩迪夫

请注意,我已从您的
#define
中删除了双下划线。带有双下划线的名称保留供实现使用。

模板函数需要位于页眉中,或者为cpp中的所有类型实例化。

模板函数需要位于页眉中,或者为cpp中的所有类型实例化。

可能重复的模板函数,或者为定义它们的编译单元中的所有类型。或为定义它们的编译单元中的所有类型实例化。