C++模板在取三个数最小值时使用出错

img

img


在用模板取三个数中最小的数时在另一个.h文件跳出来了一个代码报错,求解是什么问题。感谢!

#include <iostream>

using namespace std;

template <typename T>
T min(T a, T b, T c) {
    return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
}

int main() {
    int a = 3, b = 2, c = 1;
    cout << "Min of " << a << ", " << b << ", " << c << " is " << min(a, b, c) << endl;

    double x = 1.23, y = 2.34, z = 0.99;
    cout << "Min of " << x << ", " << y << ", " << z << " is " << min(x, y, z) << endl;

    return 0;
}


换个函数名吧,可能和库函数重名了

  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/762821
  • 这篇博客也不错, 你可以看下C/C++的“文件包含”处理时头文件被重复包含的问题探究及解决方法(用最简单的例子进行说明)
  • 除此之外, 这篇博客: c++ 程序员打字游戏 代码 (不完善)求指教中的 这是我编的第一个游戏程序 跟着破站视频自己做的 (说来惭愧大三了)不能再堕落下去了! 基本功能是可以实现的 但是没有做到完美 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • #define  _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <time.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <graphics.h> 
    #include <mmsystem.h>//为了加输入 成功音效
    #pragma comment(lib,"winmm.lib")//为了加输入 成功音效 但还是没有成功
    
    //数据设计
    //窗口属性
    const int width = 640;
    const int height =500;
    //游戏正确率和错误率
    int right = 0;
    int error = 0;
    //下坠文字的结构体
    struct target
    {  //每一个字符串的X,Y坐标
    	int x;
    	int y;
    	char *str;  //保存字符串
    };
    //用户输入的值
    struct USRKEY
    {
    	int x;
    	int y;
    	char str[20];
    }userkey = {320,500-40,""};
    //在指定位置输入整数
    void outtextxy_int(int x, int y, char *format, int num)
    {
    	char str[20] = "";
    	sprintf(str, format, num);
    	outtextxy(x, y, str);
    
    }
    //在指定位置输入浮点数
    void outtextxy_double(int x, int y, char *format, double num)
    {
    	char str[20] = "";
    	sprintf(str, format, num);
    	outtextxy(x, y, str);
    
    }
    //画界面分割线
    void divwindow()
    {
    	line(width - 100, 0, width - 100, height - 50);
    	line(0, height - 50, width + 50, height - 50);
    	line(width - 100, 130, width + 50, 130);
    
    }
    void inittarget(struct target words[], int n)
    {
    	static char str[29][10] = { "main","struct","int","static","char","switch","string","include",
    		"true","false","conin","graphics","stdlib","time","const","width","height","right","error",
    		"void","public","private","protect","sizeof","continue","short","else","union","default" };
    	//0--28
    	//随机产生
    	words[n].str = str[rand() % 29];
    	//0 1 2
    	//判断重复 如果重复 重新生成
    	// 与或非  &&   ||   !
    	while (words[n].str == words[(n + 1) % 3].str || words[n].str == words[(n + 2) % 3].str)
    	{
    		words[n].str = str[rand() % 29];
    	}
    	words[n].x = rand() % (width - 150);
    	words[n].y = -20;
    
    };
    //信息输出显示
    void drawScore()
    {
    	settextcolor(LIGHTBLUE);
    	settextstyle(15, 0, "mingliub");
    	//文本信息输出
    	outtextxy(width - 90, 25, "我的");
    	outtextxy(width - 90, 25 + 25, "程序员打字游戏");
    	outtextxy(width - 90, 50 + 25, "c++第一个程序");
    	//游戏状态栏输出
    	outtextxy(width - 90, 225, "正确数:");
    	outtextxy_int(width - 90, 225 + 25, " % d ",right );
    
    	outtextxy(width - 90, 285, "错误数:");
    	outtextxy_int(width - 90, 285 + 25, " % d ",error);
    
    	outtextxy(width - 90, 285+285-225, "正确率");
    	//分类讨论
    	if (right + error == 0)
    	{
    		outtextxy_double(width - 90, 285 + 285 - 225 + 25, "%.2lf%%",0.0);
    	}
    	else
    	{
    		double sum = right + error;
    		outtextxy_double(width - 90, 285 + 285 - 225 + 25, "%.2lf%%", right / sum * 100);
    	}
    }
    
    //主程序
    int main()
    {
    	srand((unsigned int)time(NULL));
    	initgraph(width + 50, height - 20);
    	struct target words[3];
    	//产生随机掉落字符串
    	for (int n = 0; n < 3; n++)
    	{
    		inittarget(words, n);
    		words[n].y = -15-n*30;		//形成不等高
    	} 
    	int i = 0;
    	BeginBatchDraw();  //实现双缓冲 防止屏闪
    	while (1)
    	{
    		mciSendString("open yx.mp3 alias music", NULL, 0, NULL);
    		cleardevice();
    		divwindow();
    		//碰线处理
    		for (int n = 0; n<3;n++)
    		{
    			words[n].y += 2;
    			if (words[n].y > (height - 50 - textheight(words[n].str)))
    			{
    				inittarget(words, n);
    			}
    		}
    		//打印文字
    		for (int n = 0; n < 3; n++)
    		{
    			settextcolor(RED);
    			outtextxy(words[n].x, words[n].y, words[n].str);
    		}
    
    		if (_kbhit()) //检测键盘 如果有按键则返回非零 为真
    		{
    			 char usertarget;//接受用户的值
    			 if ((usertarget = _getch()) != ' ')
    			 {
    				 userkey.str[i++] = usertarget;	
    			 }
    			 else
    			 {
    				 int flagError=0;
    				 //干掉正确的字符
    				 for (i=0;i<3;i++)
    				 {
    					 if (strcmp(userkey.str, words[i].str) == 0)
    					 {
    						 inittarget(words, i);
    						 right++;
    						 flagError = 1;
    						 //下面这句 可以不加 我就是想实现成功的音效 但不能重复实现
    						 //只响了一声  之后就不知道怎么重复让音效响了
    						 mciSendString("play music ", NULL, 0, NULL);
    						 
    						
    					 }
    					 mciSendString("stop music", NULL, 0, NULL);
    				 }
    				 if (flagError == 0)
    				 {
    					 error++;
    				 }
    				 i = 0;		
    				 userkey.x = 320;
    				 memset(userkey.str, 0, 20);
    			 }	
    		}
    		outtextxy(userkey.x, userkey.y, userkey.str);
    		drawScore();
    		FlushBatchDraw();//实现双缓冲 防止屏闪
    		Sleep(60);
    	}
    	getchar();
    	closegraph();
    	return 0;
    }
    

    这是我代码的具体展示图片
    在这里插入图片描述