我想创建一个复杂的'模板类执行复数计算。但是当我创建一个成员函数时,编译器会抛出一个"标识符未定义"我尝试调用该函数时出错。
#include <iostream>
template <class T>
class complex {
private:
T rl, im;
public:
complex() : rl(0), im(0) {}
complex(T x, T y) : rl(x), im(y) {}
void read();
complex<T> conjugate (const complex<T>& param);
};
template <class T>
void complex<T>::read() {
std::cout << "Enter the real no. and the imaginery part: " << std::endl;
std::cin >> rl >> im;
}
template< class T >
complex<T> complex<T>::conjugate(const complex<T>& param) {
complex temp;
temp.rl = param.rl;
temp.im = -param.im;
return temp;
}
主要功能看起来像
#include <iostream>
#include "complex.h"
int main() {
complex<int> ob1(4, 5);
complex<int> result;
result = conjugate(ob1); //not able to call conjugate, "identifier is undefined" error
result.display();
return 0;
}