Search code examples
c++compiler-errorsreturnexpressionconditional-operator

expected primary-expression before 'return'


In function 'int v(std::string)': 7:17: error: expected primary-expression before 'return' 7:17: error: expected ':' before 'return' 7:17: error: expected primary-expression before 'return' 8:1: warning: no return statement in function returning non-void [-Wreturn-type]

#include<iostream>
#include<string>

using namespace std;

int v(string s) 
{
    s.length()? return 1:return 0;
}

int main()
{
    string s="";
    cout<<v(s);
}

Solution

  • Statements may not be used in expressions.

    Rewrite this

    int v(string s) 
    {
        s.length()? return 1:return 0;
    }
    

    like

    int v( const string &s ) 
    {
        return s.length() != 0;
    }
    

    or

    int v(string s) 
    {
        return s.length() ? 1 : 0;
    }