0

So I trying to build this program that will take two char and a double. Once the user has input the information. I will transfer the information to an object by using the function below. But every time I try to compile i get this error incompatible types in assignment of const char* to char [20]. Can anyone give me a hand and clarify when im not understanding. Thanks in advance.

void Molecule::set(const char* sym, const char* type, double weighT);

And this how I call it.

molecule[i].set(symbol, description,weight);

And within the set() function I just have to transfer the values inpputed to my private member in my object which i did by using this-> function.

////So This part is supposed to transfer the value to my Class Molecule private members////

this->symbol_ = sym;
this->type_ = type;      
this->weight_ = weighT;

///////Here is my Class Molecule////////

class Molecule{
private:
    char symbol_[20];
    char type_[20];
    double weight_;
public:
    void set(const char* sym, const char* type, double weighT);
    void display() const;
};
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Woody008
  • 96
  • 1
  • 2
  • 9

4 Answers4

3

You need to use strcpy or similar to copy the strings from your char * to your class's char []. Using = is attempting to copy the pointer but they are indeed incompatible types.

jeremycole
  • 2,741
  • 12
  • 15
0

symbol_ is a char[], you need to use stcpy to fill it. In set method :

   this->symbol_ = sym;

should becomes

  stncpy(this->symbol_, sym, 20);

Or simpler define symbol_ as an std::string replacing :

  char symbol_[20];

with

  std::string symbol_;
mpromonet
  • 11,326
  • 43
  • 62
  • 91
0

This code

this->symbol_ = sym;
this->type_ = type;      

is invalid. Arrays have no the copy assignment operator. You should use either standard C function strcpy or strncpy that are declared in header <cstring>

For example

std::strncpy( this->symbol, sym, 20 );
this->symbol[19] = '\0';
std::strncpy( this->type, type, 20 );      
this->type[19] = '\0';

Also it is a good idea to assign a name for magic number 20 either using an enumerator or static constant.

Also instead of character arrays you could use standard class std::string that has several overloaded assignment operators.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Pointers and Arrays are not the same thing, although it is easy to believe that they are. Arrays can be passed as arguments to functions that request pointers because of pointer decay, and that can lead people to believe that they are interchangeable.

See this article: What is array decaying?

When you declare symbol_[20], this tells the class to allocate 20 bytes when the object is created. symbol_ will always point to this allocation. You cannot reassign it as you could with a pointer.

You probably will want to copy the text pointed to by sym using std::strncpy. Be careful, though. If the string pointed to by sym is bigger than 19 characters (19 + 1 for the null character at the end), then you will need to terminate your symbol_ array with a null character (\0) manually for it to be a valid string.

#include <cstring>
...
std::strncpy(symbol_, sym, sizeof(symbol));
symbol_[19] = '\0';

I didn't get the chance to compile the code above, so, it might be best to study the example at the end of this article: http://www.cplusplus.com/reference/cstring/strncpy/?kw=strncpy

Community
  • 1
  • 1
BareMetalCoder
  • 569
  • 5
  • 17