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

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

user2116
1#
user2116 Published in May 25, 2018, 8:57 am

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.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO