[问题]一个C++编译问题,调得我快崩溃了,是在不知道错在哪里,期待牛人帮忙

C、C++和Java语言
回复
头像
roamer
帖子: 43
注册时间: 2007-04-18 3:09
来自: UESTC
送出感谢: 0
接收感谢: 0
联系:

[问题]一个C++编译问题,调得我快崩溃了,是在不知道错在哪里,期待牛人帮忙

#1

帖子 roamer » 2008-02-01 16:04

以下是array.h的全部代码,为什么通不过编译呢?

代码: 全选

#ifndef ARRAY_H
#define ARRAY_H

template<class T>
class Array{
protected:
	T* data;
	unsigned int base;
	unsigned int length;
public:
	Array();
	Array(unsigned int len,unsigned int base);
	~Array();
	Array(const Array& arr);
	Array& operator = (const Array& arr);
	T& operator [] (unsigned int index) const;
	const T* Data() const;
	unsigned int Base() const;
	unsigned int Length() const;
	void SetBase(unsigned int newbase);
	void SetLength(unsigned int newlength);
	void CopyTo(Array&) const;
};

template<class T>
Array<T>::Array() :
	data(new T[1]),
	base(0),
	length(0)
{}

template<class T>
Array<T>::Array(unsigned int len,unsigned int base = 0) :
	data(new T[len]),
	base(base),
	length(len)
{}

template<class T>
Array<T>::Array(const Array<T>& arr):
	data(new T[arr.length]),
	base(arr.base),
	length(arr.lenth)
{
	for(unsigned int i = 0; i< length;i++)
		data[i] = arr.data[i];
}

template<class T>
Array<T>::~Array()
{
	delete [] data;
}

template<class T>
const T* Array<T>::Data() const
{
	return data;
}

template<class T>
unsigned int Array<T>::Base() const
{
	return base;
}

template<class T>
unsigned int Array<T>::Length() const
{
	return length;
}

template<class T>
T& Array<T>::operator [] (unsigned int index) const
{
	unsigned int const offset = index - base;
		if(offset >= length)
			throw ("invalid array index!\n");
	return data[offset];
}

template<class T>
void Array<T>::SetBase(unsigned int newbase)
{
	this->base = newbase;
}

template<class T>
void Array<T>::SetLength(unsigned int newlength)
{
	T* newData = new T[newlength];
	unsigned int const min = length < newlength ? length : newlength;
	for(unsigned int i = 0; i < min; i++)
		newData[i] = data[i];
	delete [] data;
	data = newData;
	length = newlength;
}

template<class T>
void Array<T>::CopyTo(Array<T>& array) const{
	unsigned int tmp = array.Base();
	array.SetBase(this->Base());
	array.SetLength(this->Length());
	for(unsigned int i = 0; i<this->length; i++)
		array[i] = this->data[i];
	array.SetBase(tmp);
}

/**
 * Array2D
 */
template<class T>
class Array2D{
protected:
	unsigned int numberOfRows;
	unsigned int numberOfColumns;
	Array<T> array;
public:
	class Row{
		Array2D& array2D;
		unsigned int const row;
	public:
		Row(Array2D& _array2D,unsigned int _row) : array2D(_array2D),row(_row) {}
		T& operator[] (unsigned int column) const{	return array2D.Select(row,column);}
	};
	Array2D(unsigned int,unsigned int);
	T& Select(unsigned int,unsigned int);
	Row operator[] (unsigned int);
};

template<class T>
Array2D<T>::Array2D(unsigned int m,unsigned int n) : numberOfRows(m),numberOfColumns(n),array(m*n) {}
template<class T>
T& Array2D<T>::Select(unsigned int i,unsigned int j){
	if(i >= this->numberOfRows)
		throw "invalid row.";
	if(j >= this->numberOfColumns)
		throw "invalid column.";
	return array[i*this->numberOfColumns + j];
}


template<class T>
Array2D<T>::Row Array2D<T>::operator [](unsigned int row){
	return Row(*this,row);
}

/**
 * class Matrix
 */
template<class T>
class Matrix : public Array2D<T>{
public:
	Matrix(unsigned int,unsigned int);
	Matrix operator * (const Matrix&) const;
	Matrix operator + (const Matrix&) const;
};

template<class T>
Matrix<T>::Matrix(unsigned int m,unsigned int n) : numberOfRows(m),numberOfColumns(n),array2D(m*n) {}

