重载函数仅因返回类型而异

时间:2017-02-08 04:33:18

标签: c++ visual-studio

对于这个错误是什么感到困惑,visual studio并不是最具描述性的,因为它实际上是错误的。有人可以帮忙吗?

重载函数的区别仅在于'float& vec3 :: operator [](unsigned int)的返回类型'

/*
 * vec3.cpp
 */
#include <iostream>
#include "vec3.h"

vec3::vec3() {
    data[0] = 0;
    data[1] = 0;
    data[2] = 0;
}
vec3::vec3(float x, float y, float z) {
    data[0] = x;
    data[1] = y;
    data[2] = z;
}
float vec3::operator[](unsigned int index) {             ///Thrown on these 2 functions
    return data[index];
}
const float& vec3::operator[](unsigned int index) const {
    return data[index];
}


/*
 *  vec3.h
 */

#ifndef VEC3_H
#define VEC3_H

class vec3{
private:
    float data[3];
public:
    ///----------------------------------------------------------------------
    /// Constructors
    ///----------------------------------------------------------------------
    vec3();
    vec3(float x, float y, float z);

    ///----------------------------------------------------------------------
    /// Getters/Setters
    ///----------------------------------------------------------------------       
    /// Returns the value at index
    float operator[](unsigned int index) const;

    /// Returns a reference to the value at index
    float& operator[](unsigned int index);

1 个答案:

答案 0 :(得分:1)

您不能通过仅更改其返回类型来重载方法。区别必须是方法描述中的参数类型。