Your code, unless you have failed the copypaste operation, will not compile in life because it has several errors:
template <typename T>
class Myclass{ // 1
private:
T m_a;
public:
getm(){ return m_a;}; // 2
MyClass(T a)m_a(a){ return; }; // 3
template <typename D>
friend MyClass<D> convert(MyClass<T> param);
} // 4
I'll list you below:
It's MyClass
, not Myclass
(note the c
lower case). C ++ is sensitive to capitalization, do not forget it
Missing indicate the type of return in getm
Initialization of members is done after putting two points :
The declaration of classes must always end in a semicolon.
And as a bonus:
- The implementation of functions is not necessary to end with a semicolon.
- If a function does not do anything the two empty keys are left and ready.
Your class should look like this:
template <typename T>
class MyClass{
private:
T m_a;
public:
T getm(){ return m_a;}
MyClass(T a): m_a(a){ }
template <typename D>
friend MyClass<D> convert(MyClass<T> param);//esto compila hasta
//aqui
};
For future consultations do not lie because you lose your credibility. Do not say that your code compiles when it is not like that.
Now treating your problem with the function friend
, the function will use two different types, then a possible solution would be to declare both explicitly:
template <typename T>
class MyClass{
private:
// ...
template<typename A, typename B>
friend MyClass<A> convert(MyClass<B>);
};
Although taking into account that in the code that shows there is no need for the function to be friend
:
#include <iostream>
#include <cstdio>
template <typename T>
class MyClass{
private:
T m_a;
public:
T getm(){ return m_a;}
MyClass(T a): m_a(a){ }
};
template<typename D, typename T>
MyClass<D> convert(MyClass<T> param){
return MyClass<D>(static_cast<D>(param.getm()));
}
int main()
{
MyClass<int> a(10);
MyClass<short> b = convert<short>(a);
std::cout << b.getm() << '\n';
}