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!
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:
ambiguous call in template error
(4 answers)
Closed 6 years ago.
I am trying to use c++ template generics with a void function,
The code:
#include <iostream>
using namespace std;
template <typename T>
inline void swap(T& x, T& y)
{
T temp = x;
x = y;
y = temp;
}
int main()
{
cout << "Swapper!" << endl;
int x, y;
cin >> x >> y;
swap(x, y);
cout << x << y;
cin.get();
return 0;
}
But it gives an error:
call of overloaded swap() is ambiguous
How do I remove this error?
The problem is using namespace std;. You should almost never use this line anywhere in your code. A good approach is to instead just quality all std names, i.e. write std::cout << "Swapper!" << std::endl; instead of cout << "Swapper!" << endl;.
In this particular case, your own swap function conflicts with std::swap, which you indirecty get via <iostream>. While std::swap is only guaranteed to exist in <algorithm> and <utility>, all C++ standard headers are allowed to pull in any other C++ standard headers.
So once you include any standard header, using namespace std; creates a potential conflict with all names in std.
You should remove using namespace std; because this namespace already contains a function called swap and the compiler doesn't know which one to choose. To be honest, you don't really need to write such a function yourself, it has already been done for you.
Another way is to rename your function to something other than swap.
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).
This question already has answers here:
Namespace + functions versus static methods on a class
(9 answers)
Closed 8 years ago.
in c++ what is the difference between using a namespace and a class??
like::
in this example i added namespace
#include <iostream>
using namespace std;
namespace ns{
void print(){
cout<<"Hello, World!";
}
}
int main(){
ns::print();
return 0;
}
vs:
and in this one i added a class
#include<iostream>
using namespace std;
class cs{
void print(){
cout<<"Hello World!";
}
}
int main(){
cs classOject;
classObject.print();
return 0;
}
but both got me the same result;;;
that question made me keep thinking for a week
thanks for any replies guys and all repliers are much appreciated...
they are too different to describe here in detail. i recommend you to read something about oop.
classes are definition of objects, and namespaces can be used to build logical groups of code.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
overload operator<< within a class in c++
Operator overloading
Is there any way this is possible?
#include <iostream>
using namespace std;
struct test{
int n;
};
int main(){
test t1;
cin >> t1;
return 0;
}
For all I know, it is not possible, but I had an exam yesterday and that question came in it, it asked me to write the functions missing.
Add the include:
#include <stdlib.h>
You should include both namespaces:
using namespace System; // ie System::Console
using namespace std;
You'll need to use:
std::cin >> t1.n;
In C++, structs are the same as classes. So yes, you can do the same thing as you do with classes.