Home > Back-end >  Template class according to specific problem (explicit specialization)
Template class according to specific problem (explicit specialization)

Time:10-01

Master, I want to give this template class do an explicit embodiment, but to the wrong is going on? Where do wrong? Baidu above template class explicitly embodied all had taken a... Use
 struct Person {
STD: : string m_name;
Short m_age;
};

//template class, used to store two Numbers and exchange, under normal circumstances are implicit instantiation
Template
The class Test {
Private:
T m_a;
T m_b;
Public:
The Test (T, a, T, b);
Void swap ();
Void the show () the const;
};

Template
Test : : Test (T, a, T, b) : m_a (a), m_b (b) {
}

Template
Void Test : : swap () {
T temp=m_a;
M_a m_b=;
M_b=temp;
}

Template
Void Test : : show () const {
STD: : cout & lt; <"A:" & lt; STD: : cout & lt; <"B:" & lt; }

//show the instantiation class template
The template
The class Test;

//display specific class template to handle structure
Template<>
The class Test {
Private:
The Person m_a;
The Person m_b;
Public:
The Test (Person& A, Person& B);
Void swap ();
Void the show () the const;
};

Template<>
Test : : Test (Person& A, Person& B) : m_a (a), m_b (b) {
}

Template<>
Void Test : : swap () {
Auto temp=m_a. M_age;
M_a. M_age=m_b. M_age;
M_b. M_age=temp;
}

Template<>
Void Test : : show () const {
STD: : cout & lt; <"A:" & lt; STD: : cout & lt; <"B:" & lt; }

CodePudding user response:

I recently also encountered such a problem, but I think the STL source is directly in the class content within the definition of function, and is not an error, and then I did.

CodePudding user response:

reference 1st floor Italink response:
recently and I met such a problem, but I think the STL source is directly in the class content within the definition of function, and is not an error, and then I did.

I tried the way you say, really.
There is not the function definition and template<> And then put in another CPP, also can.
Just don't know why would an error...

CodePudding user response:

Delete template<>

A reference from the MSDN (visual is VS ruling)
https://docs.microsoft.com/zh-cn/previous-versions/cx7k7hcf (v=vs. 120)

CodePudding user response:

Template<>
Test : : Test (Person& A, Person& B) : m_a (a), m_b (b) {
}
The book says template<> Is empty, this will lead to display specific, as a template with template<> ,
Define Test< directly outside; Person> : : Test (Person& A, Person& B) : m_a (a), m_b (b) {
},
  • Related