ARSN
ARSN

Reputation: 167

control reaches end of non-void function warning c++

The following code generate "control reaches end of non-void function" warning at the end of the line. What could possibly gone wrong? From quick search, it seems to have to do with return value.

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);


        }
            }
    }
}

Upvotes: 0

Views: 1753

Answers (2)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

Proper formatting is important to interpret compiler warnings and error messages correctly!

This pattern in the end of your function definition

        }
            }
    }
}

is a clear sign your formatting/indentation is seriously screwed up.

However putting a return statement before the last brace should fix the error

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

Also again regarding formatting, always make clear which code blocks are nested (ideally use braces)

            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

Upvotes: 4

SurvivalMachine
SurvivalMachine

Reputation: 8356

compute_hull is supposed to return a value of type std::vector<csce::point<T>> in all code paths, but you don't return anything if the condition if(p==hull[0]){ always fails. You could return an empty vector in the end of the function for example, just before the last }.

Upvotes: 0

Related Questions