printing to console using ostream and operator << - c++

In an old project code was a statement like:
cout.operator<<("Hi...");
ostream.operator<<("Hi....");
It argued that it can be written as followa, resulting in same output:
cout << "Hi..";
I have tried to put these statement in main(). But compiler gives error:
In function 'int main()':
11:3: error: 'ostream' was not declared in this scope
11:3: note: suggested alternative:
In file included from /usr/include/c++/4.9/ios:38:0,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 2:
/usr/include/c++/4.9/iosfwd:136:33: note: 'std::ostream'
typedef basic_ostream<char> ostream;
The other thing that I am not convinced of is this statement that ostream can directly call operator<<. Same about cout that can it call operator<< directly.
If some body could elaborate or explain how, then I will able to debug the code.
Thanks.

The code line in question can actually ber compiled and does work, e.g:
#include <iostream>
using namespace std;
int main (void)
{
cout << "Hi"; // code line in question
cout << endl;
return 0;
}
The quoted error is caused by using ostream, which is the type of cout, in a place where an object (not a type) is required.
As also mentioned in a comment, the fix of that error is using an object of that type instead. Which basically means the first line of quoted code, i.e. the line in the solution code above.

Related

Analyzing a simple C++ program with Frama-C

I started learning C++ from a great tutorial available at https://learnxinyminutes.com/docs/c++/ and would like to analyze in Frama-C a simplest example that shows references:
using namespace std;
#include <iostream>
#include <string>
int main() {
string foo = "I am foo";
string bar = "I am bar";
string& fooRef = foo; // This creates a reference to foo.
fooRef += ". Hi!"; // Modifies foo through the reference
cout << fooRef; // Prints "I am foo. Hi!"
// Doesn't reassign "fooRef". This is the same as "foo = bar", and
// foo == "I am bar"
// after this line.
cout << &fooRef << endl; //Prints the address of foo
fooRef = bar;
cout << &fooRef << endl; //Still prints the address of foo
cout << fooRef; // Prints "I am bar"
//The address of fooRef remains the same, i.e. it is still referring to foo.
return 0;
}
I compiled and installed Frama-C C++ plug-in called "Frama-Clang".
Now when I run frama-c I get warnings and errors in the output:
$ frama-c refs.cc
[kernel] Parsing FRAMAC_SHARE/libc/__fc_builtin_for_normalization.i (no preprocessing)
[kernel] Parsing refs.cc (external front-end)
refs.cc:13:17: warning: using directive refers to implicitly-defined namespace 'std'
using namespace std;
^
In file included from refs.cc:14:
In file included from /usr/share/frama-c/frama-clang/libc++/iostream:29:
/usr/share/frama-c/frama-clang/libc++/ostream:31:40: error: implicit instantiation of undefined template 'std::basic_ios<char, std::char_traits<char> >'
class basic_ostream : virtual public basic_ios<charT,traits> {
^
refs.cc:23:7: note: in instantiation of template class 'std::basic_ostream<char, std::char_traits<char> >' requested here
cout << fooRef; // Prints "I am foo. Hi!"
^
/usr/share/frama-c/frama-clang/libc++/iosfwd:37:68: note: template is declared here
template <class charT, class traits = char_traits<charT> > class basic_ios;
^
code generation aborted due to one compilation error
[kernel] user error: Failed to parse C++ file. See Clang messages for more information
[kernel] user error: stopping on file "refs.cc" that has errors.
[kernel] Frama-C aborted: invalid user input.
What is wrong?
(Frama-C is installed from a debian-testing repository in version 20170501+phosphorus+dfsg-2)
First of all, I'd like to point out the caveat on the Frama-Clang page:
Frama-Clang is currently in an early stage of development. It is known to be incomplete and comes without any bug-freeness guarantee.
Thus, if you're not already familiar with C++, I'd kindly suggest that starting right away with Frama-Clang might be a pretty big effort.
That said, the issue is, as mentioned in the comments, that STL support in Frama-Clang is minimal (in particular, the innocent-looking iostream is not exactly the easiest piece of code to handle when it comes to templates).
You might have better luck by using frama-c -cxx-nostdinc refs.cc, which will use your system's standard library instead of the one shipped with Frama-Clang: this will at least let clang type-check your code. There is however absolutely no guarantee that Frama-Clang itself will be able to understand all the constructions provided by this library.

Trouble running C++ through terminal in Ubuntu

I wrote the following code after using "gedit take_input.cpp":
#include <iostream>
int main()
{
cout<<"please enter your name (followed by 'enter')\n";
string file;
cin >> file;
cout<<"hello" << file << " ! welcome to ilinux, where innovation is a promise\n";
}
However, when I used "g++" to convert my human-readable code into object code (writing g++ take_input.cpp -o take_input), the terminal returns with a result similar to this:
take_input.cpp: In function ‘int main()’:
take_input.cpp:5:1: error: ‘cout’ was not declared in this scope
cout<<"please enter your name (followed by 'enter')\n";
^
take_input.cpp:5:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:61:18: note: ‘std::cout’
extern ostream cout; /// Linked to standard output
^
take_input.cpp:7:1: error: ‘string’ was not declared in this scope
string file;
^
take_input.cpp:7:1: note: suggested alternative:
In file included from /usr/include/c++/4.9/iosfwd:39:0,
from /usr/include/c++/4.9/ios:38,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from take_input.cpp:1:
/usr/include/c++/4.9/bits/stringfwd.h:62:33: note: ‘std::string’
typedef basic_string<char> string;
^
take_input.cpp:9:1: error: ‘cin’ was not declared in this scope
cin >> file;
^ ^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
cin >> file;
take_input.cpp:9:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
cin >> file;
^
Could you tell me what the reason is?
The errors that you are getting are because the cout is not in the global namespace rather it is in std namespace.
Well instead of writing
using namespace std;
after #include <iostream> try using:
using std::cout;
since using first option is a bad practice. You can refer to Why is using namespace std is a bad practice.
For benefits of using using std::cout refer Using std namespace
Also you can use std::cout everywhere if you don't want to use using std::cout.
Just read the error messages that you compiler gave you. The problem is that
‘cout’ was not declared in this scope
And the "suggested alternative" is std::cout. Same goes for string vs. std::string.
Note that generally, things belonging to the standard library need to be qualified with std:: to be found.
You also need to #include <string> to use std::string btw.
The compiler gives you the answer on line 7: Since you're not using the std namespace, you have to prepend std:: to your cout and cin calls.
Just add
using namespace std;
after #include <iostream>
Try this out.

Cannot get simplest Phoenix lambda to compile

I am currently trying to get the following very simple boost::phoenix::lambda to compile:
#include <iostream>
#include <boost/phoenix/scope.hpp>
int main() {
boost::phoenix::lambda[std::cout << "Lambda!!"]();
}
However, this generates a host of errors (too much to post here), none which make any sense to me. Here is an excerpt of the compiler output:
error: 'std::ios_base::ios_base(const std::ios_base&)' is private
within this context
error: initializer for
'boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,
boost::proto::argsns_::term<boost::phoenix::vector0<> >, 0l>::proto_child0
{aka boost::phoenix::vector0<>}' must be brace-enclosed
I am compiling these using MinGW 4.7.2 on Windows XP with Boost 1.53.0. What am I doing wrong?
Firstly, always
#include <boost/phoenix/phoenix.hpp>
unless you know what you're doing.
Secondly, you need to make either operand of operator<< be a phoenix terminal, otherwise, it will be just
std::cout << "Lambda!!"
which is an expression of type std::ostream&...
Now, you could do anything, really, e.g.
phx::ref(std::cout) << "Lambda!!"
or
std::cout << phx::val("Lambda!!")
Either will compile.

Passing fstream reference as function parameter in C++11

I've just encountered an problem today: The following code seems to work in MSVC++ 2010 but not with Clang LLVM 4.1 (with GNU++11).
#include <fstream>
void foo(std::fstream& file){
file << "foo";
}
int main() {
std::fstream dummy("dummy");
foo(dummy);
return 0;
}
generates
Invalid operands to binary expression (std::fstream (aka basic_fstream<char>) and const char[4])
on Clang. I thought passing iostream arguments by reference would be common practice in C++. I'm not even sure if this is related to clang, C++11 or anything else.
Any idea how I can pass streams to functions then?
I assume that your original code (that you only partially posted in your original question) looked something like this:
#include <iosfwd>
void foo(std::fstream& file){
file << "foo";
}
int main() {
std::fstream dummy("dummy");
foo(dummy);
return 0;
}
Indeed, this gives the following error message with clang++ 3.2
Compilation finished with errors:
source.cpp:4:10: error: invalid operands to binary expression ('std::fstream' (aka 'basic_fstream<char>') and 'const char [4]')
file << "foo";
~~~~ ^ ~~~~~
source.cpp:8:17: error: implicit instantiation of undefined template 'std::basic_fstream<char, std::char_traits<char> >'
std::fstream dummy("dummy");
^
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/iosfwd:118:11: note: template is declared here
class basic_fstream;
^
2 errors generated.
Unfortunately, you only posted the first error message but not the second.
From the second error message, it is obvious that you only #include <iosfwd> but not #include <fstream>. If you fix that, everything will be OK.
Please post both the complete code and all the error messages next time.

namespaces ; expected unqualified id before ')' token ; invalid use of struct

I am new to c++ and am trying to understand namespaces and how they work
I thought i'd code up a simple "hello world" program using namespaces but as it turned
out, it seems to have backfired on me and i am getting a bunch of weird errors.
Here is my code:
#include <iostream>
namespace names
{
using namespace std;
void class hello() //line 7 <-- here is where the compiler is complaining
about the 'unqualified id'
{
cout <<"Hello World";
}
}
int main()
{
names::hello(); //line 16
}
And here is the output:
E:\CB_Workspace\Names\names_main.cpp|7| error: expected unqualified-id before ')' token|
E:\CB_Workspace\Names\names_main.cpp|| In function 'int main()':|
E:\CB_Workspace\Names\names_main.cpp|16| error: invalid use of incomplete type 'struct names::hello'|
E:\CB_Workspace\Names\names_main.cpp|7| error: forward declaration of 'struct names::hello'|
||=== Build finished: 3 errors, 0 warnings ===|
I am not sure what is going on and I have tried to search through other posts on this error.
The other post i found on this did not really address the context of namespaces.
g++ error - expected unqualified-id before ')' token
Any help would be much appreciated. Thank you
edit: ok thanks guys. I removed the "class" under my namespace and it works now. I'll flag it to be closed now. Thanks for the help
You are not trying to write a class there. A class is different than a function. Please try:
void hello()
This has nothing to do with namespace.
In C/C++ the rule for declaring a function is:
returnType functionName(functionArgument1,functionArgument2,...);
Your way of declaring the function does not follow the C/C++ rule. What you have is:
void class hello();
It should be:
void hello();
Probably you are confusing it with syntax to define the function outside the class body. In that case the rule is:
returnType className::functionName(functionArgument1, functionArgument2,...)
{
}
Namespace does not affect how function is declared. It defines where the function is available
void class hello()
Huh? How can a function also be a class? Just remove that:
void hello()