无法构造对象

时间:2014-03-10 07:56:09

标签: c++ c++11

我花了最后两个小时试图弄清楚为什么以下代码无法编译,我什么都没得到。我的代码的相关部分如下(如果需要,完整代码在这里http://pastebin.com/z78iy3AA(hpp)和http://pastebin.com/5mC6TwET(cpp):

/*
File: du1simd.hpp
*/

#include <iterator>
#include <cstdint>
#include <new>

template< typename T, typename S>
class simd_vector;

template<typename T, typename S>
class simd_vector_iterator
    : public std::iterator<std::random_access_iterator_tag, T> {
    typedef
        typename simd_vector<T, S>::iterator
        self_type;
    typedef
        typename std::iterator<std::random_access_iterator_tag, T>::pointer
        pointer;
    typedef
        typename std::iterator<std::random_access_iterator_tag, T>::reference
        reference;
    typedef
        typename std::iterator<std::random_access_iterator_tag, T>::value_type
        value_type;
    typedef
        typename
            std::iterator<std::random_access_iterator_tag, T>::difference_type
        difference_type;
 public:
    simd_vector_iterator()
        : ptr_(nullptr) {}
    explicit simd_vector_iterator(pointer ptr)
        : ptr_(ptr) {}
    explicit simd_vector_iterator(const self_type & other)
        : ptr_(other.ptr_) {}
    explicit simd_vector_iterator(self_type && other)
        : ptr_(other.ptr_) {
        other.ptr_ = nullptr;
    }

    /*
    Operators for random access iterator are here
    */

 private:
    pointer ptr_;
};

template< typename T, typename S>
simd_vector_iterator< T, S>
operator+(std::ptrdiff_t n, simd_vector_iterator< T, S> a) {
    return a += n;
}

template< typename T, typename S>
simd_vector_iterator< T, S>
operator-(std::ptrdiff_t n, simd_vector_iterator< T, S> a) {
    return a -= n;
}

template<typename T, typename S>
class simd_vector {
 public:
    typedef simd_vector_iterator<T, S> iterator;

    explicit simd_vector(std::size_t s) throw(std::bad_alloc) {
        pointer_to_allocated_memory_ =
            ::operator new(sizeof(T) * s + sizeof(S));

        uintptr_t pointer_to_allocated_memory =
            (uintptr_t) pointer_to_allocated_memory_;
        uintptr_t pointer_to_alligned_memory =
            pointer_to_allocated_memory + sizeof(S)
            - pointer_to_allocated_memory % sizeof(S);
        data_s_ = reinterpret_cast<S*>(pointer_to_alligned_memory);
        data_t_ = reinterpret_cast<T*>(pointer_to_alligned_memory);
    }

    ~simd_vector() {
        ::operator delete(pointer_to_allocated_memory_);
    }

    iterator begin() {
        iterator i(data_t_);
        return i;
    }

    iterator end() {
        /*...*/
    }

    std::size_t size() {
        return count_;
    }

    /*...*/

 private:
    void * pointer_to_allocated_memory_;
    std::size_t count_;
    S * data_s_;
    T * data_t_;
};

和测试代码:

/*
File: du1simd.cpp
*/

#include "du1simd.hpp"

typedef int T;
struct S {
    T a, b, c;
};

int main(int argc, char ** argv) {
    simd_vector<T, S> sv(5);
    *(sv.begin()) = 1;
    return 0;
}

当我尝试编译时,我看到以下错误:

In file included from du1simd.cpp:8:0:
du1simd.hpp: In instantiation of ‘simd_vector<T, S>::iterator simd_vector<T, S>::begin() [with T = int; S = S; simd_vector<T, S>::iterator = simd_vector_iterator<int, S>]’:
du1simd.cpp:17:16:   required from here
du1simd.hpp:192:16: error: no matching function for call to ‘simd_vector_iterator<int, S>::simd_vector_iterator(simd_vector<int, S>::iterator&)’
         return i;
                ^
du1simd.hpp:192:16: note: candidate is:
du1simd.hpp:59:5: note: simd_vector_iterator<T, S>::simd_vector_iterator() [with T = int; S = S]
     simd_vector_iterator()
     ^
du1simd.hpp:59:5: note:   candidate expects 0 arguments, 1 provided

我认为我没有适当的构造函数,当我看到simd_vector_iterator的代码时,我看到(IMHO)匹配签名的构造函数:/

有人会这么善意帮助我吗?

披露:是的,这是一个家庭作业的分配。不,我不希望有人为我完成它。我只是需要帮助这个“不会编译”的情况。

2 个答案:

答案 0 :(得分:4)

VS2013抛出以下错误,其中包含更好的消息:

  

错误C2558:类'simd_vector_iterator':没有复制构造函数   可用或复制构造函数声明为'explicit'

删除复制构造函数中的explicit关键字应该足够了:

simd_vector_iterator(const self_type & other) : ptr_(other.ptr_) {}

答案 1 :(得分:0)

您似乎在begin()中正确构建了迭代器,但是当您返回i时,需要制作副本。因此,编译器想要使用不存在的复制构造函数(simd_vector_iterator(const simd_vector_iterator&)

解决方案可以是添加默认的复制构造函数:

simd_vector_iterator(const simd_vector_iterator&) = default;