类 的 C++ 模板
C++ Templates for classes
在这段代码中,我尝试为我的 class 创建一个模板,还有 2 个模板函数,一个用于标准类型,一个用于我的模板 class,但我想看看我是否可以在模板中制作模板以获得定义的功能(我不知道这是不是正确的制作方式)。最后,在我 运行 代码之后,它给了我一些奇怪的错误,比如语法错误等。谢谢你的帮助!
#include <iostream>
template <typename T>
class Complex
{
T _re, _im;
public:
Complex(T re = 0, T im = 0) :_re{re}, _im{im} {};
~Complex() {}
Complex<T>& operator=(const Complex<T>& compl) { if (this == &compl) return *this; this._re = compl._re; this._im = compl._im; return *this; }
Complex<T> operator+(const Complex<T>& compl) { Complex<T> temp; temp._re = _re + compl._re; temp._im = _im + compl._im; return temp}
friend std::ostream& operator<<(std::ostream& os, const Complex<T>& compl) { os << compl._re << " * i " << compl._im; return os; }
};
template <typename T>
T suma(T a, T b)
{
return a + b;
}
template <template<typename> typename T, typename U>
void suma(T<U> a, T<U> b)
{
T<U> temp;
temp = a + b;
std::cout << temp;
}
int main()
{
int a1{ 1 }, b1{ 3 };
std::cout << "Suma de int : " << suma<int>(a1, b1) << std::endl;
double a2{ 2.1 }, b2{ 5.9 };
std::cout << "Suma de double: " << suma<double>(a2, b2) << std::endl;
Complex<int> c1(3, 5), c2(8, 9);
std::cout << "Suma comple int: ";
suma<Complex, int>(c1, c2);
return 0;
}
compl
是一个 c++ 关键字(作为一元运算符 ~
的替代)。您使用 compl
作为运算符重载参数。
几分钟前我还不知道这一点。我是怎么知道的?我将您的代码粘贴到 godbolt.org 中,它突出显示了单词“compl”,就好像它们是关键字一样。后来上网一查,果然是关键词。
还有 this._re = ...
和 this._im = ...
的使用,这不起作用,因为 this
是指针而不是引用,所以它应该是 this->_re = ...
或最好是 _re = ...
.
你的程序有 3 个错误。
First 而不是使用关键字 compl
你可以像我一样使用其他名称 here.
第二个,你在上面的代码 link.[=17 中修复的语句 return temp
之后缺少 ;
=]
第三个 您正在使用
this._re = rhs._re;
this._im = rhs._im;
虽然正确的写法是:
_re = rhs._re;//note the removed this.
_im = rhs._im;//note the removed this.
更正这些错误后,程序可以正常工作 here。
在这段代码中,我尝试为我的 class 创建一个模板,还有 2 个模板函数,一个用于标准类型,一个用于我的模板 class,但我想看看我是否可以在模板中制作模板以获得定义的功能(我不知道这是不是正确的制作方式)。最后,在我 运行 代码之后,它给了我一些奇怪的错误,比如语法错误等。谢谢你的帮助!
#include <iostream>
template <typename T>
class Complex
{
T _re, _im;
public:
Complex(T re = 0, T im = 0) :_re{re}, _im{im} {};
~Complex() {}
Complex<T>& operator=(const Complex<T>& compl) { if (this == &compl) return *this; this._re = compl._re; this._im = compl._im; return *this; }
Complex<T> operator+(const Complex<T>& compl) { Complex<T> temp; temp._re = _re + compl._re; temp._im = _im + compl._im; return temp}
friend std::ostream& operator<<(std::ostream& os, const Complex<T>& compl) { os << compl._re << " * i " << compl._im; return os; }
};
template <typename T>
T suma(T a, T b)
{
return a + b;
}
template <template<typename> typename T, typename U>
void suma(T<U> a, T<U> b)
{
T<U> temp;
temp = a + b;
std::cout << temp;
}
int main()
{
int a1{ 1 }, b1{ 3 };
std::cout << "Suma de int : " << suma<int>(a1, b1) << std::endl;
double a2{ 2.1 }, b2{ 5.9 };
std::cout << "Suma de double: " << suma<double>(a2, b2) << std::endl;
Complex<int> c1(3, 5), c2(8, 9);
std::cout << "Suma comple int: ";
suma<Complex, int>(c1, c2);
return 0;
}
compl
是一个 c++ 关键字(作为一元运算符 ~
的替代)。您使用 compl
作为运算符重载参数。
几分钟前我还不知道这一点。我是怎么知道的?我将您的代码粘贴到 godbolt.org 中,它突出显示了单词“compl”,就好像它们是关键字一样。后来上网一查,果然是关键词。
还有 this._re = ...
和 this._im = ...
的使用,这不起作用,因为 this
是指针而不是引用,所以它应该是 this->_re = ...
或最好是 _re = ...
.
你的程序有 3 个错误。
First 而不是使用关键字 compl
你可以像我一样使用其他名称 here.
第二个,你在上面的代码 link.[=17 中修复的语句 return temp
之后缺少 ;
=]
第三个 您正在使用
this._re = rhs._re;
this._im = rhs._im;
虽然正确的写法是:
_re = rhs._re;//note the removed this.
_im = rhs._im;//note the removed this.
更正这些错误后,程序可以正常工作 here。