gpt4 book ai didi

c++ - 将 Array 类转换为模板

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:38 25 4
gpt4 key购买 nike

我正在尝试将我的数组类转换为模板,但我不断收到错误消息“错误 C2955:‘数组’:类模板的使用需要模板”

这是我尝试转换后的类(class)。

// Fig. 10.10: Array.h
// Array class definition with overloaded operators.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdexcept>
#ifndef ARRAY_H
#define ARRAY_H
using namespace std;

template<typename T>
class Array
{
friend ostream &operator<<( ostream &, const Array & );
friend istream &operator>>( istream &, Array & );

public:
explicit Array(int arraySize = 3)// default constructor
: size( arraySize > 0 ? arraySize :
throw invalid_argument( "Array size must be greater than 0" ) ),
ptr( new T[ size ] )
{
for ( size_t i = 0; i < size; ++i )

ptr[ i ] = 0; // set pointer-based array element
}

~Array()// destructor
{
delete [] ptr; // release pointer-based array space
}

size_t getSize() const
{
return size; // number of elements in Array
} // end function getSize

const Array &operator=( const Array &right )
{
if ( &right != this ) // avoid self-assignment
{
// for Arrays of different sizes, deallocate original
// left-side Array, then allocate new left-side Array
if ( size != right.size )
{
delete [] ptr; // release space
size = right.size; // resize this object
ptr = new T[ size ]; // create space for Array copy
} // end inner if

for ( size_t i = 0; i < size; ++i )
ptr[ i ] = right.ptr[ i ]; // copy array into object
} // end outer if

return *this; // enables x = y = z, for example
} // end function operator=
bool operator==( const Array &right ) const
{
if ( size != right.size )
return false; // arrays of different number of elements

for ( size_t i = 0; i < size; ++i )
if ( ptr[ i ] != right.ptr[ i ] )
return false; // Array contents are not equal

return true; // Arrays are equal
} // end function operator==

// subscript operator for const objects returns rvalue
T operator[]( int subscript ) const
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= size )
throw out_of_range( "Subscript out of range" );

return ptr[ subscript ]; // returns copy of this element
} // end function operator[]


private:
size_t size; // pointer-based array size
T *ptr; // pointer to first element of pointer-based array
}; // end class Array
template <typename T>
istream &operator>>( istream &input, Array &a )
{
for ( size_t i = 0; i < a.size; ++i )
input >> a.ptr[ i ];

return input; // enables cin >> x >> y;
} // end function
template <typename T>
ostream &operator<<( ostream &output, const Array &a )
{
// output private ptr-based array
for ( size_t i = 0; i < a.size; ++i )
{
output << setw( 12 ) << a.ptr[ i ];

if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
output << endl;
} // end for

if ( a.size % 4 != 0 ) // end last line of output
output << endl;

return output; // enables cout << x << y;
} // end function operator<<
#endif

错误来自 isteam 和 ostream 函数。使用类模板错误似乎是我遇到的唯一问题。感谢您的帮助。

最佳答案

您对模板化类是什么有一点误解,当您创建模板化类时,您需要完全限定应返回模板化类的方法。

template<typename T> Array {
const Array<T>& operator=(const Array<T>& right);
}

这是因为如果没有模板类型,数组就不存在,如果您使用了数组和数组,但是您的方法返回一个数组,编译器应该如何知道您指的是哪个数组?

关于c++ - 将 Array 类转换为模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43277405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com