【问题标题】:template programming caused several errors in xutility模板编程导致 xutility 出现几个错误
【发布时间】:2016-02-08 09:03:29
【问题描述】:

我最近尝试自己编写一个 Tiny STL。但是,在完成向量容器的实现后,它似乎失败了。我在“xutility”类中发生了一些错误。恐怕它可能来自模板代码,但我不知道如何处理它。有什么建议吗?

类定义和成员函数声明在vector.h中定义

// vector.h
#ifndef _VECTOR_H_
#define _VECTOR_H_

#include <initializer_list>
#include <cstddef>
#include <memory>

#include "../Declaration/iterator.h"
#include "../Declaration/type_traits.h"
namespace MySTL{
    template<typename T>
    class vector{
    public:
        typedef std::size_t                     size_type;
        typedef T                               value_type;
        typedef value_type&                     reference;
        typedef const value_type&               const_reference;
        typedef value_type*                     iterator;
        typedef const value_type*               const_iterator;
        typedef value_type*                     pointer;
        typedef const value_type*               const_pointer;
        typedef std::reverse_iterator<T*>       reverse_iterator;
        typedef std::reverse_iterator<const T*> const_reverse_iterator;
        typedef std::ptrdiff_t                  difference_type;
    public:
        vector() : elements(nullptr), first_free(nullptr), cap(nullptr) {}
        vector(const vector &); 
        vector(vector &&); //noexcept;
        // explicit 
        vector(const size_type n);  
        vector(const size_type n, const value_type &val);
        vector(std::initializer_list<value_type> il);
        template<typename InputIterator>
        vector(InputIterator first, InputIterator second);

        vector& operator=(const vector &rhs);
        vector& operator=(std::initializer_list<value_type> il);
        vector& operator=(vector &&rhs); //noexcept;
        ~vector();

        // TOO MANY CODES TO DISPLAY 
        // SO I HAVE TO OMIT SOME OF THEM
        // I'M SURE THEY ARE INNOCENT
        // :-)

    private:
        // ...
        std::allocator<T> alloc;
        T* elements;    // head pointer
        T* first_free;  // the pointer that point to the first free element in the array
        T* cap;         // end of storage
    };
}

#include "../Implementation/vector_impl.h"  // inclusion compilation model

#endif // _VECTOR_H_

而成员函数的实现在vector_impl.h中定义

// vector_impl.h
#ifndef _VECTOR_IMPL_H_
#define _VECTOR_IMPL_H_

#include <utility>
#include <memory>
#include <algorithm>
#include <stdexcept>

#include "../Declaration/vector.h"

using std::uninitialized_fill_n;
using std::uninitialized_copy;

namespace MySTL
{
    template <typename T>
    vector<T>::vector(const vector &s)
    {
        auto newdata = alloc_n_copy(s.begin(), s.end());
        elements = newdata.first;
        first_free = cap = newdata.second;
    }


    template <typename T>
    vector<T>::vector(vector &&s) //noexcept
        : elements(s.elements), first_free(s.first_free), cap(s.cap)
    {
        s.elements = s.first_free = s.cap = nullptr;
    }

    // I'VE TO OMIT MAJORITY OF FUNCTION IMPLEMENTATIONS BECAUSE THEY ARE PROLIXITY
    // YOU CAN FIND IT THROUGH THE QUICK LINKS MENTIONED BEFORE. :-)

}
#endif

然后我写了一个测试用例,用main函数调用,报错如下:

error   13  error C2868: 'std::iterator_traits<_InIt>::value_type': illegal syntax for using-declaration; expected qualified-name   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   28  error C2868: 'std::iterator_traits<_InIt>::reference': illegal syntax for using-declaration; expected qualified-name    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   23  error C2868: 'std::iterator_traits<_InIt>::pointer': illegal syntax for using-declaration; expected qualified-name  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   8   error C2868: 'std::iterator_traits<_InIt>::iterator_category': illegal syntax for using-declaration; expected qualified-name    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   18  error C2868: 'std::iterator_traits<_InIt>::difference_type': illegal syntax for using-declaration; expected qualified-name  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL
error   4   error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   9   error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   14  error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL
error   19  error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   24  error C2825: '_Iter': must be a class or namespace when followed by '::'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   30  error C2665: 'std::_Uninitialized_copy0': none of the 2 overloads could convert all the argument types  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory  337 1   MySTL
error   29  error C2665: 'std::_Debug_range2': none of the 2 overloads could convert all the argument types d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 584 1   MySTL
error   12  error C2602: 'std::iterator_traits<_InIt>::value_type' is not a member of a base class of 'std::iterator_traits<_InIt>' d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   27  error C2602: 'std::iterator_traits<_InIt>::reference' is not a member of a base class of 'std::iterator_traits<_InIt>'  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   22  error C2602: 'std::iterator_traits<_InIt>::pointer' is not a member of a base class of 'std::iterator_traits<_InIt>'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   7   error C2602: 'std::iterator_traits<_InIt>::iterator_category' is not a member of a base class of 'std::iterator_traits<_InIt>'  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   17  error C2602: 'std::iterator_traits<_InIt>::difference_type' is not a member of a base class of 'std::iterator_traits<_InIt>'    d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL
error   11  error C2146: syntax error : missing ';' before identifier 'value_type'  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   26  error C2146: syntax error : missing ';' before identifier 'reference'   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   21  error C2146: syntax error : missing ';' before identifier 'pointer' d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   6   error C2146: syntax error : missing ';' before identifier 'iterator_category'   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   16  error C2146: syntax error : missing ';' before identifier 'difference_type' d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL
error   10  error C2039: 'value_type': is not a member of '`global namespace''  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 372 1   MySTL
error   25  error C2039: 'reference': is not a member of '`global namespace''   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 376 1   MySTL
error   20  error C2039: 'pointer': is not a member of '`global namespace'' d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 375 1   MySTL
error   5   error C2039: 'iterator_category': is not a member of '`global namespace''   d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 371 1   MySTL
error   15  error C2039: 'difference_type': is not a member of `global namespace''  d:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 373 1   MySTL

有没有人可以帮助我?

【问题讨论】:

  • 源码可以从我的github获取
  • 与您的问题无关,但不要使用符号_VECTOR_IMPL_H_,前导下划线后跟大写字母保留用于“实现”(编译器/标准库)。除非您真正尝试创建一个完整的标准库,否则不要使用这样的名称。 Reference.
  • @JoachimPileborg:我终于得出结论,这是一场失败的战斗。这些名字 BEACUSE 有一种疯狂的吸引力,它们是保留的。
  • “我最近尝试自己编写一个 Tiny STL” - 我希望这是出于学术目的,而不是用于生产代码。
  • @JoachimPileborg 非常感谢,我会努力解决的。 :-)

标签: c++ templates c++11 visual-c++ vector


【解决方案1】:

我相信你在这里遇到了麻烦:

using std::uninitialized_fill_n;
using std::uninitialized_copy;

因为这些算法(在错误消息中 xmemory 337 周围的行中)包含验证调用中使用的迭代器(“检查迭代器”)的代码。您的 iterator 作为指针不适合原始库的代码。

您可能希望自己实现这些函数作为一种解决方法(但也请注意,真正的向量实现必须使用分配器在未初始化的内存中构造)。

【讨论】:

  • 欣赏这一点....那么接下来我该怎么做?在函数调用前使用类型转换?还是自己实现这些功能?请给我更多提示,因为我是 C++ 编程的新手。非常感谢:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
  • 2018-12-04
相关资源
最近更新 更多