首页 > 解决方案 > How to pass a reference of template reference into class method?

问题描述

I have been working on this problem for a few days and can not find a solution to my problem online. The problem given to me was to create an insert and erase method for a vector class. The method headers were given to me as iterator insert(iterator pos, const Object& x); and iterator erase(iterator pos);. When I made a vector class everything compiled without errors other than never being used. the code is as follows.

template<typename iterator, typename Object> class Vector
{
public:
    iterator insert(iterator pos, const Object& x)
    {
        element[pos] = x;
    }
    iterator erase(iterator pos)
    {
        element[pos] = '\0';
    }
private:
    iterator pos;
    Object element;
};

The main function looks like this:

int main()
{
    Vector<int, class Object> myVec;
    int i = 9;
    myVec.insert(0,i);
}

After I finished that the only error occors on the line Object element line. With the error message "Field has incomplete type 'Object'". I apologize if this is very simple to fix but I am new to iterators and vectors so to have to create the methods myself is confusing. All help is appreciated, thank you.

标签: c++classmethodsdata-structuresreference

解决方案


推荐阅读