【发布时间】:2020-03-09 12:57:24
【问题描述】:
我用模板编写了一个代码,但它只适用于 Visual Studio(不适用于 Dev c++ 或任何在线编译器。我不明白为什么。
#include <iostream>
using namespace std;
template <class Q1,class Q2,class Q3> // but when i write instead of 3 classes 1 class it will work
//everywhere, how could it be possible?
void min(Q1 a, Q2 b, Q3 c) {
if (a <= b && a <= c) {
cout << "\nMinimum number is: " << a << endl; }
if (b < a && b < c) {
cout << "\nMinimum number is: " << b << endl; }
if (c < a && c < b) {
cout << "\nMinimum number is: " << c << endl; }
}
int main()
{
double x,y,z;
cout << "Enter 3 numbers: " << endl;
cin >> x;
cin >> y;
cin >> z;
min(x, y, z);
}
【问题讨论】:
-
using namespace std;- 删除它并在需要的地方输入std::。std::min是一个东西,你不想名字冲突。 -
请将错误复制/粘贴到您的问题中。
-
错误:“__comp”不能用作函数
-
编译器无法区分你的函数和
std::min。碰巧 MSVC 没有在iostream中使用#include <algorithm>,但其他编译器做到了(并且完全允许他们这样做)。 -
是的,或者将其更改为
::min(x, y, z)。您自己的函数与std::min冲突,这是为什么using namespace不好的一个很好的例子。
标签: c++ visual-studio dev-c++