user3241316
user3241316

Reputation: 167

How to call function defined in template

I'm in the process of trying to find a polynomial regression for a set of x and y data. I downloaded the template below to do so, but I have no idea how to call it, and my attempts are met with error LNK2019: unresolved external symbol.

#pragma once

#ifdef BOOST_UBLAS_TYPE_CHECK
#   undef BOOST_UBLAS_TYPE_CHECK
#endif
#define BOOST_UBLAS_TYPE_CHECK 0
#ifndef _USE_MATH_DEFINES
#   define _USE_MATH_DEFINES
#endif

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <vector>
#include <stdexcept>

/*
Finds the coefficients of a polynomial p(x) of degree n that fits the data, 
p(x(i)) to y(i), in a least squares sense. The result p is a row vector of 
length n+1 containing the polynomial coefficients in incremental powers.

param:
    oX              x axis values
    oY              y axis values
    nDegree         polynomial degree including the constant

return:
    coefficients of a polynomial starting at the constant coefficient and
    ending with the coefficient of power to nDegree. C++0x-compatible 
    compilers make returning locally created vectors very efficient.

*/
template<typename T>
std::vector<T> polyfit( const std::vector<T>& oX, 
const std::vector<T>& oY, int nDegree )
{
...
}

I don't think ya'll need the rest of the template to help me out, but I will post it if necessary. (It's from this site http://vilipetek.com/2013/10/07/polynomial-fitting-in-c-using-boost/) If there's a better/easier tool out there, let me know about it.

This is how I tried to run it: Declaration

std::vector<int> polyfit( const std::vector<int> xsplinevector, 
                   const std::vector<int> ysplinevector, 

function call

polynomial = polyfit((xsplinevector), (ysplinevector), 4);
               int nDegree );

Upvotes: 1

Views: 266

Answers (3)

Paweł Stawarz
Paweł Stawarz

Reputation: 4012

Since you define the templated function as follows:

template<typename T>
std::vector<T> polyfit( const std::vector<T>& oX, 
const std::vector<T>& oY, int nDegree )
{
...
}

When using it, the template argument needs to appear after the function name. In the case of classes, you would do:

myClass<int> someVariable;

In the case of functions you would do:

myFunction<int>();

So, in your particular case:

vector<int> polynomial;
polynomial = polyfit<int>((xsplinevector), (ysplinevector), 4);

Upvotes: 0

B.R.W.
B.R.W.

Reputation: 1566

Your (explicit) function call should be:

polynomial = polyfit<int>((xsplinevector), (ysplinevector), 4);

And don't re-declare the function with the int templated type, that's probably what is causing your error.

Upvotes: 0

Amadeus
Amadeus

Reputation: 10655

Here is an example:

#include <vector>
#include <polyfit.hpp>

int main()
{
    std::vector<int> xsplinevector;
    std::vector<int> ysplinevector;
    std::vector<int> poly;

    // fill both xsplinevector and ysplinevector

    poly = polyfit(xsplinevector, ysplinevector, 4);

}

Upvotes: 2

Related Questions