检查在模板第一个参数之后声明的函数是否为 NULL(传递向量和数组)
Check if function declared after Template first Argument is NULL (Passing Vector & Array)
在处理我的另一个项目时,我遇到了一个我认为是重载错误的问题。我打开了一个新项目,研究了重载,这里是快速代码:
#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;
}
如果我运行这个,我得到错误代码C2678:二进制'=='等。我通过添加以下代码行解决了这个问题:
template<class T, class A>
void operator==(const T &q, const A &w);
就在我包含头文件之后。新的错误状态
error C2451: conditional expression of type 'void' is illegal c:usersaxiomdocumentsvisual studio 2013projects_test_template_test_templatesource.cpp 11 1 _test_Template
我认为这意味着,从所有的谷歌搜索,我不能比较"first_Arg"与NULL。这就是我要做的,看看first_Arg是否为空然后从那里开始。
感谢您的帮助
您正在传递值类型(向量)给函数,但随后尝试&将它与指针(NULL)进行比较。这行不通。
所以要么你声明你的函数接受一个参数T*
,强迫你使用&my_Vector
传递my_Vector
,或者你切换到引用语义(const如果你喜欢),根本不与NULL
比较。
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- 我可以为向量函数返回 NULL<double> 吗?
- 使用 push_back() 时指针指向 NULL 的向量
- 为什么类中的向量自动变为 NULL
- 对象向量中的空对象与指针向量中的NULL/nullptr
- 在std向量中查找NULL指针
- basic_string::_M_construct null 在构造字符串的子向量后无效
- 如何用NULL(c++)初始化对的向量
- 指针指向NULL向量
- 不能将NULL发送给获取向量的函数
- 在一个向量中找到NULL
- 与向量一起使用的C++可为Null/可选项的整数
- 检查在模板第一个参数之后声明的函数是否为 NULL(传递向量和数组)