Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant send NULL to a function getting vector

Tags:

c++

vector

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?

like image 560
Itzik984 Avatar asked Sep 13 '11 19:09

Itzik984


People also ask

Can we assign null to a vector?

You can't have a null vector in C++. C++ is a value-type language … a variable of type vector<int> contains the value of vector<int> directly, NOT a reference to a vector like in java. So, you declare a vector, you get an empty vector. There is no such thing as a null vector.

How do you pass vector to a function?

When we pass an array to a function, a pointer is actually passed. However, to pass a vector there are two ways to do so: Pass By value. Pass By Reference.

How do you return a NULL in vector?

Explanation to the Code:Declare using std::cout and std::vector. Declare function with vector type return type which accepts vector as a variable. Then return null vector inside function body. Declare main function.

Can we return NULL for vector in C++?

A vector cannot possibly be NULL, but it can be empty(). No. NULL is normally used with a pointer. You could however return an empty vector and verify on the other side if it's empty.


1 Answers

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&.

like image 146
K-ballo Avatar answered Oct 02 '22 17:10

K-ballo