应用类模板的排序算法出现编译错误,帮忙看看,谢谢

软件和网站开发以及相关技术探讨
回复
头像
comfanter
帖子: 190
注册时间: 2009-05-02 14:51

应用类模板的排序算法出现编译错误,帮忙看看,谢谢

#1

帖子 comfanter » 2009-08-23 18:36

帮忙看看这些错误怎么解决,谢谢

/home/linux/cpp/algorithm/sort/sort.h|159|错误: expected constructor, destructor, or type conversion before ‘<’ token|
/home/linux/cpp/algorithm/sort/sort.h|171|错误: expected initializer before ‘<’ token|
/home/linux/cpp/algorithm/sort/sort.h|202|错误: expected initializer before ‘<’ token|
/home/linux/cpp/algorithm/sort/sort.h|221|错误: expected initializer before ‘<’ token|
/home/linux/cpp/algorithm/sort/sort.h|266|错误: expected initializer before ‘<’ token|
/home/linux/cpp/algorithm/sort/main.cpp||In function ‘int main()’:|
/home/linux/cpp/algorithm/sort/main.cpp|198|错误: 对‘sort’的引用有歧义|
/home/linux/cpp/algorithm/sort/sort.h|144|错误: candidates are: template<class T> class sort|
/usr/include/c++/4.2/bits/stl_algo.h|2852|错误: template<class _RandomAccessIterator, class _Compare> void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare)|
/usr/include/c++/4.2/bits/stl_algo.h|2816|错误: template<class _RandomAccessIterator> void std::sort(_RandomAccessIterator, _RandomAccessIterator)|
/home/linux/cpp/algorithm/sort/main.cpp|198|错误: expected primary-expression before ‘long’|
/home/linux/cpp/algorithm/sort/main.cpp|198|错误: expected `;' before ‘long’|
/home/linux/cpp/algorithm/sort/main.cpp|202|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|205|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|208|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|212|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|233|错误: 对‘sort’的引用有歧义|
/home/linux/cpp/algorithm/sort/sort.h|144|错误: candidates are: template<class T> class sort|
/usr/include/c++/4.2/bits/stl_algo.h|2852|错误: template<class _RandomAccessIterator, class _Compare> void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare)|
/usr/include/c++/4.2/bits/stl_algo.h|2816|错误: template<class _RandomAccessIterator> void std::sort(_RandomAccessIterator, _RandomAccessIterator)|
/home/linux/cpp/algorithm/sort/main.cpp|233|错误: expected primary-expression before ‘double’|
/home/linux/cpp/algorithm/sort/main.cpp|233|错误: expected `;' before ‘double’|
/home/linux/cpp/algorithm/sort/main.cpp|237|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|240|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|243|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|247|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|269|错误: 对‘sort’的引用有歧义|
/home/linux/cpp/algorithm/sort/sort.h|144|错误: candidates are: template<class T> class sort|
/usr/include/c++/4.2/bits/stl_algo.h|2852|错误: template<class _RandomAccessIterator, class _Compare> void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare)|
/usr/include/c++/4.2/bits/stl_algo.h|2816|错误: template<class _RandomAccessIterator> void std::sort(_RandomAccessIterator, _RandomAccessIterator)|
/home/linux/cpp/algorithm/sort/main.cpp|269|错误: expected primary-expression before ‘int’|
/home/linux/cpp/algorithm/sort/main.cpp|269|错误: expected `;' before ‘int’|
/home/linux/cpp/algorithm/sort/main.cpp|273|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|276|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|279|错误: ‘s’在此作用域中尚未声明|
/home/linux/cpp/algorithm/sort/main.cpp|283|错误: ‘s’在此作用域中尚未声明|
||=== Build finished: 35 errors, 0 warnings ===|


代码: 全选

sort.h
#include <cstdlib>
#include <iostream>
using namespace std;


template <class T>
class sort {
public:
	sort(int);								// constructor
	void bubble(T array[]);					// bubble sort
	void insertion(T array[]);				// insertion sort
	void quick(T array[], int, int);		// quick sort
	void selection(T array[]);				// selectioni sort

private:
	int size;
};



template <class T>
sort<T>::sort(int s) {
	if (s < 0) {			// make sure the size is valid
		cerr << "** An invalid array size was entered." << endl;
		exit(-1);
		return;
	}

	size = s;				// if the size is valid, assign it
}


template <class T>
void sort<T>::bubble(T array[]) {

	T temp;
	int last = size - 1;
	bool sorted = true;

	do {
		sorted = true;
		for (int i = 0; i < last; i++) {

			/*
				swap elements if the higher index element is
				greater than the smaller index element
			 */

			if (array[i] > array[i + 1]) {
				temp = array[i];
				array[i] = array[i + 1];
				array[i + 1] = temp;
				sorted = false;
			}

		}

		last--;
	} while (!sorted);

}


template <class T>
void sort<T>::insertion(T array[]) {

	T cVal;		// current value being examined

	for (int i = 1; i < size; i++) {

		cVal = array[i];
		for (int n = i - 1; n >= 0 && cVal < array[n]; n--) {
			array[n + 1] = array[n];
		}

		array[n + 1] = cVal;

	}	// end for loop

}


template <class T>
void sort<T>::quick(T array[], int llimit, int rlimit) {

	T temp;
	int left = llimit;
	int right = rlimit;
	int pivot = (left + right) / 2;	// find the median
	T median = array[pivot];


	do {

		while ((array[left] < median) && (left < rlimit)) {
			left++;
		}

		while ((median < array[right]) && (right > llimit)) {
			right--;
		}

		if (left <= right) {

			// swap elements
			temp = array[left];
			array[left] = array[right];
			array[right] = temp;
			left++;
			right--;
		}

	} while (left <= right);


	if (llimit < right) {
		sort<T>::quick(array, llimit, right);
	}


	if (left < rlimit) {
		sort<T>::quick(array, left, rlimit);
	}

}


template <class T>
void sort<T>::selection(T array[]) {

	T temp;
	int min;

	for (int i = 0; i < size - 1; i++) {
		min = i;

		for (int n = i + 1; n < size; n++) {
			if (array[n] < array[min]) {
				min = n;
			}

			temp = array[min];
			array[min] = array[i];
			array[i] = temp;
		}
	}

}

main.cpp

#include <cstdlib>
#include <iostream>
#include "sort.h"
using namespace std;

int typemenu(int &n);
int sortmenu();

int main() {

	int i = 0, num = 0, choice = 0;
	choice = typemenu(num);	// find what type of data to store


	if (choice == 1) {
		long in;					// used to store data in the array
		long * l = new long [num];	// dynamically create the array
		cout << endl << endl;
		for (i = 0; i < num; i++) {	// get user input for array elements
			cout << "element " << i + 1 << ":  ";
			cin >> in;
			l[i] = in;
		}

		sort<long> s(num);			// create sort object
		choice = sortmenu();		// determine which sort to use

		if (choice == 1) {
			s.bubble(l);

		} else if (choice == 2) {
			s.insertion(l);

		} else if (choice == 3) {
			s.quick(l, 0, num - 1);

		} else
		{
			s.selection(l);
		}

		for (i = 0; i < num; i++) {
			cout << l[i] << ' ';
		}

		cout << endl;
		delete [] l;				// return allocated memory to system
	}

	else if (choice == 2) {
		double in;						// used to store data in the array
		double * d = new double [num];	// dynamically create the array
		cout << endl << endl;
		for (i = 0; i < num; i++) {		// get user input for array elements
			cout << "element " << i + 1 << ":  ";
			cin >> in;
			d[i] = in;
		}

		sort<double> s(num);		// create sort object
		choice = sortmenu();		// determine which sort to use

		if (choice == 1) {
			s.bubble(d);

		} else if (choice == 2) {
			s.insertion(d);

		} else if (choice == 3) {
			s.quick(d, 0, num - 1);

		} else
		{
			s.selection(d);
		}

		for (i = 0; i < num; i++) {
			cout << d[i] << ' ';
		}

		cout << endl;
		delete [] d;				// return allocated memory to system
	}


	else if (choice == 3) {
		int in;						// used to store array elements
		int * iA = new int [num];	// dynamically create array
		cout << endl << endl;
		for (i = 0; i < num; i++) {	// retrieve array elements
			cout << "element " << i + 1 << ":  ";
			cin >> in;
			iA[i] = in;
		}

		sort<int> s(num);			// create sort object
		choice = sortmenu();		// determine which sort to use

		if (choice == 1) {
			s.bubble(iA);

		} else if (choice == 2) {
			s.insertion(iA);

		} else if (choice == 3) {
			s.quick(iA, 0, num - 1);

		} else
		{
			s.selection(iA);
		}

		for (i = 0; i < num; i++) {
			cout << iA[i] << ' ';
		}

		cout << endl;
		delete [] iA;				// return allocated memory to system
	}


	else {
		cerr << "** Your choice was invalid." << endl;
		exit(-1);

	}


	cout << endl << endl;	// output buffer
	cin.get();
	cin.ignore();

}



int typemenu(int &n) {

	int choice;

	cout << "Enter the type to store:" << endl << endl;
	cout << "\t1.  long" << endl;
	cout << "\t2.  double" << endl;
	cout << "\t3.  integer" << endl << endl;
	cout << "> What type (1 - 3):  ";
	cin >> choice;

	cout << "\n> How many elements would you like to store:  ";
	cin >> n;

	return choice;
}


int sortmenu() {

	int choice;

	cout << endl << endl;
	cout << "Enter the sort to use:" << endl << endl;
	cout << "\t1.  bubble" << endl;
	cout << "\t2.  insertion" << endl;
	cout << "\t3.  quick" << endl;
	cout << "\t4.  selection" << endl << endl;
	cout << "> What type (1 - 4):  ";
	cin >> choice;
	cout << endl << endl;

	return choice;
}
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 应用类模板的排序算法出现编译错误,帮忙看看,谢谢

#2

帖子 BigSnake.NET » 2009-08-23 19:09

std 里面本来就有个 sort
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
comfanter
帖子: 190
注册时间: 2009-05-02 14:51

Re: 应用类模板的排序算法出现编译错误,帮忙看看,谢谢

#3

帖子 comfanter » 2009-08-23 19:22

我把sort改为Sort后就只有两个错误了
/home/linux/cpp/algorithm/sort/sort.h||In member function ‘void Sort<T>::insertion(T*)’:|
/home/linux/cpp/algorithm/sort/sort.h|213|错误: 在新的 ISO‘for’作用域中,‘n’的名称查找有变化|
/home/linux/cpp/algorithm/sort/sort.h|209|错误: 在‘n’使用过时的绑定|
||=== Build finished: 2 errors, 0 warnings ===|
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

Re: 应用类模板的排序算法出现编译错误,帮忙看看,谢谢

#4

帖子 BigSnake.NET » 2009-08-23 19:29

comfanter 写了:我把sort改为Sort后就只有两个错误了
/home/linux/cpp/algorithm/sort/sort.h||In member function ‘void Sort<T>::insertion(T*)’:|
/home/linux/cpp/algorithm/sort/sort.h|213|错误: 在新的 ISO‘for’作用域中,‘n’的名称查找有变化|
/home/linux/cpp/algorithm/sort/sort.h|209|错误: 在‘n’使用过时的绑定|
||=== Build finished: 2 errors, 0 warnings ===|
在for第一分句中声明的东西只在for里面有效
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
comfanter
帖子: 190
注册时间: 2009-05-02 14:51

Re: 应用类模板的排序算法出现编译错误,帮忙看看,谢谢

#5

帖子 comfanter » 2009-08-23 19:37

:em11
BigSnake.NET 写了:
comfanter 写了:我把sort改为Sort后就只有两个错误了
/home/linux/cpp/algorithm/sort/sort.h||In member function ‘void Sort<T>::insertion(T*)’:|
/home/linux/cpp/algorithm/sort/sort.h|213|错误: 在新的 ISO‘for’作用域中,‘n’的名称查找有变化|
/home/linux/cpp/algorithm/sort/sort.h|209|错误: 在‘n’使用过时的绑定|
||=== Build finished: 2 errors, 0 warnings ===|
在for第一分句中声明的东西只在for里面有效
已经编译通过了,Thank you very much!
回复