This question already has answers here:
how does url within function body get compiled
(2 answers)
Closed 6 years ago.
Any anyone explain why I can put URIs in C++ source code?
#include <iostream>
int main() {
using namespace std;
http://www.google.com
int x = 5;
cout << x;
}
This seems strange to me?
For example Visual Studio 2015 gives me a warning: warning C4102: 'http': unreferenced label
but the code compiles!
The compiler sees http: as a label, and //www.google.com as a comment.
Related
This question already has answers here:
Function call missing argument list warning
(2 answers)
Closed 2 years ago.
I'm learning C++ online and working with Visual Studio Community 2019 (version 16.7.2). Sometimes it displays errors or weird results despite using the exact same code as the one the tutor used. Take the code below for instance. It should print out to the console but it doesn't. Instead, it shows an empty console when I run it on my end. What could be the problem? The teacher is also using Visual Studio on a Windows machine.
#include <iostream>
using namespace std;
void printSomething();
int main()
{
printSomething;
return 0;
}
void printSomething()
{
cout << "Hey! Over here." << endl;
}
Because you are only "mentioning" it here printSomething;.
To actually call it try the appropriate operator () as in printSomething();.
This question already has answers here:
How do I get crtdbg.h file?
(8 answers)
Closed 2 years ago.
When i start my code i get 'the system cannot find the file specified' error. What's wrong?
#include <iostream>
using namespace std;
int main() {
cout << "Hello world";
return 0;
}
https://i.stack.imgur.com/QoY3I.png <--- Here is my problem
Your executable did not build. If you look in the console output you have an error: cannot include crtdbg.h. This is the error you have to fix.
This might help: How do I get crtdbg.h file?
This question already has answers here:
Why statements cannot appear at namespace scope?
(3 answers)
Closed 5 years ago.
I don't know if I am just using the wrong keywords.. but I can't find an answer on google. Not can I wrap my feeble mind around my error.
This is a simple demonstration of the error:
#include <iostream>
//std::cout << "hello";
int main()
{
std::cout << "hello";
return 0;
}
Upon compiling/running this I receive this error:
main.cpp:3:6: error: 'cout' in namespace 'std' does not name a type
However, if I remove the first cout line, and just allow the program to execute the one inside of the main function, it works just fine.
Anyone got any ideas?
You cannot run code outside functions in C++. The compiler only expect variable declarations outside functions and, thus, expects std::cout to be a type, which it is not.
This question already has answers here:
What are the rules about using an underscore in a C++ identifier?
(5 answers)
Closed 7 years ago.
I tried to define a global array, named _end, of size ~1000 in C/C++, but I got a segmentation fault even when I simply iterated it. Is the name "_end" very special in C/C++ that causes such problem? Or this can be a very serious bug... (The code is attached below, and it breaks in g++ 4.3.2, 4.5.2, 4.9.2, etc.)
#include <iostream>
using namespace std;
int _end[1111];
int main() {
for (int i=0; i<1111; i++) {
cout << i << endl;
_end[i]++;
}
return 0;
}
You can see the result at https://ideone.com/XAcUeZ.
See here also for the C compiler.
Names which start with an underscore (or two) are reserved for the compiler. This is official C++ standard. Use at own risk.
This question already has answers here:
Why does this work: returning C string literal from std::string function and calling c_str()
(9 answers)
Closed 8 years ago.
Please look at this code.
#include <iostream>
#include <string>
using namespace std;
int main() {
string hello = "Hello"
, world = "World";
const char *p = (hello+world).c_str();
cout << "STRING: " << p <<endl;
return 0;
}
I have no reputation, can't post images so that I will write results by hand.
= Visual Studio 2013 ver.12.0.30110.00
STRING:
= Dev-C++ ver.4.9.9.2
STRING: HelloWorld
The first following is execution result that compiled by Visual Studio.
Second is compiled by Dev-C++.
I wonder what makes this difference.
I will be looking forward to your reply. Thanks :)
(hello+world).c_str() is only valid until the trailing ;. Accessing the memory afterwards is undefined behavior.
Visual studio probably actually clears the memory, Dev-C++ doesn't bother. Try building a release version with Visual studio (optimizations on) and you'll probably see the same behavior.