Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 794
D roger Avatar asked Sep 14 '19 12:09

D roger


People also ask

What is expected primary expression before?

The “expected primary expression before int” error means that you are trying to declare a variable of int data type in the wrong location. It mostly happens when you forget to terminate the previous statement and proceed with declaring another variable.

What is expected primary expression before token Arduino?

A “define” replaces the text before the compiler starts compiling. A “const int” is a fixed number and is a little more preferred for pin assignments.

What is primary expression in C++?

An expression enclosed in parentheses is a primary expression. Its type and value are identical to the type and value of the unparenthesized expression. It's an l-value if the unparenthesized expression is an l-value.


1 Answers

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;
}
like image 136
Vlad from Moscow Avatar answered Oct 14 '22 10:10

Vlad from Moscow