class 模板中的重载运算符>朋友
Overloaded operator> friend in class template
我正在尝试在我的模板 class 中重载 operator> friend 函数。此重载运算符的目标是确定左数组是否大于右数组,而与类型无关。我想要一些类似 arrayInt > arrayFloat 到 return 布尔值的东西。但是,在定义 friend 函数时,我收到一条错误消息,提示我有一个未声明的标识符。我在 Mac 上使用 XCode。知道如何在仍然使用友元重载函数的同时解决这个问题吗?
Array.h
#ifndef Array_h
#define Array_h
#include <iostream>
template <class T> class Array;
template <class T, class S>
bool operator> (const Array<T>& arr1, const Array<S>& arr2);
template <class T>
class Array {
private:
int arraySize;
T * array;
public:
Array() : arraySize(0), array(nullptr){};
Array(int);
~Array();
int getSize(){return this->arraySize;}
void printArray();
//error here, "S is an undeclared identifier"
friend bool operator> (const Array<T>& arr1, const Array<S>& arr2) {
return (arr1.arraySize > arr2.arraySize);
}
};
template <class T>
Array<T>::Array(int size) {
this->arraySize = (size > 0 ? size : 0);
this->array = new T [this->arraySize];
for(int i=0; i<this->arraySize; i++) {
this->array[i] = 0;
}
}
template <class T>
Array<T>::~Array() {
delete [] this->array;
}
template <class T>
void Array<T>::printArray() {
for(int i=0; i<this->arraySize; i++) {
std::cout << this->array[i] << std::endl;
}
}
#endif /* Array_h */
main.cpp
#include "Array.h"
int main(int argc, char ** argv) {
Array<int> arrayInt(5);
Array<float> arrayFloat(10);
std::cout << "Number of elements in integer array: " << arrayInt.getSize()
<< std::endl;
std::cout << "Values in integer array:" << std::endl;
arrayInt.printArray();
std::cout << "Number of elements in float array: " << arrayFloat.getSize()
<< std::endl;
std::cout << "Values in float array:" << std::endl;
arrayFloat.printArray();
bool isGreater = arrayInt > arrayFloat;
std::cout << isGreater;
return 0;
}
朋友声明不匹配函数模板,它也必须是一个模板:
template<typename TT, typename TS> friend bool
operator >(const Array<TT>& arr1, const Array<TS>& arr2) {
return (arr1.arraySize > arr2.arraySize);
}
其实不用加friend,在外面定义,直接调用getSize()
:
template<typename T, typename S> bool
operator >(const Array<T>& arr1, const Array<S>& arr2) {
return (arr1.getSize() > arr2.getSize());
}
我正在尝试在我的模板 class 中重载 operator> friend 函数。此重载运算符的目标是确定左数组是否大于右数组,而与类型无关。我想要一些类似 arrayInt > arrayFloat 到 return 布尔值的东西。但是,在定义 friend 函数时,我收到一条错误消息,提示我有一个未声明的标识符。我在 Mac 上使用 XCode。知道如何在仍然使用友元重载函数的同时解决这个问题吗?
Array.h
#ifndef Array_h
#define Array_h
#include <iostream>
template <class T> class Array;
template <class T, class S>
bool operator> (const Array<T>& arr1, const Array<S>& arr2);
template <class T>
class Array {
private:
int arraySize;
T * array;
public:
Array() : arraySize(0), array(nullptr){};
Array(int);
~Array();
int getSize(){return this->arraySize;}
void printArray();
//error here, "S is an undeclared identifier"
friend bool operator> (const Array<T>& arr1, const Array<S>& arr2) {
return (arr1.arraySize > arr2.arraySize);
}
};
template <class T>
Array<T>::Array(int size) {
this->arraySize = (size > 0 ? size : 0);
this->array = new T [this->arraySize];
for(int i=0; i<this->arraySize; i++) {
this->array[i] = 0;
}
}
template <class T>
Array<T>::~Array() {
delete [] this->array;
}
template <class T>
void Array<T>::printArray() {
for(int i=0; i<this->arraySize; i++) {
std::cout << this->array[i] << std::endl;
}
}
#endif /* Array_h */
main.cpp
#include "Array.h"
int main(int argc, char ** argv) {
Array<int> arrayInt(5);
Array<float> arrayFloat(10);
std::cout << "Number of elements in integer array: " << arrayInt.getSize()
<< std::endl;
std::cout << "Values in integer array:" << std::endl;
arrayInt.printArray();
std::cout << "Number of elements in float array: " << arrayFloat.getSize()
<< std::endl;
std::cout << "Values in float array:" << std::endl;
arrayFloat.printArray();
bool isGreater = arrayInt > arrayFloat;
std::cout << isGreater;
return 0;
}
朋友声明不匹配函数模板,它也必须是一个模板:
template<typename TT, typename TS> friend bool
operator >(const Array<TT>& arr1, const Array<TS>& arr2) {
return (arr1.arraySize > arr2.arraySize);
}
其实不用加friend,在外面定义,直接调用getSize()
:
template<typename T, typename S> bool
operator >(const Array<T>& arr1, const Array<S>& arr2) {
return (arr1.getSize() > arr2.getSize());
}