CS253 Implicit Inclusion
Is this code correct?
#include <iostream> using namespace std; int main() { string s = "hello\n"; cout << s; return 0; }
hello
#include <iostream> using namespace std; int main() { string s = "hello\n"; cout << s; return 0; }
hello
The code is not correct. A std::string
was declared,
but there is no #include <string>
. But, still, it compiled. ☹
operator<<
cout << s;
. This meaning of <<
isn’t
built into C++ like integer addition. Instead, somebody must have
written a operator<<
function that takes an ostream
and a
std::string
.
operator<<
be?
<iostream>
and <string>
.
ostream
and string
.
<iostream>
, then <iostream>
has to #include <string>
.
<string>
, then <string>
has to #include <iostream>
.
#include <iostream> using namespace std; int main() { string s = "hello\n"; cout << s; return 0; }
hello
<iostream>
includes <string>
.
#include <iostream> #include <string> using namespace std; int main() { string s = "hello\n"; cout << s; return 0; }
hello
#include
what you need.
Is this just theoretical handwaving?
No. For nearly every assignment, somebody turns in code that compiles on their computer, but fails on CSU computers.
Modified: 2017-04-28T13:06 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |