简体   繁体   English

内部模板化类C ++

[英]Internal templated class c++

I'm trying this little exercice from my c++ class and i've manage to do this from now. 我正在尝试从我的c ++类中学习这个小练习,并且从现在开始我就设法做到这一点。

It's not fully completed, but for now i'm facing the eternal problem of template of internal class. 它还没有完全完成,但是现在我面临着内部类模板的永恒问题。

I've seen a lot of different solution here on stack and on other websites, but still missing someting. 我在堆栈和其他网站上看到了很多不同的解决方案,但仍然缺少某些功能。 + my goal at the end is also to understand the "why". +最后,我的目标也是了解“为什么”。

It's an Iterator internal class needed to iterate to my array. 这是迭代器需要迭代到我的数组的内部类。 I've seen some exemple giving 2 differents typename for External and Iternal class and then using the typedef, but i'm not sure what's the best way to implement it. 我看到一些示例为外部类和Iternal类提供2个不同的类型名,然后使用typedef,但是我不确定实现它的最佳方法是什么。

As you can understand my Iterator class need to take the same kind of type as my Array class. 如您所了解的,我的Iterator类需要采用与Array类相同的类型。

I for sure will need to change some function signatures and add some <\\T> here and there, but for now i just need to avoid all the template traps. 我确定将需要更改一些函数签名并在此处和此处添加一些<\\ T>,但是现在我只需要避免所有模板陷阱。

using namespace std;

template <typename T>
class Iterator;

template<typename T>
class Array{

    T* _data;
    size_t _size;
    Iterator* _start;

public:

    class Iterator{
        T* content;

    public:
        explicit Iterator(T* value):content(value){}

        Iterator& operator++(){
            ++content;
            return *this;
        }

        T* operator*(){
            return content;
        }

        bool operator ==(const Iterator& o)const{
            if(content == o.content){
                return true;
            }
            return false;
        }

        bool operator !=(const Iterator& o)const{
            return !(*this == o);
        }
    };


    Array(const size_t s):_size(s){
        _data = new T[_size]();
        _start = new Iterator(_data);
    }

    Array(const Array& o):_size(o._size), _data(o._data), _start(o._start){}

    Array(const std::initializer_list<T>& list):_size(list.size()){
        auto start = list.begin();
        auto fin = list.end();

        _data = new T[_size];

        size_t index = 0;
        while(start != fin){
        _data[index++] = *start++;
        }

        _start = new Iterator(_data);
    }

    virtual ~Array(){
        cout << "~Array" << endl;
        delete[] _data;
    }

    Array<T>& operator= (const Array& o){
        if(this != &o){
            delete[] _data;

            for (size_t i = 0; i < o._size; ++i) {
                _data[i] = o._data[i];
            }
            _size = o._size;
        }

        return *this;
    }

    T& operator[](const size_t index){
        if(index < _size){
            return *_data[index];
        }
    }

    const size_t size()const{
        return _size;
    }

    Iterator begin(){
        return Iterator(_data[0]);
    }

    Iterator end(){
        return Iterator(_data[_size-1]);
    }

};

Can you please give me a clue or help me with this. 您能给我一个提示或帮助我吗?

For more here is my basic main: 有关更多信息,这是我的基本主旨:

#include "Array.h"

int main() {

    Array<string> array({"h","e","l","l","o"});

    for (Array<string>::Iterator i = array.begin(); i != array.end(); ++i)
        cout << *i << endl;

    return 0;
}

Thank you! 谢谢!

There is no template Iterator at the global scope, so this is wrong: 全局范围内没有模板Iterator ,所以这是错误的:

template <typename T>
class Iterator;

Also, Array<T>::Iterator isn't a template, it's just an inner class. 另外, Array<T>::Iterator不是模板,它只是一个内部类。 You can simply forward-declare it inside the class like this: 您可以像这样在类中简单地将其向前声明:

