calling a parameter from the constructor in c++


I am trying to use something that in the constructor but I get compile error
here is the constructor

Matrix::Matrix(int rows, int cols, std::string matType) {
type = matType;
row = rows;
col = cols;
array= new double*[row];
for (int i = 0; i < row; ++i)
    array[i] = new double[col];
for (int i = 0; i < rows; i++)
    for (int j = 0; j<cols; j++)
        array[i][j] = 0;}

Here is the implimintation of the function

void Matrix::setElement(int i, int j, double data) {
if (i > row || j > col)

if (strcmp(type, "Any") == 0) {//here is the problem i cant use type i get compile error
    array[i][j] = data;
if (strcmp(type, "Arrowhead") == 0) {
    if (data != 0 && i == 0) {
        array[i][j] = data;
    if (data != 0 && j == 0)
        array[i][j] = data; {
    if (data != 0 && j == i) {
        array[i][j] = data;
} }

And here is the header(my class)

 class Matrix {
string type;
int row, col;
double **array;

Matrix(int rows, int cols, std::string matType);    // set the (i,j) element to be 'data'
void setElement(int i, int j, double data); // return the (i,j) element

The problem is here

if (strcmp(type, "Any") == 0)

Ia m new to C++ and I don’t get whats the problem I get no suitable conversion function from std::string to const char * exists


strcmp() is for char[], just use == for std::string
Try this if (strcmp(type.c_str(), “Any”) == 0) but using == as Arun explained is better.
@dsp_user – The answer section is bellow. You should post this anti-pattern there, where it may be rightfully down-voted.
He mentioned the conversion function so that’s what I gave him. Anyway, this is simple enough for a comment.
@dsp_user – It’s an attempt at an answer. And it’s an awful answer. Which I surmise you understand, otherwise you would not attempt to bypass the peer review mechanism.



A std::string is not a const char*. It cannot be implicitly converted to one, and you can’t pass it to strcmp as if it were. But you don’t need to. std::string is sane type, and you can just compare directly:

if (type == "Any") {

For a situation where you do need a “C-string” view of a std::string, it has a member function named c_str() that returns such a pointer. But again, comparison is not such a situation.



[hint] if you’re new to C++,

  • try to not use pointers, anywhere
  • try to use a library instead of implementing matrix (eigen is a good one)
  • do read up on how bad it can get if your code contains new, and how you can benefit from using std::vector instead

This small investment will save you tons of headaches in the (near) future.


I understand that you’re giving advice which is good and all, but do add the answer to the question as well along with it
Sometimes, the question hides the true need of the OP :). His current need seems to be to avoid overly complex matter. I wish people had steered me away from writing code for stuff that existed. Heck – the sheer number of matrix implementations I’ve seen…