【问题标题】:control reaches end of non-void function warning c++控制到达非无效函数警告c ++的结尾
【发布时间】:2016-07-24 04:20:29
【问题描述】:

以下代码在行尾生成“控制到达非无效函数的末尾”警告。可能出了什么问题?从快速搜索来看,似乎与返回值有关。

std::vector<csce::point<T>> compute_hull(std::vector<csce::point<T>>& points) const {

        for(std::size_t x=0; x<points.size(); x++){

            for(std::size_t m=1; m<(1<<(1<<x)); m++){
                std::vector<std::vector<csce::point<T>>> hulls;

                for(std::size_t i=0; i<points.size(); i=i+m){
                std::vector<csce::point<T>> chunk;

                if(points.begin()+i+m <= points.end())
                chunk.assign(points.begin()+i,points.begin()+i+m);

                else
                chunk.assign(points.begin()+i,points.end());            
                hulls.push_back(this->graham_scan(chunk));
            }


        std::vector<std::pair<int,int>> hull;

        hull.push_back(this->extreme_hullpt_pair(hulls));

        for(std::size_t i=0; i<m; ++i){
            std::pair<int,int> p = this->next_hullpt_pair(hulls,hull[hull.size()-1]);

            std::vector<csce::point<T>> output;

            if(p==hull[0]){

                for(std::size_t j=0; j<hull.size(); j++){
                    output.push_back(hulls[hull[j].first][hull[j].second]);
                }

                return output;
            }

            hull.push_back(p);


        }
            }
    }
}

【问题讨论】:

  • 修正您的格式并在最后一个右大括号之前放置 return 语句。
  • 回滚您的编辑,因为原始表单是正确答案的重要部分。

标签: c++


【解决方案1】:

正确的格式对于正确解释编译器警告和错误消息很重要!

这个模式在你的函数定义的末尾

        }
            }
    }
}

是您的格式/缩进严重搞砸的明显迹象。

但是在最后一个大括号之前放置一个 return 语句应该可以修复错误

    // ...
    return points; // <<<<
}

同样关于格式,始终明确哪些代码块是嵌套的(最好使用大括号)

            if(points.begin()+i+m <= points.end()) {
                chunk.assign(points.begin()+i,points.begin()+i+m); 
            }
            else {
                chunk.assign(points.begin()+i,points.end()); 
            }         
            hulls.push_back(this->graham_scan(chunk)); // Outside if / else

【讨论】:

  • 谢谢。这摆脱了警告。但是,输出仍然不稳定。有时输出是正确的。有时不是。是否有任何行为不稳定的代码?另外,感谢您的格式建议。
  • @ARSN 我建议您通过调试器逐步检查您的代码。我不能告诉你它有什么问题。
【解决方案2】:

compute_hull 应该在所有代码路径中返回 std::vector&lt;csce::point&lt;T&gt;&gt; 类型的值,但如果条件 if(p==hull[0]){ 总是失败,则不会返回任何内容。例如,您可以在函数末尾返回一个空向量,就在最后一个 } 之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    相关资源
    最近更新 更多