This question already has answers here:
Why GCC allows calling this function without using its namespace first? [duplicate]
(1 answer)
Why does C++ parameter scope affect function lookup within a namespace?
(3 answers)
What is "Argument-Dependent Lookup" (aka ADL, or "Koenig Lookup")?
(4 answers)
Closed 4 years ago.
Case 1:
#include <iostream>
int main()
{
std::cout<<"Hello World"<<endl;
return 0;
}
Compiler give an error because endl need namespace std like std::endl.
case 2:
#include <iostream>
int main()
{
endl(std::cout);
return 0;
}
But, In second case without namespace std, endl working fine. Demo.
Why does endl working fine without namespace std?
Related
This question already has answers here:
generic way to print out variable name in c++
(6 answers)
Closed 1 year ago.
#include <iostream>
using namespace std;
#define debug(x) cout<<#x<<": "<<(x)<<endl;
int main() {
int a = 0;
debug(a)
return 0;
}
the output is: a:0
I want to print the name of variable like a
can I do it by a function not use define
i think the answers here can help you :
generic way to print out variable name in c++
and it seems the only way is using macro!
This question already has answers here:
Can a declaration affect the std namespace?
(2 answers)
Why doesn't adding sqrt() cause a conflict in C++? [duplicate]
(1 answer)
Closed 2 years ago.
#include<iostream>
double sqrt(double);
int main()
{
double a = std::sqrt(4.0);
std::cout << a;
return 0;
}
double sqrt(double a)
{
return 1.0;
}
I know I declare the sqrt at first, but I use std::sqrt, it still call my own sqrt. why?
Search your code for a using namespace std; somewhere. Perhaps hidden in some other set of include files.
This question already has answers here:
What is the meaning of prepended double colon "::"?
(9 answers)
C++ : what is :: for?
(5 answers)
Closed 5 years ago.
I just saw in some DLib code, in a .cpp file a thing like that:
#include <stdio>
namespace {
using namespace std;
void foo() {
::std::vector<int> my_vector;
}
}
Is there a reason we specify ::std:: in front of our vector we imported from std already ?
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:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 5 years ago.
In my function sumofrange I decided to output an undeclared variable just to learn the different compiler errors in C++. To my surprise, time seems to output 1 even though it is not declared anywhere.
#include <iostream>
#include <cmath>
using namespace std;
int sumOfrange( int lower, int upper){
cout<<time<<endl;
return ((( (pow(upper,2)) + upper) - ((pow(lower,2)) + lower)) / 2);
}
int main(){
cout<<sumOfrange(7,100)<<endl;
return 0;
}
You are outputting the address of a std::time function declared in a <ctime> header. You are also using a using namespace std; statement. Why that should be avoided is explained in this SO post. Depending on the compiler and the platform you might get the hexadecimal output similar to (0x)00DC52E0 if using a VC++ compiler on Windows or a number 1 if using a g++ compiler on Linux.