2014-09-10 19 views
1

我正在試圖找到一組x和y數據的多項式迴歸。我下載了下面的模板來這樣做,但我不知道如何調用它,並且我的嘗試遇到了錯誤LNK2019:無法解析的外部符號。如何調用模板中定義的函數

#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) 
{ 
... 
} 

我不認爲你們大家需要的模板的其餘部分來幫助我,但如果有必要,我將它張貼。 (它來自這個網站http://vilipetek.com/2013/10/07/polynomial-fitting-in-c-using-boost/)如果有一個更好/更容易的工具,讓我知道它。

這是我試圖運行它: 宣言

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

函數調用

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

你爲什麼要重新 - 宣佈它?這具有宣告你沒有實現的專業化的效果,因此沒有解決的符號。 – tadman 2014-09-10 17:03:02

+0

@tadman也許這是錯誤的術語。爲了在另一個方法中使用方法,您是否必須具有上述方法的頭部? – user3241316 2014-09-10 17:07:00

+0

哪個錯誤(完整的描述)? – Amadeus 2014-09-10 17:13:56

回答

2

下面是一個例子:

#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); 

} 
+0

我讓它比必要的複雜得多。這工作。謝謝! – user3241316 2014-09-10 18:33:20

0

你的(明確的)函數調用應該成爲:

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

並且不要用int模板類型重新聲明函數,這可能是導致錯誤的原因。

+0

不需要'',這是一個推論的上下文。 – MSalters 2014-09-10 18:18:45

+0

這就是爲什麼我說**顯式**函數調用。 – 2014-09-11 04:03:09

0

既然你定義模板功能如下:

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

在使用它時,模板參數需要的函數名後出現。在類的情況下,你會怎麼做:

myClass<int> someVariable; 

在功能的情況下,你會怎麼做:

myFunction<int>(); 

所以,你的具體情況:

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

沒必要。函數模板參數通常可以推導出來,這並不是一個例外。類模板參數不能推導出來,這就是爲什麼我們有像'std :: make_pair'這樣的函數來推導出一個'std :: pair'類型。 – MSalters 2014-09-10 18:20:08

相關問題