c++ - 指针的模板数组类

标签 c++ templates


我遇到了一个问题,我已经尝试解决了一段时间,但就是做不到。这是场景:
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:\x\x\x\visual studio 2010\projects\x\x\array.h(26) : while
compiling class template member function 'Array<T>::Array(int)'
1>          with
1>          [
1>              T=Tune
1>          ]
1>          c:\x\x\x\visual studio 2010\projects\x\x\main.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];
    ...
}

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

最佳答案

首先请出示完整的错误信息。其次不清楚什么是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];
    ...
}

那么它与问题没有任何共同之处。考虑到 main 应定义为具有返回类型 int。

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

关于c++ - 指针的模板数组类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24109990/

相关文章:

c++ - 如何在另一个结构中初始化结构数组?

c++ - 增量数组

C++命令行参数验证

c++ - 您如何比较两个未知数以查看它们在特例模板中是否相等?

c++ - 将所有程序输出写入 C++ 中的 txt 文件

c++ - 矩阵加法 : Ambiguous overload for ‘operator+’

templates - Play框架模板For Loop

C++ 从 DLL 实例化模板类

C++ - typedef/用于函数模板

c++ - 如何使用定界符将 vector<char> 拆分为字符串