Home Templated class with templated static member function no match for prototype in implementation
Reply: 1

Templated class with templated static member function no match for prototype in implementation

Vivick
1#
Vivick Published in 2018-02-11 10:13:05Z

In the making of a library I encountered the following problem :

Let's have four files :

  • main.cpp
  • nsp/nsp.hpp
  • nsp/A.h
  • nsp/A.cpp

Below is the content of each file

main.cpp

#include <iostream>
#include <vector>
#include "nsp/nsp.hpp"

using namespace std;

int main(){
  vector<int> v = {0, 1};
  nsp::A<int> a = nsp::A<int>::from(v.begin(), v.end());
  cout << a.getElem();
  return 0;
}

nsp/nsp.hpp

#ifndef NSP_HPP
#define NSP_HPP

namespace nsp{};

#include "A.h"

#endif

nsp/A.h

#ifndef NSP_A_H
#define NSP_A_H
namespace nsp{
    template <class T>
    class A{
        protected:
            T elem;

            A();

        public:
            A(T e);

            template <class It>
            static A<T> from(It begin, It end);

            T getElem();
    };
};
#endif

nsp/A.cpp

#include "A.h"

template <class T>
nsp::A<T>::A(){}

template <class T>
nsp::A<T>::A(T elem){ this->elem = elem; }

template <class T, class It>
nsp::A<T> nsp::A<T>::from(It begin, It end){
    nsp::A<T> tmp;

    if(begin != end)
        tmp.elem = static_cast<T>(*begin);

    return tmp;
};

template <class T>
T nsp::A<T>::getElem(){ return this->elem; }

Despite the fact that this code seems correct to me (no syntax errors) and that the IDE (CLion) I am using indicates that all member functions have been implemented, I am given the following error at compile time : (nsp/A.cpp) error: prototype for 'nsp::A<T> nsp::A<T>::from(It, It)' does not match any in class 'nsp::A<T>'

followed by (nsp/A.h) error: candidate is: template<class T> template<class It> static nsp::A<T> nsp::A<T>::from(It, It)

I would like to know what is the cause of this error and how I may solve it.

HolyBlackCat
2#
HolyBlackCat Reply to 2018-02-11 10:19:00Z

When defining a template member from a template class, you have to write template <...> twice:

template <class T>
template <class It>
nsp::A<T> nsp::A<T>::from(It begin, It end){
    nsp::A<T> tmp;

    if(begin != end)
        tmp.elem = static_cast<T>(*begin);

    return tmp;
}

But you have another problem: template functions can only be defined in header files (unless you wish to enumerate all types your templates should work for in advance, the link should explain that too).

You can either define your member functions inside the class body, or below the class in the header file; whatever you like more.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301619 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO