Expression list treated as compound expression - c++

I'm trying to compile a program I got from the net. Trying to use in codeblocks but its showing errors. I don't understand what is going wrong. I've looked up in various forums but not much light is shed. Can anyone help soon? Thanks in advance
#include <functional>
#include <iostream>
int print_num(int i, int j) { return i + j; }
int main() {
std::function<int(int, int)> foo = print_num;
std::function<int(int, int)> bar;
try {
std::cout << foo(10, 20) << '\n';
std::cout << bar(10, 20) << '\n';
} catch (std::bad_function_call& e) {
std::cout << "ERROR: Bad function call\n";
}
return 0;
}
These are some of the errors other than 14 other errors saying declaration not done. I guess clearing these error would solve that problem.
main.cpp|10|error: 'function' is not a member of 'std'
main.cpp|10|error: expression list treated as compound expression in functional cast [-fpermissive]
main.cpp|10|error: expected primary-expression before 'int'

You need to compile with -std=c++11 to add in the C++11 features.
$ g++ -std=c++11 test.cxx && ./a.out
30
ERROR: Bad function call
vs:
$ g++ test.cxx && ./a.out
test.cxx: In function ‘int main()’:
test.cxx:10:3: error: ‘function’ is not a member of ‘std’
test.cxx:10:28: error: expression list treated as compound expression in functional cast [-fpermissive]
test.cxx:10:17: error: expected primary-expression before ‘int’
...

Related

Variable assignment inside a C++ 'if' statement

In C++, the following is valid and I can run it without a problem:
int main(){
if (int i=5)
std::cout << i << std::endl;
return 0;
}
However, even though the following should also be valid, it gives me an error:
if ((int i=5) == 5)
std::cout << i << std::endl;
Error:
test.cpp: In function ‘int main()’:
test.cpp:4:10: error: expected primary-expression before ‘int’
if ((int i=5) == 5)
^
test.cpp:4:10: error: expected ‘)’ before ‘int’
test.cpp:5:36: error: expected ‘)’ before ‘;’ token
std::cout << i << std::endl;
^
Furthermore, in C++17 the below code must be valid too, but it gives me a similar error again:
if (int i=5; i == 5)
std::cout << i << std::endl;
Error:
test.cpp: In function ‘int main()’:
test.cpp:4:16: error: expected ‘)’ before ‘;’ token
if (int i=5; i == 5)
^
test.cpp:4:18: error: ‘i’ was not declared in this scope
if (int i=5; i == 5)
^
I am trying to compile with g++ test.cpp -std=c++17. g++ --version gives me g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609. What am I missing here?
if ((int i=5) == 5) is a syntax error. It does not match any supported syntax for if statements. The syntax is init-statement(optional) condition, where condition could either be an expression, or a declaration with initializer. You can read more detail about the syntax on cppreference.
if (int i=5; i == 5) is correct. However, you are using an old version of GCC that dates from before C++17 was standardized. You would need to upgrade your compiler version. According to C++ Standards Support in GCC this feature was added in GCC 7.
For starters, I believe your compiler is right to reject
if ((int i=5) == 5)
because this is not legal C++ code. A variable declaration statement isn’t an expression, so you can’t treat (int i = 5) as an expression.
For the second one, I suspect you just need to update your compiler. g++ 5.6 is a fairly old version at this point, and I believe more updates versions of g++ will handle that code with no problem.

Imitating fortran print and write syntaxes in C++

I am trying to implement a class in C++ to imitate the syntax of the print and write statements from FORTRAN.
In order to achieve this, I implemented a class fooprint and overloaded fooprint::operator, (comma operator). Since this class should print to the standard output or to a file, I also defined two macros: print (for stdout) and write (to operate on files).
I get compilation errors when trying to use write(data) a; (see below for error log). How can I get a working write statement with the above properties?
This is the code (Live Demo):
#include <iostream>
#include <fstream>
class fooprint
{
private:
std::ostream *os;
public:
fooprint(std::ostream &out = std::cout) : os(&out) {}
~fooprint() { *os << std::endl;}
template<class T>
fooprint &operator, (const T output)
{
*os << output << ' ';
return *this;
}
};
#define print fooprint(), // last comma calls `fooprint::operator,`
#define write(out) fooprint(out),
int main()
{
double a = 2.0;
print "Hello", "World!"; // OK
print "value of a =", a; // OK
print a; // OK
std::ofstream data("tmp.txt");
write(data) "writing to tmp"; // compiles with icpc; it doesn't with g++
write(data) a; // this won't compile
data.close();
return 0;
}
And the compilation message:
g++ -Wall -std=c++11 -o print print.cc
error: conflicting declaration ‘fooprint data’
#define write(out) fooprint(out),
^
note: in expansion of macro ‘write’
write(data) a;
^
error: ‘data’ has a previous declaration as ‘std::ofstream data’
error: conflicting declaration ‘fooprint a’
write(data) a;
^
error: ‘a’ has a previous declaration as ‘double a’
icpc -Wall -std=c++11 -o print print.cc
error: "data" has already been declared in the current scope
write(data) a;
error: "a" has already been declared in the current scope
write(data) a;
fooprint(out) is not the creation of a temporary but rather the declaration of a variable of type fooprint with the name provided as the argument to the macro. In order to not make this a declaration, and instead an expression, two quick changes you can make are surrounding it in parenthesis (fooprint(out)) or using brace-initialization (C++11) (fooprint{out}).

C++ I'm expecting a narrowing conversion error but not getting it

In the following code from Stroustrup's book we are warned against an error from a narrowing conversion which does not occur on my version GCC (4.7.2)
#include <iostream>
using namespace std;
int main()
{
int i1 = 7.2;
int i2{7.2};
cout << i1 << " " << i2 << endl;
return 0;
}
As demonstrated here at ideone is this a bug or am I not running with the appropriate command line arguments to the compiler? Or is this meant to be a purely semantic error?
With g++ 4.8.1:
foo.cpp: In function 'int main()':
foo.cpp:8:15: warning: narrowing conversion of '7.2000000000000002e+0' from 'double' to 'int' inside { } [-Wnarrowing]
int i2{7.2};
^
I assume it's simply a bug in 4.7.2's c++11 support.
I get a narrowing warning with GCC 4.7.3 when I use -std=c++0x:
g++ -std=c++0x test.cpp
test.cpp: In function ‘int main()’:
test.cpp:8:23: warning: narrowing conversion of ‘7.2000000000000002e+0’ from ‘double’ to ‘int’ inside { } [-Wnarrowing]
g++ --version
g++ (GCC) 4.7.3

LEDA programming with c++:

I am a LEDA-6.3 user.
I have an error when compiling this simple code:
#include <LEDA/core/d_array.h>
#include <iostream>
using namespace std;
main()
{
d_array<string,string> dic;
dic["hello"] = "hallo";
dic["world"] = "Welt";
dic["book"] = "Buch";
dic["key"] = "Schluessel";
string s;
forall_defined(s,dic) cout << s << " " << dic[s] << endl;
}
G++ Compiler:
g++ -I$LEDAROOT/incl -L$LEDAROOT d_array.cpp /usr/lib/LEDA/libleda.a -lX11 -lm -o d_array
The ERROR:
d_array.cpp: In function ‘int main()’:
d_array.cpp:8: error: ‘d_array’ was not declared in this scope
d_array.cpp:8: error: expected primary-expression before ‘,’ token
d_array.cpp:8: error: expected primary-expression before ‘>’ token
d_array.cpp:8: error: ‘dic’ was not declared in this scope
Please if there is a guide for LEDA-6.3 give me the link
You probably mean leda::d_array or are forgetting using namespace leda;

Typo with "cout < myint". Why does it work?

I have this code and I searched for hours why it fails to print my income
int const income = 0;
std::cout << "I'm sorry, your income is: " < income;
Until I found I missed to write << but wrote <. Why doesn't the compiler detect this and error out? I'm not sure why comparing cout makes sense?
integral constant 0 is also a null pointer constant - it can be compared to the result of ostream's operator void *. Note that it'll fail if the constant has any value but 0.
The prototypes of the < operator are like :
​bool T::operator <(const T& b) const;
So I guess the compiler transtype the argument as the type of this instance.
Did you enabled all the warnings like -Wall
It does compile with g++ 4.4.3
#include <iostream>
int main (void)
{
int const income = 0;
std::cout << "I'm sorry, your income is: " < income;
}
However, when running it with -Wall (good practice!), I got a funny message:
:~/stack$ g++ test.cpp -o temp
:~/stack$ g++ -Wall test.cpp -o temp
test.cpp: In function 'int main()':
test.cpp:5: warning: right-hand operand of comma has no effect
No clue what it actually does (or tries to do)...
When I compile this code using GCC 4.3.4, I see a warning:
prog.cpp: In function ‘int main()’:
prog.cpp:6: warning: right-hand operand of comma has no effect
...though why it's a warning rather than an error, I don't know.
EDIT: In fact, I don't know which comma it's referring to either, because this code:
int const income = 0;
std::cout << "I'm sorry your income is: " < income;
...generates the same warning (see here).