为什么我的自定义 const_iterator end() 函数在使用 gtest 时无法编译?

问题描述 投票:0回答:1

我正在尝试学习 C++ 标准库的工作原理及其数据结构,并且我正在实现一个数组和一些迭代器,只是为了学习目的。我正在使用 Gtest/gmock 与 VisualStudio 2022 进行一些 TDD,目前我拥有

array
const_iterator
iterator
类及其相应的测试,一切正常。

但是当我在数组结构中添加迭代器函数(begin、end、cbegin、cend)的代码时,gtest 无法编译,并抛出与

const_iterator end() const
函数相对应的错误。

Build started at 6:45 PM...
1>------ Build started: Project: CppStd Test, Configuration: Debug x64 ------
1>array_test.cpp
1>C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25): error C2440: '<function-style-cast>': cannot convert from 'const int *' to 'cppstd::const_iterator<T>'
1>C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25): error C2440:         with
1>C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25): error C2440:         [
1>C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25): error C2440:             T=int
1>C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25): error C2440:         ]
1>(compiling source file 'array_test.cpp')
1>C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25):
1>'cppstd::const_iterator<T>::const_iterator': no overloaded function could convert all the argument types
1>        with
1>        [
1>            T=int
1>        ]
1>  C:\Users\PC\Visual Studio\CppStd\CppSTD\const_iterator.h(130,1):
1>  could be 'cppstd::const_iterator<T>::const_iterator(cppstd::const_iterator<T> &&)'
1>        with
1>        [
1>            T=int
1>        ]
1>      C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25):
1>      'cppstd::const_iterator<T>::const_iterator(cppstd::const_iterator<T> &&)': cannot convert argument 1 from 'const int *' to 'cppstd::const_iterator<T> &&'
1>        with
1>        [
1>            T=int
1>        ]
1>          C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,38):
1>          Reason: cannot convert from 'const int *' to 'cppstd::const_iterator<T>'
1>        with
1>        [
1>            T=int
1>        ]
1>  C:\Users\PC\Visual Studio\CppStd\CppSTD\const_iterator.h(130,1):
1>  or       'cppstd::const_iterator<T>::const_iterator(const cppstd::const_iterator<T> &)'
1>        with
1>        [
1>            T=int
1>        ]
1>      C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25):
1>      'cppstd::const_iterator<T>::const_iterator(const cppstd::const_iterator<T> &)': cannot convert argument 1 from 'const int *' to 'const cppstd::const_iterator<T> &'
1>        with
1>        [
1>            T=int
1>        ]
1>          C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,38):
1>          Reason: cannot convert from 'const int *' to 'const cppstd::const_iterator<T>'
1>        with
1>        [
1>            T=int
1>        ]
1>  C:\Users\PC\Visual Studio\CppStd\CppSTD\const_iterator.h(31,3):
1>  or       'cppstd::const_iterator<T>::const_iterator(int *) noexcept'
1>        with
1>        [
1>            T=int
1>        ]
1>      C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25):
1>      'cppstd::const_iterator<T>::const_iterator(int *) noexcept': cannot convert argument 1 from 'const int *' to 'int *'
1>        with
1>        [
1>            T=int
1>        ]
1>          C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,38):
1>          Conversion loses qualifiers
1>  C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25):
1>  while trying to match the argument list '(const int *)'
1>C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(55,25):
1>the template instantiation context (the oldest one first) is
1>  C:\Users\PC\Visual Studio\CppStd\CppStd Test\array_test.cpp(64,30):
1>  see reference to class template instantiation 'cppstd::array<int,3>' being compiled
1>  C:\Users\PC\Visual Studio\CppStd\CppSTD\array.h(53,42):
1>  while compiling class template member function 'cppstd::const_iterator<T> cppstd::array<T,3>::end(void) noexcept const'
1>        with
1>        [
1>            T=int
1>        ]
1>      C:\Users\PC\Visual Studio\CppStd\packages\gmock.1.11.0\lib\native\include\gtest\gtest-printers.h(136,24):
1>      see the first reference to 'cppstd::array<int,3>::end' in 'testing::internal::ContainerPrinter::PrintValue'
1>const_iterator_test.cpp
1>iterator_test.cpp
1>Generating Code...
1>Done building project "CppStd Test.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 6:45 PM and took 11.959 seconds ==========

我的失败编译实现如下:

template<typename T, std::size_t Size>
struct array
{
    using value_type = T;
    using size_type = std::size_t;
    using difference_type = std::ptrdiff_t;
    using reference = value_type&;
    using const_reference = const value_type&;
    using pointer = value_type*;
    using const_pointer = const value_type*;
    using iterator = cppstd::iterator<T>;
    using const_iterator = cppstd::const_iterator<T>;
    
    value_type m_array[Size];

    [[nodiscard]] constexpr iterator begin() noexcept
    {
        return iterator(&m_array[0]);
    }

    [[nodiscard]] constexpr iterator end() noexcept
    {
        return iterator(&m_array[0] + Size);
    }

    [[nodiscard]] constexpr const_iterator begin() const noexcept
    {
        return const_iterator(&m_array[0]);
    }

    [[nodiscard]] constexpr const_iterator end() const noexcept
    {
        // HERE IS WHERE I GET THE ERROR
        return const_iterator(&m_array[0] + Size);
    }
};


template<typename T>
class const_iterator
{
public:
    using iterator_category = std::random_access_iterator_tag;
    using value_type = T;
    using size_type = std::size_t;
    using difference_type = std::ptrdiff_t;
    using reference = value_type&;
    using pointer = value_type*;
    using const_reference = const value_type&;
    using const_pointer = const value_type*;

    const_iterator() = default;

    explicit const_iterator(pointer ptr) noexcept : m_pointer{ ptr } {}
};

如果我将函数名称更改为其他任何内容,错误就会停止,并且即使我没有任何引用迭代器或数组类的测试或类型,错误也会继续出现。

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include "../CppSTD/const_iterator.h"

using namespace ::testing;

class const_iterator_test : public Test
{
public:
    void SetUp() override
    {
        ptr_int = new int[5];
        for (int i{ 0 }; i < 5; ++i)
            ptr_int[i] = i + 1;
    }

    void TearDown() override
    {
        delete ptr_int;
        ptr_int = nullptr;
    }

    int* ptr_int{ nullptr };

private:

};

也许是一些非常明显但我没有看到的东西。这很奇怪,因为如果我在没有 gtest 的情况下编译,一切都会正常运行。 我在这里留下数组和迭代器类的完整定义:完整代码

提前谢谢您。

c++ googletest const-iterator
1个回答
0
投票

class const_iterator
中,构造函数

explicit const_iterator(pointer ptr) noexcept : m_pointer{ ptr } {}

应该是

explicit const_iterator(const_pointer ptr) noexcept : m_pointer{ ptr } {}
© www.soinside.com 2019 - 2024. All rights reserved.