phenomenon
// error
// a.h
template<typename T>
struct A {
public:
A(int a):a_(a){std::cout << "A";}
private:
int a_;
};
template<>
A<std::string>::A(int a):a_(a){std::cout << "string A";}
template<>
A<std::vector<int>>::A(int a):a_(a){std::cout << "vector A";}
// aa.h
void func_aa();
// aa.cpp
#include "a.h"
#include "aa.h"
void func_aa() {
auto aa = A<std::string>(1);
}
// ab.h
void func_ab();
// ab.cpp
#include "a.h"
#include "ab.h"
void func_ab() {
auto ab = A<std::string>(1);
auto ab1 = A<std::vector<int>>(1);
}
// main.cpp
#include "aa.h"
#include "ab.h"
int main(){
func_aa();
func_ab();
return 0;
}
Solution
Option 1
Merge aa.h aa.cpp ab.h ab.cpp into AA_ ab.h aa_ Ab.cpp this scheme can solve the problem of error reporting, but the reason cannot be explained clearly=_= Welcome experts to solve doubts!
// aa_ab.h
void func_aa();
void func_ab();
// aa_ab.cpp
#include "a.h"
#include "aa_ab.h"
void func_ab() {
auto ab = A<std::string>(1);
auto ab1 = A<std::vector<int>>(1);
}
void func_aa() {
auto aa = A<std::string>(1);
}
Option 2
Change the constructor of a into a function template and move the specialization to the class. Standard practice: specialize the function template
// a.h
template<typename T>
struct A {
public:
template<typename T1>
A(T1 a):a_(a){std::cout << "A";}
template<>
A(std::string a):a_(a){std::cout << "string A";}
template<>
A(std::vector<int> a):a_(a){std::cout << "vector A";}
T a_;
};
Option 3
Standard Practice for specialization of class template a: specialization of class template
// a.h
template<typename T>
struct A {
public:
A(int a):a_(a){std::cout << "A";}
void print(){std::cout << "A print";}
private:
int a_;
};
template<>
struct A<std::string> {
public:
A(int a){std::cout << "string A";}
private:
int a_;
};
template<>
struct A<std::vector<int>> {
public:
A(int a){std::cout << "vector A";}
private:
int a_;
};