gpt4 book ai didi

c++ - 虚方法的返回值

转载 作者:太空宇宙 更新时间:2023-11-04 15:00:50 25 4
gpt4 key购买 nike

我继承了一个在 clang 和 GCC 上完美构建的应用程序。现在,我正在尝试将其移植到 Windows 上。当我尝试使用 visual studio 2010 构建它时出现错误,我尝试使用以下示例进行上下文化。

考虑 Interface 类:

class Interface
{
public:

virtual ~Interface(){}

virtual void someMethod() = 0;
virtual QVector<int >& getSomething(){}
};

someMethod 是纯虚拟的,getSomething 是虚拟的。

然后是两个派生类Derived

class Derived : public Interface
{
public:
Derived(){ cout << "derived constructor";}

~Derived(){ cout << "derived destructor";}

virtual void someMethod() override { cout << "derived someMethod";}

};

它只实现纯虚方法,DerivedB:

class DerivedB : public Interface
{
public:
DerivedB(){ cout << "derivedB constructor";}
~DerivedB(){ cout << "derivedB destructor";}
virtual void someMethod() override { cout << "derivedB someMethod";}
virtual QVector<int >& getSomething(){ return myComputedVector; }
};

它同时实现了纯虚方法和虚方法。

总而言之,我有一些实现 getSomething() 的类和所有实现 someMethod() 的类。

如果我尝试使用 visual studio 构建此代码,我会收到以下错误:

error C4716: 'Interface::getSomething' : must return a value

我了解编译器,但我不知道如何处理这种情况。如果有人可以向我解释或给我一个简单的例子来说明如何管理它,我会非常有帮助!!

最佳答案

函数

virtual QVector<int >& getSomething(){}

需要返回对 QVector<int> 的引用.您通过此函数的实现 提供的空函数体并没有这样做。您有用的编译器正在通知您。

关于c++ - 虚方法的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com