Omar Moodie
Omar Moodie

Reputation: 290

Check if function declared after Template first Argument is NULL (Passing Vector & Array)

While working with one of my other projects I came across what I believed to be an overloading error. I opened a new project, researched about overloading and here is quick code:

#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;
}

If I run this I get the error code C2678: binary '==' etc. I solved that issue by including these lines of code:

template<class T, class A>
void operator==(const T &q, const A &w);

Right after I included the headers. The new error states,

error C2451: conditional expression of type 'void' is illegal c:\users\axiom\documents\visual studio 2013\projects\_test_template\_test_template\source.cpp 11 1 _test_Template

I THINK it means, from all the googling that I can't compare "first_Arg" with NULL. Which is pretty much what I'm trying to do, see if the first_Arg is null and then go from there.

Thank you for any assistance.

Upvotes: 1

Views: 349

Answers (1)

deets
deets

Reputation: 6395

You are passing a value-type (vector) to a function, but then try & compare it with a pointer (NULL). That can't work.

So either you declare your function to take a parameter T*, forcing you to pass my_Vector using &my_Vector, or you switch to reference semantics (const if you like), and don't compare with NULL at all.

Upvotes: 1

Related Questions