Cant send NULL to a function getting vector
Asked Answered
O

5

7

I have this function:

    void RegMatrix::MatrixInit(int numRow,int numCol, std::vector<double> fill)

    {

      do something;

    }

and i want to send this:

MatrixInit(numRow,numCol,NULL);

how can i pass NULL as vector?

Oklahoma answered 13/9, 2011 at 19:31 Comment(5)
NULL is not a vector. What would you like it to mean when you pass NULL for that parameter?Ias
i come from java, its a hard world here at c++, iwant an empty vectorOklahoma
Then you should also appreciate that a null pointer is not an empty vector in Java, either.Ias
in java you can send null instaed of empty vectorOklahoma
In java you're actually sending a vector pointer as well. Remy Lebeau - TeamB's answer is most java-like, but K-ballo's answer is more C++ -likePejoration
L
17

You cannot pass NULL as vector, you could instead pass an empty vector like this:

MatrixInit( numRow, numCol, std::vector<double>() )

Note that you would be better off taking the fill vector as const&.

Leatherback answered 13/9, 2011 at 19:33 Comment(6)
Presumably the function is supposed to change the vector.Osman
Oh is it? There is no reference for that and I assumed fill were the initial values for the matrix. If that's the case, then it should be & rather than const&, of course.Leatherback
@wilhelmtell, since the original was pass-by-value it couldn't change the vector.Orme
@Mark I said presumably, drawing from the name of the function. The signature of the function implies the function has zero side-effects, not even a return-value, and I guess that's a mistake.Osman
@wilhelmtell: It's a non const member function, pressumably to initialize the object.Leatherback
holy lazer beams of batman, it's true. i was skimming so fast didn't even realize it's a member. :-SOsman
A
13

In order to pass NULL, you need to change your parameter to accept a vector* pointer instead, eg:

void RegMatrix::MatrixInit(int numRow, int numCol, std::vector<double> *fill)
{
    if (fill != NULL)
    {
        do something with fill;
    }
}

When you need to pass in a vector, you would then do it like this:

std::vector<double> v;
MatrixInit(numRow, numCol, &v); 
Andres answered 13/9, 2011 at 19:35 Comment(0)
O
2

If you want a value that represents a special case, you can make a static variable and test against it.

static std::vector<double> null_fill;

void RegMatrix::MatrixInit(int numRow,int numCol, const std::vector<double> & fill = null_fill)
{
    if (&fill == &null_fill)
        // do something special
    else
        // do something;
} 
Orme answered 13/9, 2011 at 19:38 Comment(0)
G
0

You could make the argument type boost::optional<std::vector<double> >

As the name says, that's an optional argument. If you don't use the argument, you pass boost::none instead. (not NULL; that's a null pointer constant and we're not using pointers here).

Glynis answered 13/9, 2011 at 21:2 Comment(0)
C
0

You cannot have a null vector in C++.

C++ is a value-type language.A variable of type vector contains the value of vector directly, NOT a reference to a vector like in java.

MatrixInit(numRow,numCol,NULL);

The above statement will give you an error : Cannot convert NULL(zero) to std::vector.

Coercion answered 18/7, 2022 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.