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_;
};
Read More:
- Android studio simulator architecture choose arm or x86 to solve the problem of missing libraryexception
- .. ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status make[2]: ***
- swift stdlib tool error is reported after xcode upgrade xcode9 1
- RuntimeError: Expected hidden[0] size (x, x, x), got(x, x, x)
- [Solved] emulator: glteximage2d: got err pre 🙁 0x502 internal 0x1908 format 0x1908 type 0x1401
- Apache Groovy——java.lang.NoSuchMethodError: x.x.x: method <init>()V not found
- CLP: error: getaddrinfo enotfound http://x.x.x.x/
- System architecture and product design of resource scheduling platform
- [-] handler failed to bind to x.x.x.x: Port troubleshooting
- module ‘win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribute ‘CLSIDToClassMa
- A problem occurred configuring project ‘:x x x‘. > java.lang.NullPointerException (no error message)
- 1067 – Invalid default value for ‘sex‘1366 – Incorrect string value: ‘\xE6\x8A\x80\xE6\x9C\xAF…‘ f
- The corresponding solutions to the problems of various external symbols that cannot be resolved
- Could not write JSON: write javaBean error, fastjson version x.x.x, class
- [study notes] a quick search of latex mathematical symbols
- Display spaces and tab symbols in vscode
- Error LNK1169: one or more multiple defined symbols were found
- Markdown and latex: approximately equal to the writing of symbols
- error adding symbols: DSO missing from command line
- GCC forces all symbols in the link library