Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        12天前关闭。
                                                                                            
                
        
我正在实现一个为循环链表创建新节点的函数,如下所示:

Node *newNode(int data)
{
  Node *temp = new Node;
  temp->next = temp;
  temp->data = data;
}


但是,我从编译器收到警告。我知道它应该有一个返回值,但不确定实现它的正确方法。对此,我将不胜感激。
谢谢!

最佳答案

根据定义

Node *newNode(int data)


您的函数应该返回Node指针。但是,您的实现没有这样的回报。编译器会警告您有关此问题。您可以通过以下方式先验地解决问题:

Node *newNode(int data)
{
Node *temp = new Node;
temp->next = temp;
temp->data = data;
return temp;
}


注意:这是一个非常严重的问题,您必须修复您的代码,否则将有未定义的行为。

关于c++ - “警告:函数中的return语句返回非void”是什么意思? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59233380/

10-12 00:07
查看更多