Home > Blockchain >  Template must have trailing return type
Template must have trailing return type

Time:12-08

To prevent quick judgement - I read enough resources online and did not quite understand them as they are too advanced for me.

Basically, what I'm trying to do is create a header file which lets me use any type. The C way of doing this is a template, as I understand.

Now I'm getting weird errors and am left a bit clueless.

A minimal example of this would be as follows:

Map.h

#ifndef __MAP_H__
#define __MAP_H__

template <typename K, typename V>
class Map{
private:
  ...
public:
  Map(K, V);
  ...
};

#endif

Map.cpp

#include "Map.h"
#include <string>

Map(unsigned A, std::string B){
  std::cout << A << B << endl;
}

This would result in an error message like:

error: deduction guide for ‘Map<K, V>’ must have trailing return type

CodePudding user response:

As explained in comments, it looks like you're trying to define a specialization of your templated class with

Map(unsigned A, std::string B){
  std::cout << A << B << endl;
}

C only allows you to specialize the whole thing via explicit template specialization:

template<>
class Map<unsigned, std::string> {
    // [...]
    Map(unsigned A, std::string B){
        std::cout << A << B << endl;
    }
    // [...]
}

Then you'll find that templates and source (i.e. .cpp) files don't mix very well, see why templates can only be implemented in header files.

  •  Tags:  
  • c
  • Related