template<class T>
Matrix<T> Matrix<T>::operator * (const Matrix& arg) const{
	if(this->numberOfColumns != arg.numberOfRows)
		throw "incompatible matrices.";
	Matrix<T> result(this->numberOfRows,arg.numberOfColumns);
	for(unsigned int i = 0; i < this->numberOfRows; i++){
		for(unsigned int j = 0; j < arg.numberOfColumns; j++){
			T sum = 0;
			for(unsigned int k = 0; k < this->numberOfColumns; k++)
				sum += (*this)[i][k] * arg[k][j];
			result[i][j] = sum;
		}
	}
	return result;
}
template<class T>
Matrix<T> Matrix<T>::operator + (const Matrix& arg) const{
	if(this->numberOfColumns != arg.numberOfColumns || this->numberOfRows != arg.numberOfRows)
		throw "incompatible matrices.";
	Matrix<T> result(this->numberOfRows,this->numberOfColumns);
	for(unsigned int i = 0; i < this->numberOfRows; i++){
		for(unsigned int j = 0; j < this->numberOfColumns; j++)			
			result[i][j] = (*this)[i][j] + arg[i][j];
	}
	return result;
}
#endif

主函数非常简单,什么都不做,但以上代码就是没办法通过编译

代码: 全选

#include "array.h"
int main()
{
      return 0;
}
在这个函数

代码: 全选

template<class T>
Array2D<T>::Row Array2D<T>::operator [](unsigned int row){
	return Row(*this,row);
}
错误提示为:array.h:145: error: expected constructor, destructor, or type conversion before ‘Array2D’
在这个地方

代码: 全选

Matrix<T>::Matrix(unsigned int m,unsigned int n) : numberOfRows(m),numberOfColumns(n),array2D(m*n) {}
错误提示为:
array.h: In constructor ‘Matrix<T>::Matrix(unsigned int, unsigned int)’:
array.h:161: error: class ‘Matrix<T>’ does not have any field named ‘numberOfRows’
array.h:161: error: class ‘Matrix<T>’ does not have any field named ‘numberOfColumns’
array.h:161: error: class ‘Matrix<T>’ does not have any field named ‘array2D’
怎么看得莫名其妙的
上次由 roamer 在 2008-02-01 16:12,总共编辑 1 次。
头像
roamer
帖子: 43
注册时间: 2007-04-18 3:09
来自: UESTC
送出感谢: 0
接收感谢: 0
联系:

#2

帖子 roamer » 2008-02-01 16:11

不好意思代码有点多,但是不用看逻辑,为什么会统不过编译呢
头像
frank7258
帖子: 61
注册时间: 2007-01-31 23:01
来自: 慕尼黑
送出感谢: 0
接收感谢: 0
联系:

#3

帖子 frank7258 » 2008-02-02 8:43

看看这样:

代码: 全选

Matrix<T>::Matrix(unsigned int m,unsigned int n) : Array2D<T>::numberOfRows(m),Array2D<T>::numberOfColumns(n),Array2D<T>::array2D(m*n) {} 
头像
roamer
帖子: 43
注册时间: 2007-04-18 3:09
来自: UESTC
送出感谢: 0
接收感谢: 0
联系:

#4

帖子 roamer » 2008-02-02 13:05

frank7258 写了:看看这样:

代码: 全选

Matrix<T>::Matrix(unsigned int m,unsigned int n) : Array2D<T>::numberOfRows(m),Array2D<T>::numberOfColumns(n),Array2D<T>::array2D(m*n) {} 
首先谢谢你的热心:)
嗯,这样是可以的,也可以这样

代码: 全选

Matrix<T>::Matrix(unsigned int row,unsigned int column) : Array2D<T>(row,column) {}
这样这里是没有问题了,不过在这里有问题

代码: 全选

Row operator [] (unsigned int);
实在搞不懂后我把类Row放到外面

代码: 全选

template <class T>
class Row
{
	Array2D<T>& array2D;
	unsigned int const row;
public:
	Row (Array2D<T>& _array2D, unsigned int _row) :  array2D (_array2D), row (_row) {}
	T& operator [] (unsigned int column) const  { return array2D.Select (row, column); }
};
这样似乎可以了,在visual studio 2005里面确实可以运行了,但是我转到liunx系统下发现Matrix类没有办法通过编译,两个运算符重载函数都报错说numberOfRows和numberOfColumns没有定义
这是怎么回事呢?
头像
frank7258
帖子: 61
注册时间: 2007-01-31 23:01
来自: 慕尼黑
送出感谢: 0
接收感谢: 0
联系:

#5

帖子 frank7258 » 2008-02-02 20:58

Visual Studio 2005下你那种方法应该也是同不过编译的。要初始化的成员变量一定要存在阿,如果是父类的得说明的。你后来的那种方法是调用了父类的构造函数。

代码: 全选

Row operator [] (unsigned int);
代码在哪里我都找不到。

这里出错有可能是你Row这个类没有复制构造函数。

代码: 全选

Row::Row(Row&);
代码太多了,不高兴看了。[/code]
头像
titainium
帖子: 689
注册时间: 2006-12-02 12:25
送出感谢: 0
接收感谢: 0

#6

帖子 titainium » 2008-02-03 16:54

unsigned int const row;

Row(Array2D& _array2D,unsigned int _row) : array2D(_array2D),row(_row) {}

这两句有问题。
Titainium
回复

回到 “C/C++/Java”