函数中没有返回语句返回非空

时间:2022-09-10 23:55:58

Im new at coding and I get "error:no return statement in function returning non-void" for this code:

我是编码新手,我得到了“错误:函数中没有返回语句返回非空”的代码:

template <class T>
T Stack<T>::pop()
{
  `myStack.pop_front();
}
template <class T>
T Stack<T>::peek() const
{
  myStack.front();
}

any ideas of what I am doing wrong? thanks!

你知道我做错了什么吗?谢谢!

6 个解决方案

#1


3  

The function signature:

函数签名:

template <class T> T Stack<T>::pop()

tells the compiler that your function will return a type T however your function actually does not return any value and hence the compiler gives you the waring of a possible silly mistake on your part.

告诉编译器您的函数将返回类型T,但是您的函数实际上不会返回任何值,因此编译器会为您提供一个可能的愚蠢错误的警告。

So you in your case you need to make sure that the statements,
myStack.pop_front(); & myStack.front(); actually return a type T.

你需要确保语句myStack.pop_front();& myStack.front();实际上返回类型T。

Assuming you are using some standard library container, pop_front(); just removes the first element in the container it does not return anything.

假设您正在使用某个标准库容器pop_front();只删除容器中的第一个元素,它不会返回任何东西。

#2


1  

You must add the return keyword:

您必须添加return关键字:

template <class T>
T Stack<T>::pop()
{
  return myStack.pop_front();
}
template <class T>
T Stack<T>::peek() const
{
  return myStack.front();
}

or something similar, depending on your imlementation of stack.

或者类似的东西,取决于你的堆栈。

#3


0  

This will work:

这将工作:

 template <class T>
    void Stack<T>::pop()
    {
      `myStack.pop_front();
    }
    template <class T>
    void Stack<T>::peek() const
    {
      myStack.front();
    }

Or:

或者:

