简体   繁体   English

重载中的C ++编译错误

[英]C++ Compilation error in overloading

The following code compiles fine. 以下代码可以正常编译。

#include <iostream>
#include <vector>
using namespace std;

class MyClass
{
public:
    MyClass()
    {
        x.resize(2);
        x[0] = 10;
        x[1] = 100;
    }
    std::vector<int> getValue()
    {
        return x;
    }
    const std::vector<int>& getValue() const
    {
        return x;
    }
private:
       std::vector<int> x;
};


int main()
{

    MyClass m;
    std::vector<int> y = m.getValue();
    for(int i=0; i<y.size(); i++)
    {
        std::cout<<y[i]<<std::endl;
    }

    const std::vector<int>& z = m.getValue();
    for(int i=0; i<z.size(); i++)
    {
        std::cout<<z[i]<<std::endl;
    }
    return 0;
}

However, when I change the "std::vector getValue()" to a more correct version (since the function is supposed to change the object) by adding "const" (std::vector getValue() const) it gives the following compilation error. 但是,当我通过添加“ const”(std :: vector getValue()const)将“ std :: vector getValue()”更改为更正确的版本时(由于该功能应该更改对象),它给出了以下内容编译错误。

error: 'const std::vector<int>& MyClass::getValue() const' cannot be overloaded const std::vector<int>& getValue() const

Why is it so? 为什么会这样呢?

I have used "gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)" 我使用了“ gcc版本4.8.4(Ubuntu 4.8.4-2ubuntu1〜14.04.3)”

You can't define two functions with the same name, which differ only in return type. 您不能定义两个具有相同名称的函数,这些函数仅在返回类型上有所不同。 So define function with different name, for example: 因此,用不同的名称定义函数,例如:

std::vector<int> getValueCopy() const;

By adding const to the first function you render calls to getValue ambiguous: What is the difference between those 2 functions: 通过将const添加到第一个函数中,您可以使对getValue调用变得模棱两可:这两个函数之间有什么区别:

std::vector<int> getValue() const;        // 1
const std::vector<int>& getValue() const; // 2

Well, they are the same, except for the return value, but wait! 好吧,除了返回值外,它们都是一样的,但是请稍等! You can't overload based on the return type in C++! 您不能基于C ++中的返回类型进行重载! It wouldn't make sense, most calls would be ambiguous: 这没有任何意义,大多数通话会模棱两可:

std::vector<int> y = m.getValue(); // which one? It can be 1, it can be 2 (std::vector<int>
                                   // is not a better match than const std::vector<int>&)

const std::vector<int>& z = m.getValue(); // same as above

m.getValue(); // which one?

But also, what is supposed to be the difference between the two? 但是,两者之间的区别应该是什么?

The first one is 100% safe, while the second one is not: one could store a reference to x , and it becomes a dangling reference if the object gets destroyed. 第一个是100%安全的,而第二个则不是:一个可以存储对x引用,如果该对象被破坏,它将成为悬挂的引用。 And so I would say that you get rid of the second one, if possible. 所以我想说,如果可能的话,您可以摆脱第二个。

your problem is that you didn't understand functions overloading concept 您的问题是您不了解函数重载概念

when you overload function The definition of the function must differ from each other by the types of argument or the number of arguments in the argument list. 当重载函数时函数的定义必须因参数类型或参数列表中参数的数量而彼此不同。

You can not overload function declarations that differ only by return type. 您不能重载仅在返回类型上有所不同的函数声明。

in your functions : 在您的职能中:

std::vector<int> getValue() const 

const std::vector<int>& getValue() const

it only differ in return type so it will be not considered as overloaded functions 它仅在返回类型上有所不同,因此将不被视为重载函数

best way to correct your error is to change your second function name to getValuev2() 纠正错误的最佳方法是将第二个函数名称更改为getValuev2()

or change arguments of one of the functions. 或更改其中一个功能的参数。

you can read more about overloading in C++ : https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm 您可以阅读有关C ++中的重载的更多信息: https : //www.tutorialspoint.com/cplusplus/cpp_overloading.htm

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

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