vendredi 19 janvier 2018

Forward declaration of template with incomplete argument

I am stuck with this code and don't understand the error. I'm trying to make a forward declaration of template specialization in a header and then define this specialization in a source file. When I try to include this header, compiler complains that I'm using incomplete type.

Error: $ error: invalid use of incomplete type 'A_Template<Stub *>' std::cout << A_Template<Stub*>().int_method() << std::endl; ^~~~~~~~~~~~~~~~~~~ $ note: forward declaration of 'A_Template<Stub *>' struct A_Template<Stub*>; ^

/* a.h */
#pragma once
template <class T>
struct A_Template{};

class Stub{};

template<>
struct A_Template<Stub*>;


/* a.cpp */
#include "a.h"

template<>
struct A_Template<Stub*> {
    int int_method() {return 42;}
};


/* main.cpp */
#include <iostream>
#include "a.h"

int main() {    
    std::cout << A_Template<Stub*>().int_method();
    return 0;
}

Can anyone tell me what is wrong and is there a way to fix it?

Aucun commentaire:

Enregistrer un commentaire