template<typename T>
class Array {
public:
    class Iterator;

Then there are some bugs in your code (eg end() should be 1 past the last element, you need to dereference the iterator twice and construct one from a pointer). 然后,您的代码中存在一些错误(例如, end()应该比最后一个元素晚1,需要将迭代器取消引用两次并从指针构造一个)。

Here's a fixed version: 这是固定版本:

#include <iostream>
#include <string>

using namespace std;

template<typename T>
class Array {

    T* _data;
    size_t _size;
public:
    class Iterator;
private:
    Iterator* _start;

public:

    class Iterator {
        T* content;

    public:
        explicit Iterator(T* value) :content(value) {}

        Iterator& operator++() {
            ++content;
            return *this;
        }

        T* operator*() {
            return content;
        }

        bool operator ==(const Iterator& o)const {
            if (content == o.content) {
                return true;
            }
            return false;
        }

        bool operator !=(const Iterator& o)const {
            return !(*this == o);
        }
    };


    Array(const size_t s) :_size(s) {
        _data = new T[_size]();
        _start = new Iterator(_data);
    }

    Array(const Array& o) :_size(o._size), _data(o._data), _start(o._start) {}

    Array(const std::initializer_list<T>& list) :_size(list.size()) {
        auto start = list.begin();
        auto fin = list.end();

        _data = new T[_size];

        size_t index = 0;
        while (start != fin) {
            _data[index++] = *start++;
        }

        _start = new Iterator(_data);
    }

    virtual ~Array() {
        cout << "~Array" << endl;
        delete[] _data;
    }

    Array<T>& operator= (const Array& o) {
        if (this != &o) {
            delete[] _data;

            for (size_t i = 0; i < o._size; ++i) {
                _data[i] = o._data[i];
            }
            _size = o._size;
        }

        return *this;
    }

    T& operator[](const size_t index) {
        if (index < _size) {
            return *_data[index];
        }
    }

    const size_t size()const {
        return _size;
    }

    Iterator begin() {
        return _start;
    }

    Iterator end() {
        return Iterator(_data + _size);
    }

};

int main() {
    Array<string> array({ "h","e","l","l","o" });

    for (Array<string>::Iterator i = array.begin(); i != array.end(); ++i)
        cout << **i << endl;
}

Thank you @rustyx for your help, Wasn't far from it, but realy thank you. 谢谢@rustyx的帮助,距离我们不远,但真的谢谢。

I will post my corrected and working code here in case it can help others. 我将在此处发布经过更正和正常工作的代码,以防它可以帮助他人。

#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;


template<typename T>
class Array{

T* _data;
size_t _size;

public:

    class Iterator{
        T* content;

    public:
        explicit Iterator(T* value):content(value){}

        Iterator& operator++(){
            ++content;
            return *this;
        }

        T& operator*(){
            return *content;
        }

        bool operator ==(const Iterator& o)const{
            if(content == o.content){
                return true;
            }
            return false;
        }

        bool operator !=(const Iterator& o)const{
            return !(*this == o);
        }
    };


Array(const size_t s):_size(s){
    _data = new T[_size]();
}

Array(const Array& o):_size(o._size){
    _data = new T[_size];
    for (size_t i = 0; i < o._size; ++i) {
        _data[i] = o._data[i];
    }
}

Array(const std::initializer_list<T>& list):_size(list.size()){
    auto start = list.begin();
    auto fin = list.end();

    _data = new T[_size];

    size_t index = 0;
    while(start != fin){
        _data[index++] = *start++;
    }

}

virtual ~Array(){
    cout << "~Array" << endl;
    delete[] _data;
}

Array<T>& operator= (const Array& o){
    if(this != &o){
        delete[] _data;

        for (size_t i = 0; i < o._size; ++i) {
            _data[i] = o._data[i];
        }
        _size = o._size;
    }

    return *this;
}

T& operator[](const size_t index){
    if(index < _size){
        return *_data[index];
    }
}

const size_t size()const{
    return _size;
}

Iterator begin(){
    return Iterator(_data);
}

Iterator end(){
    return Iterator(_data + _size);
}

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM