简体   繁体   English

错误C4716:必须返回一个值,该值由实际上确实返回值的函数抛出

[英]error C4716: must return a value, thrown by function that actually does return a value

My problem is quite simple, yet I fail to understand the cause of it and no similiar posting has turned up even after extensive research so here it is: 我的问题很简单,但我不了解其原因,即使经过大量研究,也没有类似的帖子出现,所以这里是:

I have the following operator overload: 我有以下运算符重载:

template <class T, size_t size>
    inline Vector<T, size> operator + (Vector<T, size> &a, Vector<T, size> &b) {
        Vector<T, size> result;
        for (auto i = 0; i < size; ++i) {
            result[i] = a[i] + b[i];
        }
        return result;
    }

Obiously there's only a single code path, and this path also returns a value, but compiling under Visual Studio 2013, I get an error C4716, stating that the function instantiated by the compiler 'must return a value'. 令人讨厌的是,只有一个代码路径,并且该路径还返回一个值,但是在Visual Studio 2013下编译时,出现错误C4716,指出由编译器实例化的函数“必须返回一个值”。 I get this error for all instantiations I've tried so far. 对于到目前为止我尝试过的所有实例化,我都会收到此错误。 I also get this error for every other operator overload in the same header, all of which are structured similarly to the snippet above. 对于同一标头中的所有其他运算符重载,我也都会收到此错误,所有这些标头的结构都与上面的代码段相似。 Am I missing something obvious here? 我在这里错过明显的东西吗?

EDIT: This is the templated vector class definition: 编辑:这是模板化的矢量类定义:

template <class T, size_t size>
    struct Vector {
        explicit Vector(T value = static_cast<T>(0)) {
            for (auto i = 0; i < size; ++i) {
                _data[i] = value;
            }
        }
        explicit Vector(const Vector &other) {
            for (auto i = 0; i < size; ++i) {
                _data[i] = other._data[i];
            }
        }
        explicit Vector(T values[size]) {
            for (auto i = 0; i < size; ++i) {
                _data[i] = values[i];
            }
        }
        T & operator = (const Vector &other) {
            for (auto i = 0; i < size; ++i) {
                _data[i] = other._data[i];
            }
            return *this;
        }

        T & operator [] (size_t index) {
            return _data[index];
        }

        T _data[size];
    };

The problem was solved for my by making the copy constructor of Vector non-explicit. 通过使Vector的副本构造函数非显式,为我解决了该问题。

To describe how I came to this conclusion, I went and specialized the operator function for Vector: 为了描述我如何得出这个结论,我专门研究了Vector的运算符功能:

template <>
    inline Vector<int, 1> operator + (Vector<int, 1> &a, Vector<int, 1> &b) {
        Vector<int, 1> result;
        return result;
    }

Visual Studio came up with an error stating that no suitable copy constructor exists for the return value, which was resolved by removing the explicit keyword from the copy constructor. Visual Studio出现一个错误,指出不存在适合返回值的副本构造函数,此错误可以通过从副本构造函数中删除显式关键字来解决。 This would have been obvious to me if I'd properly understood how explicit copy constructors work. 如果我正确理解了显式副本构造函数的工作原理,这对我来说是显而易见的。

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

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