template <class T>
T Stack<T>::pop()
{
  `myStack.pop_front();
  // return something of type T
}
template <class T>
T Stack<T>::peek() const
{
  myStack.front();
  //return something of type T
}

#4


0  

Your signature asks you to return an object of type T.

签名要求返回类型为T的对象。

template <class T> T Stack<T>::pop()

To break this down a bit template <class T> means that for each class that you put in for T another copy of the code will be generated. For example if you did

要将其分解为一个位模板 意味着,对于您为T输入的每个类,将生成另一个代码副本。举个例子

Stack intStack; Stack doubleStack;

堆栈intStack;堆栈doubleStack;

The compiler would generate two copies of you Stack class: one specialised for int and another for double.

编译器将生成两个堆栈类的副本:一个用于int,另一个用于double。

So when you write

所以当你写

template <class T>
T Stack<T>::pop()
{
  myStack.pop_front();
}

you need to return an object of the specialised type. pop_front() only removes the first element. You need to access it remove the first item then return it.

您需要返回专门化类型的对象。pop_front()只删除第一个元素。您需要访问它,删除第一个项目,然后返回它。

This might not be the best way of doing it but to illustrate my point

这也许不是最好的方法,但可以说明我的观点

template <class T>
T Stack<T>::pop()
{
  T tmp = myStack.front();
  myStack.pop_front();
  return tmp;
}

#5


0  

As you signature indicate that function is returning type T, you should change your code to code like following for both function.

当您的签名表明函数正在返回类型T时,您应该将代码更改为如下两种函数的代码。

template <class T>
T Stack<T>::peek()
{
  return myStack.front();
}

#6


0  

While the shortviewed solution is to return a value, it is better if a pop() is of type void.

虽然简短的解决方案是返回一个值,但是如果pop()类型为void,那就更好了。

template <typename T>
void Stack<T>::pop() {
    ...
}

The reason is that you cannot write an exception safe pop()-function that returns something:

原因是您不能编写异常安全pop()函数来返回:

template <typename T>
T Stack<T>::pop() {
    T foo = stor_.top(); // May throw.
    stor_.pop(); // This line shall never throw.
    return foo;  // May throw.
}

The second line (with the pop()) should never throw something, because destructors shall not throw. So it is sane to assume that line is nothrow and will always succeed (except when you pop an empty container).

第二行(与pop()))不应该抛出什么东西,因为析构函数不应该抛出。因此,假设这一行是nothrow,并且总是会成功(除非你打开一个空容器)是合理的。

The first and third line may throw because data is being copied.

第一行和第三行可能会抛出,因为数据正在被复制。

Now imagine that

现在想象一下,

    T foo = stor_.top(); // May throw.
    stor_.pop(); // This line shall never throw.

run flawlessly: You have a value ready to be returned, and you have successfully changed the state of the holding object.

无瑕疵地运行:您有一个值准备返回,并且您已经成功地更改了保持对象的状态。

But then

但后来

    return foo;  // May throw.

bang it throws an exception. What happened:

它会抛出一个异常。事情发生的经过:

  • The function did not succeed, the caller got nothing except an exception
  • 函数没有成功,调用者除了一个异常之外什么都没有
  • Still yet, the state of the object has been modified.
  • 但是,对象的状态已经被修改了。
  • And noone has an up to date copy of the lost object anymore which typically contains critical business data that the boss phoned over seconds ago before having a fatal accident
  • 而且noone已经有了一个最新的丢失对象的副本,它通常包含了老板在发生致命事故前几秒钟就打来的关键业务数据。

This is contradictory to Abrahams Guarantees:

这与亚伯拉罕的保证相矛盾:

  • The no-throw guarantee: that the operation will not throw an exception.

    无抛出保证:操作不会抛出异常。

  • The strong guarantee: that the operation has either completed successfully or thrown an exception, leaving the program state exactly as it was before the operation started.

    强大的保证:操作成功完成或抛出异常,使程序状态与操作开始前完全相同。

  • The basic guarantee: that the invariants of the component are preserved, and no resources are leaked. Often referred to as the weak guarantee, because following an exception the system is left in a safe, but unknown, state.

    基本保证:保留组件的不变量,不泄漏任何资源。通常被称为弱担保,因为在出现异常后,系统处于安全但未知的状态。

And with a pop() that returns a value, you cannot guarantee that no information is lost.

对于返回值的pop(),不能保证不丢失任何信息。

See also GotW #8 and GotW #82

也见第8号和第82号

#1


3  

The function signature:

函数签名:

template <class T> T Stack<T>::pop()

tells the compiler that your function will return a type T however your function actually does not return any value and hence the compiler gives you the waring of a possible silly mistake on your part.

告诉编译器您的函数将返回类型T,但是您的函数实际上不会返回任何值,因此编译器会为您提供一个可能的愚蠢错误的警告。

So you in your case you need to make sure that the statements,
myStack.pop_front(); & myStack.front(); actually return a type T.

你需要确保语句myStack.pop_front();& myStack.front();实际上返回类型T。

Assuming you are using some standard library container, pop_front(); just removes the first element in the container it does not return anything.

假设您正在使用某个标准库容器pop_front();只删除容器中的第一个元素,它不会返回任何东西。

#2


1  

You must add the return keyword:

您必须添加return关键字:

template <class T>
T Stack<T>::pop()
{
  return myStack.pop_front();
}
template <class T>
T Stack<T>::peek() const
{
  return myStack.front();
}

or something similar, depending on your imlementation of stack.

或者类似的东西,取决于你的堆栈。

#3


0  

This will work:

这将工作:

 template <class T>
    void Stack<T>::pop()
    {
      `myStack.pop_front();
    }
    template <class T>
    void Stack<T>::peek() const
    {
      myStack.front();
    }

Or:

或者:

