C++ 检查模板第一个参数后声明的函数是否为NULL(传递向量和数组)
在处理我的另一个项目时,我遇到了一个我认为是过载的错误。我打开了一个新项目,研究了重载,下面是快速代码:C++ 检查模板第一个参数后声明的函数是否为NULL(传递向量和数组),c++,templates,C++,Templates,在处理我的另一个项目时,我遇到了一个我认为是过载的错误。我打开了一个新项目,研究了重载,下面是快速代码: #include <iostream> #include <vector> #include <string> template<class T, class A> void template_Function(T first_Arg, A second_Arg) { if (first_Arg == NULL){ std::
#include <iostream>
#include <vector>
#include <string>
template<class T, class A>
void template_Function(T first_Arg, A second_Arg)
{
if (first_Arg == NULL){
std::cout << "First argument of the template function is null." << std::endl;
std::cin.get();
return;
}
int main()
{
//Declare and assign values to vector.
std::vector<std::string> my_Vector;
my_Vector.push_back("Hello, Friend");
//Declare and assign values (using for loop) to array.
int my_Array[10];
for (int i = 0; i < 10; i++)
{
my_Array[i] = i;
}
//Attempting to pass BOTH the vector and array to the template function.
template_Function(my_Vector, my_Array);
std::cin.get();
return 0;
}
#包括
#包括
#包括
模板
无效模板函数(T第一个参数,第二个参数)
{
if(first_Arg==NULL){
std::cout您正在将值类型(vector)传递给函数,但尝试将其与指针(NULL)进行比较。这不起作用
因此,要么声明函数采用参数T*
,强制使用&my\u Vector
传递my\u Vector
,要么切换到引用语义(const,如果愿意)你的第二个错误意味着,你的第二个错误意味着,你的运算符==必须返回一个布尔值。对于你的第一个,你应该为指针类型创建一个专门的模板,然后与<代码> null pTr> /代码>进行比较。你想检查你的<代码>向量< /C>对象是否是空的吗?如果可以的话,可以考虑使用<代码>>第一个参数是empty()
,而不是与NULL进行比较。
template<class T, class A>
void operator==(const T &q, const A &w);