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);
}
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With