template <class T>
T Stack<T>::pop()
{
  `myStack.pop_front();
  // return something of type T
}
template <class T>
T Stack<T>::peek() const
{
  myStack.front();
  //return something of type T
}

#4


0  

Your signature asks you to return an object of type T.

签名要求返回类型为T的对象。

template <class T> T Stack<T>::pop()

To break this down a bit template <class T> means that for each class that you put in for T another copy of the code will be generated. For example if you did

要将其分解为一个位模板 意味着,对于您为T输入的每个类,将生成另一个代码副本。举个例子

Stack intStack; Stack doubleStack;

堆栈intStack;堆栈doubleStack;

The compiler would generate two copies of you Stack class: one specialised for int and another for double.

编译器将生成两个堆栈类的副本:一个用于int,另一个用于double。

So when you write

所以当你写

template <class T>
T Stack<T>::pop()
{
  myStack.pop_front();
}

you need to return an object of the specialised type. pop_front() only removes the first element. You need to access it remove the first item then return it.

您需要返回专门化类型的对象。pop_front()只删除第一个元素。您需要访问它,删除第一个项目,然后返回它。

This might not be the best way of doing it but to illustrate my point

这也许不是最好的方法,但可以说明我的观点

template <class T>
T Stack<T>::pop()
{
  T tmp = myStack.front();
  myStack.pop_front();
  return tmp;
}

#5


0  

As you signature indicate that function is returning type T, you should change your code to code like following for both function.

当您的签名表明函数正在返回类型T时,您应该将代码更改为如下两种函数的代码。

template <class T>
T Stack<T>::peek()
{
  return myStack.front();
}

#6


0  

While the shortviewed solution is to return a value, it is better if a pop() is of type void.

虽然简短的解决方案是返回一个值,但是如果pop()类型为void,那就更好了。

template <typename T>
void Stack<T>::pop() {
    ...
}

The reason is that you cannot write an exception safe pop()-function that returns something:

原因是您不能编写异常安全pop()函数来返回:

template <typename T>
T Stack<T>::pop() {
    T foo = stor_.top(); // May throw.
    stor_.pop(); // This line shall never throw.
    return foo;  // May throw.
}

The second line (with the pop()) should never throw something, because destructors shall not throw. So it is sane to assume that line is nothrow and will always succeed (except when you pop an empty container).

第二行(与pop()))不应该抛出什么东西,因为析构函数不应该抛出。因此,假设这一行是nothrow,并且总是会成功(除非你打开一个空容器)是合理的。

The first and third line may throw because data is being copied.

第一行和第三行可能会抛出,因为数据正在被复制。

Now imagine that

现在想象一下,

    T foo = stor_.top(); // May throw.
    stor_.pop(); // This line shall never throw.

run flawlessly: You have a value ready to be returned, and you have successfully changed the state of the holding object.

无瑕疵地运行:您有一个值准备返回,并且您已经成功地更改了保持对象的状态。

But then

但后来

    return foo;  // May throw.

bang it throws an exception. What happened:

它会抛出一个异常。事情发生的经过:

  • The function did not succeed, the caller got nothing except an exception
  • 函数没有成功,调用者除了一个异常之外什么都没有
  • Still yet, the state of the object has been modified.
  • 但是,对象的状态已经被修改了。
  • And noone has an up to date copy of the lost object anymore which typically contains critical business data that the boss phoned over seconds ago before having a fatal accident
  • 而且noone已经有了一个最新的丢失对象的副本,它通常包含了老板在发生致命事故前几秒钟就打来的关键业务数据。

This is contradictory to Abrahams Guarantees:

这与亚伯拉罕的保证相矛盾:

  • The no-throw guarantee: that the operation will not throw an exception.

    无抛出保证:操作不会抛出异常。

  • The strong guarantee: that the operation has either completed successfully or thrown an exception, leaving the program state exactly as it was before the operation started.

    强大的保证:操作成功完成或抛出异常,使程序状态与操作开始前完全相同。

  • The basic guarantee: that the invariants of the component are preserved, and no resources are leaked. Often referred to as the weak guarantee, because following an exception the system is left in a safe, but unknown, state.

    基本保证:保留组件的不变量,不泄漏任何资源。通常被称为弱担保,因为在出现异常后,系统处于安全但未知的状态。

And with a pop() that returns a value, you cannot guarantee that no information is lost.

对于返回值的pop(),不能保证不丢失任何信息。

See also GotW #8 and GotW #82

也见第8号和第82号