读书人

本人小菜菜鸟运算符重载出了有关问

发布时间: 2013-02-27 10:48:11 作者: rapoo

本人小菜,初学者,运算符重载出了问题,哪位大神帮忙解决一下?谢啦
一共有三个文件:datalist.h , selecttm.h ,unit1_01.cpp
/*---------第一个文件---------*/
#include "stdafx.h"
template <class Type>
class dataList {
private:
Type *Element;
int ArraySize;
void Swap (const int m1, const int m2);
int MaxKey (const int low, const int high);
public:
dataList (int size = 10) : ArraySize (size),
Element (new Type [Size]) { }
~dataList ( ) {delete [ ] Element;}
void Sort ( );
friend ostream& operator << (ostream&
outStream, const datalist<Type>&
outList);
friend istream& operator >> (istream&
inStream, const datalist<Type>&
inList){};
};
#endif

/*---------第二个文件---------*/
#ifndef SELECTTM_H
#define SELECTTM_H
#include "datalist.h"
template <class Type>
void dataList <Type>::
Swap (const int m1, const int m2) {
//交换由m1, m2为下标的两个数组元素的值
Type temp = Element [m1];
Element [m1] = Element [m2];
Element [m2] = temp;
}
template <class Type>
int dataList<Type>::
MaxKey (const int low, const int high) {
//查找数组Element[low]~Element[high]中
//的最大值,函数返回其位置
int max = low;
for (int k = low+1, k <= high, k++)
if ( Element[max] < Element[k] )
max = k;
return max;
}
template <class Type>
ostream&operator<< (ostream& OutStream,
const dataList<Type> OutList) {
OutStream << "Array Contents : \n";
for (int i = 0; i < OutList.ArraySize; i++)
OutStream << OutList.Element[i] << ' ';
OutStream << endl;
OuStream << "Array Current Size : " <<
OutList.ArraySize << endl;
return OutStream;
}
template <class Type>


istream& operator >> (istream& InStream,
dataList<Type> InList) {
//输入对象为InList,输入流对象为InStream
cout << "Enter array Current Size : ";
Instream >> InList.ArraySize;
cout << "Enter array elements : \n";
for (int i = 0; i < InList.ArraySize; i++) {
cout << "Elememt" << i << ":" ;
InStream >> InList.Element[i];
}
return InStream;
}
template <class Type>
void dataList<Type>::Sort ( ) {
//按非递减顺序对ArraySize个关键码
//Element[0]~Element[ArraySize-1]排序。
for ( int i = ArraySize -1; i > 0; i-- ) {
int j = MaxKey (0, i);
if ( j != i ) swap (j, i);
}
}
#endif


/*---------第三个文件---------*/
#include "stdafx.h"
#include "selecttm.h"
const int SIZE = 10;
int main ( ) {
dataList <int> TestList (SIZE);
cin >> TestList;
cout << "Testing Selection Sort : \n" <<
TestList << endl;
TestList.Sort ( );
cout << "After sorting : \n" <<
TestList << endl;
return 0;
}

编译出的问题是:
1>d:\program\c++test\unit1_01\unit1_01\datalist.h(17): error C2143: 语法错误 : 缺少“,”(在“<”的前面)
1> d:\program\c++test\unit1_01\unit1_01\datalist.h(22): 参见对正在编译的类 模板 实例化“dataList<Type>”的引用
1>d:\program\c++test\unit1_01\unit1_01\datalist.h(20): error C2143: 语法错误 : 缺少“,”(在“<”的前面)
1>d:\program\c++test\unit1_01\unit1_01\datalist.h(17): error C2143: 语法错误 : 缺少“,”(在“<”的前面)
1> d:\program\c++test\unit1_01\unit1_01\unit1_01.cpp(8): 参见对正在编译的类 模板 实例化“dataList<Type>”的引用
1> with
1> [
1> Type=int
1> ]
1>d:\program\c++test\unit1_01\unit1_01\datalist.h(20): error C2143: 语法错误 : 缺少“,”(在“<”的前面)


在线坐等问题解决,多谢啦~~ c++ class
------解决方案--------------------


模板类的声明和定义要放在一起,不能分成头文件和cpp文件。因为现在的编译器大多还不支持模板的分离编译。
[解决办法]

引用:
关于link错误,楼主参考下这个:http://blog.csdn.net/yyzsyx/article/details/6048582

刚刚试了下,直接将重载流操作符的函数定义写在类的声明里是可以的:
#include <iostream>
using namespace std;

template <class Type>
class dataList {
private:
Type *Element;
int ArraySize;
void Swap (const int m1, const int m2);
int MaxKey (const int low, const int high);
public:
dataList (int size = 10) : ArraySize (size),
Element (new Type [size]) {}
~dataList () {delete [] Element;}
void Sort ();
friend ostream& operator<< (ostream& OutStream, const dataList<Type>& OutList)
{
OutStream << "Array Contents : \n";
for (int i = 0; i < OutList.ArraySize; i++)
OutStream << OutList.Element[i] << ' ';
OutStream << endl;
OutStream << "Array Current Size : " <<OutList.ArraySize << endl;
return OutStream;
}
friend istream& operator>> (istream&
inStream, dataList<Type>&
inList){
//输入对象为InList,输入流对象为InStream
//cout << "Enter array Current Size : ";
InStream >> InList.ArraySize;
cout << "Enter array elements : \n";
for (int i = 0; i < InList.ArraySize; i++) {
//cout << "Elememt" << i << ":" ;
InStream >> InList.Element[i];
}
return InStream;
}
};

读书人网 >C++

热点推荐