首页 > 解决方案 > C++模板参数函数

问题描述

这里是 C++ 初学者!:-) 我试图通过模仿 STL 算法中的函数来编写函数。

template<class InputIterator>
InputIterator findNegativeMax(InputIterator, InputIterator);

以上是some.hpp文件中的声明。下面是函数的定义findNegativeMax()

template<class InputIterator>
InputIterator findNegativeMax(InputIterator first, InputIterator last){
    Iterator position = nullptr;
    while(first <= last){
         /*some logic*/
    }
    return position;
}

如前所述,我试图模仿诸如 max_element implementation之类的 STL 代码。但是当我编译看起来像的测试代码时

int main(){
std::vector<int> vec = {1,2,3,44,4,-5,-5,-2};
auto it = algo::findNegativeMax(vec.begin(),vec.end());
std::cout<<"position::"<<*it<<std::endl;
return 0;

}

.但是当使用所有必要的对象和属性进行编译时,我收到以下错误

/tmp/ccma7n9Q.o:在函数main': testAlgo.cpp:(.text+0x87): undefined reference to__gnu_cxx::__normal_iterator >> algo::findNegativeMax<__gnu_cxx::__normal_iterator >>>(__gnu_cxx::__normal_iterator >>, __gnu_cxx::__normal_iterator >>)' collect2:错误:ld 返回 1退出状态

如果问题无论如何是错误的,请原谅。

先感谢您。

标签: c++c++11templateslinker-errorsundefined-reference

解决方案


推荐阅读