C++ functor compile error

I have the following main program, which when compiled gives this error: Undefined symbols for double squarer(double, SineFunctor&) Why can't the linker find the function squarer?

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;
}

Here are the files for 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);
}

and squarer>

//  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;
}

Answers


Template functions either need to be in the header or be instantiated for all types in the cpp.


Template definitions must be given in the header file because the compiler needs to know exactly what it should be instantiating. Move the definition of squarer into the header file:

// 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

Notice that I've removed the double underscores from your #define. Names with double underscores are reserved for use by the implementation.


Need Your Help

How to find the comment tag <!--…--> with BeautifulSoup?

python html tags beautifulsoup

I tried soup.find('!--') but it doesn't seem to work. Thanks in advance.

jQuery getting tag's content dynamically

javascript jquery angularjs

First of all, I know my question seems to be already asked many many times but I'm facing a weird issue.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.