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).
Related
This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 2 years ago.
I want to call a function reverse(BidirectionalIterator first, BidirectionalIterator last) from <algorithm> header file inside my function, whose name is also reverse(int).
code:
#include<iostream>
#include<algorithm>
using namespace std;
class Solution{
public:
int reverse(int x){
string num = to_string(x);
reverse(num.begin(), num.end());
}
};
I thought it would automatically call the appropriate function based on the parameters passed just like function overloading. But, it doesn't.
I tried:
namespace algo{
#include<algorithm>
}
But it is giving a lot of errors.
Ahh, now you're experiencing the reason people on StackOverflow are always yelling about not using using namespace std;. The issue is that you're bringing the whole namespace into the global namespace, which'll lead to clashes like this.
If you delete that line, however, now all of your imported functions stay in the std namespace, so you could do:
#include<iostream>
#include<algorithm>
// BAD
// using namespace std;
class Solution{
public:
int reverse(int x){
std::string num = std::to_string(x);
std::reverse(num.begin(), num.end());
return std::stoi(num); // Don't forget to return!
}
};
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:
Warnings or errors for C++ implicit conversion of primitives
(6 answers)
Closed 5 years ago.
I am using the following sample program in C++
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
namespace mine{
template<class T>
inline void swap(T &a, T &b){
char c= a; //This should not have compiled
a=b;
b=c;
}
}
int main(){
int a,b;
cout<< "Enter two values: ";
cin>>a>>b;
mine::swap(a,b); //type variable T is instantiated as in
cout << a <<' '<<b << endl;
}
I am expecting the compiler to throw an error in the swap function, because c is declared as a char, but assigned a variable of generic type variable T. Not only that, when invoking swap, T is instantiated as int. However, not only is g++ not giving any error, the program works perfectly. Why is this the case?
C++ gives you the ability to shoot yourself in the foot.
The fact remains that any integral type is convertible to a char with implementation defined behaviour.
The compiler is assuming you know what you are doing, that's all.
auto c = a; would be the best replacement these days. Prior to C++11 you could have written T C = a; (you still can of course.) Although since C++11 you ought to use std::move when swapping, see how std::swap is implemented on your platform. (Reference How does the standard library implement std::swap?)
gcc will warn you of this if you specify -Wconversion on the command line.
First of all, converting int to char is legal, it is a narrowing conversion, and the compiler might warn you if you configure it to do so.
As for why the code compiles, this is due to the fact that the type is known at compile time, therefore the compiler knows that for all the instances of T that it initialised, T is convertible to char.
If you will change a to a type that is not convertible to char, the compiler will complain: e.g with MSVC
The following code give error C2440: 'initializing' : cannot convert from 'std::string' to 'char':
#include "stdafx.h"
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
namespace mine{
template<class T>
inline void swap(T &a, T &b){
char c= a; //This should not have compiled
a=b;
b=c;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string a("test");
string b("test2");
mine::swap(a,b); //type variable T is instantiated as in
}
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.
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!