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)
    return;

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 {
public:
string type;
int row, col;
double **array;

public:
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

问题评论:

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

答案:

答案1:

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.

答案评论:

答案2:

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

答案评论:

3  
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…

原文地址:

https://stackoverflow.com/questions/47753422/calling-a-parameter-from-the-constructor-in-c

Tags:

添加评论