why using std::sqrt failed if I redefine the sqrt function? [duplicate] - c++

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.

Related

Why does `endl` working fine without namespace `std`? [duplicate]

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?

What's the meaning of ::std::vector [duplicate]

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 ?

c++ template ambiguous error [duplicate]

This question already has answers here:
Why Template Function call is ambiguous?
(3 answers)
Closed 5 years ago.
Here's my code trying to simply add 2 numbers.
#include <iostream>
#include <string>
using namespace std;
template<class first, class second>
first plus(first x, second y) {
return x + y;
}
int main() {
int a = 123;
int b = 21;
plus(a, b);
return 0;
}
The plus() gives me an error stating that it's "ambiguous". This is basically copied code I've seen in tutorials(where it has worked!) on templates so I'm really confused now.
Remove the using namespace std, you are colliding with std::plus
http://en.cppreference.com/w/cpp/utility/functional/plus
I've solved the issue either removing std namespace or changing the function name is all it takes!

Macro anomaly in c++/g++ [duplicate]

This question already has answers here:
Unexpected Result in Macro
(2 answers)
Sequence points and partial order
(3 answers)
Closed 7 years ago.
The code below should return the value 216 but it returns 392 that is (7*7*8) . Can somebody please explain me how?
#include<iostream>
#define cube(x) (x*x*x)
using namespace std;
int main()
{
int x=5;
cout<<cube(++x);
cout<<endl;
return 0;
}
After macro expansion you will get:
using namespace std;
int main()
{
int x=5;
cout<<(++x*++x*++x);
cout<<endl;
return 0;
}
and it is undefined behavior.
Just use a function instead of the cube macro:
int cube(int x) {
return x * x * x;
}
You should really use a function and not a macro. The cube(++x) expands to
++x*++x*++x
which isn't at all what we want.

Templates do not compile with dev c++ [duplicate]

This question already has answers here:
call of overloaded 'min(int&, int&)' is ambiguous
(5 answers)
Closed 6 years ago.
#include<conio.h>
#include<iostream>
using namespace std;
template<class T>
T min(T a,T b)
{
return (a>b)?a:b;
}
int main()
{
int x,y;
cin>>x>>y;
cout<<"min. of integer value is="<<min(x,y); //error is call of overloaded function is ambiguous.
float p,q;
cin>>p>>q;
cout<<"min. of floating value is="<<min(p,q);//same error as above
char c1,c2;
cin>>c1>>c2;
cout<<"min. of c1 and c2(basis of ASCII values)="<<min(c1,c2);// same error as above
getch();
return 0; }
Is there any inbuilt feature of dev c++ that doesnt support templates or is there some other error?
The reason is there is a std::min, which gets imported into the global namespace (due to the using namespace std;).
So you have 2 different versions of min: Your version (which actually returns the maximum...), and the standard min.
Either rename your min-function, or remove it (and use std::min instead).