指针的模板数组类

Template array class of pointers

本文关键字:数组 指针      更新时间:2023-10-16


我遇到了一个问题,我已经尝试解决了一段时间,但根本无法解决。以下是场景:
1( 我有一个模板数组类,看起来像这个

//code taken from http://www.learncpp.com, much appreciation for Alex
#ifndef ARRAY_H
#define ARRAY_H 
#include <assert.h> // for assert()
template <typename T>
class Array {
private:
    int m_nLength;
    T *m_ptData;
public:  
    Array() {
        m_nLength = 0;
        m_ptData = 0;
    }
    Array(int nLength) {
        m_ptData= new T[nLength];
        m_nLength = nLength;
    }
    ~Array() {
        delete[] m_ptData;
    }
    void Erase() {
        delete[] m_ptData;
        m_ptData= 0;
        m_nLength = 0;
    }
    T& operator[](int nIndex) {
        assert(nIndex >= 0 && nIndex < m_nLength);
        return m_ptData[nIndex];
    }
    int GetLength() { return m_nLength; }
    friend ostream& operator<<(ostream& out, const Array<T>& n) {
    for(int i=0; i<n.m_nLength; i++) {
            if(i) it << "n";
            it << n[i];
        }
        return it;
    } 
};
#endif

2( 这是我尝试制作数组的类,以及我是如何做到的(它有动态内存分配(

class Tune {
    char* artist;
    char* song;
public:
    explicit Tune(const char* a, const char* s) {
        artist = new char [strlen(a)+1]; strcpy(artist, a);
    song = new char [strlen(s)+1]; strcpy(song, s);     
}
...
#include "Array.h"    
void main() {
    Array<Tune> tunes(5);          //Array of 5 elements
}

error C2512: 'Tune' : no appropriate default constructor available
1>          c:xxxvisual studio 2010projectsxxarray.h(26) : while
compiling class template member function 'Array<T>::Array(int)'
1>          with
1>          [
1>              T=Tune
1>          ]
1>          c:xxxvisual studio 2010projectsxxmain.cpp(10) : see reference to
class template instantiation 'Array<T>' being compiled
1>          with
1>          [
1>              T=Tune
1>          ]

3( 然后我想起来我可以用这样的东西来解决这个问题(不需要使用我的数组模板类(:

void main() {
    Tune **tunes = new Tune*[5];
    ...
}

我想知道这是解决方案吗?我如何使用我的模板数组类创建指针数组,其次(以免说我有overriden运算符<<(,我如何打印数组的一个或所有元素
整个程序非常庞大,这就是它的一部分。大部分代码都在注释中,所以这个问题是孤立的
我很困,这个项目对我来说意义重大,但我是一个缺乏经验的程序员,所以我发现很难处理这样的问题。感谢您提前提供帮助
干杯

首先,请显示完整错误消息。其次,还不清楚什么是MyType,以及它是否有默认的构造函数。

例如,如果MyType是某种算术类型,那么下面的代码将被编译而不会出错。

#include "Array.h"    
int main() {
    Array<MyType> data(5);          //Array of 5 elements
}

至少类Array有默认构造函数,尽管它没有被使用。至于类型MyType,则可以什么都不说,因为您既没有显示完整的错误消息,也没有显示MyType的定义。我建议检查MyType是否有默认的构造函数。

如果你想创建一个指针数组,那么你应该写

Array<MyType *> data(5);          

对于这个代码

void main() {
    MyType **data = new MyType*[5];
    ...
}

那么这个问题就没有什么共同点了。考虑到主应定义为具有内部返回类型

EDIT:如果在类Tune的定义中不考虑错误,那么它就没有默认的构造函数。因此,您应该决定是要创建一个类型为Tune的obects数组,还是创建一个指向类型为Tune的对象的指针数组。我已经展示了如何定义指针数组。或者为类Tune定义默认构造函数。

如何使用模板Array类创建指针数组?

如果您的类需要另一种构造元素的方法,那么您应该创建另一个构造函数,以您希望的方式初始化它,类似于std::vector的构造函数:

Array( int count, const T& value );

实现这一点的最简单方法是将m_ptData声明为双指针,并将其初始化为:

Array( int count, const T& value ) : m_ptData(new T*[count])
{
    for (int i = 0; i < count; ++i)
    {
        m_ptData[i] = new T(value);
    }
}

最好(也是最困难(的方法是使用placement-new来初始化它,这就是std::vector的方法。你可以这样使用它:

int main()
{
    Array<MyType> data(5, MyType("abc")); // Array of 5 elements,
                                          // all initialized to MyType("abc")
}

如何打印数组中的一个或全部元素?

插入器operator<<()应该用于打印整个数组,因此让它做一些类似于只打印某些元素的事情会让代码的维护人员感到有点困惑。作为替代方案,如果您希望自定义输出,可以创建流操纵器,也可以使用成员函数来计算要打印的数字。此外,您的类可以具有begin()end()函数,它们返回指向数组开头和结尾的指针,因此类的用户可以自行实现打印。在任何一种情况下,都应该使用循环来打印元素。