Question

I am attempting to initialize variables within my object, using a function with const pointers as parameters.

I keep getting errors in many of the ways i attempted, here is my code:

class Molecule
{
   private:
    char s[21];
    char d[21];
    double w= 0;

   public:
    Molecule();
    void set(const char*, const char*, double);
    void display() const;
};

int main() {
    int n;

    cout << "Molecular Information\n";
    cout << "=====================" << endl;

    cout << "Number of Molecules : ";
    cin >> n;

    Molecule *molecule = new Molecule[n];
    for (int i = 0; i < n; i++) {
        char symbol[21];
        char description[21];
        double weight;

       molecule[i].set(&symbol,&discription,weight);
      //...
   }
   //implementation of class

   #include "Molecule.h"
   #include <iostream>
   #include <cstring>

   void Molecule::set(const char*, const char*, double)
   {

    s = &symbol;
    d = &discription;
    w = &weigth;
   }

My question is: How would i correctly call the member function from an array of objects, using constant chars as parameter, and what is the correct way to set them to my variables in my class.

P.S: I have been trying to figure this out for a long time, and posting here is a last resort.

Was it helpful?

Solution

There are multiple errors in your code

  1. &symbol (where symbol is char[21]) yields char(*)[21], use symbol directly and let it decay to char* or use explicitly &symbol[0]

  2. double weight; is uninitialized local variable, using it results in undefined behavior - you should initialize it: double weight = 0.0;

  3. double w= 0; used to declare a member of class Molecule is invalid, you could use constructor's initializer list:

    Molecule() : w(0.0) { } // initializes `w` to `0.0`
    
  4. s = symbol; where s is char[21] and symbol is char* will not copy strings, for C-style copying strcpy could be used (note that C and C++ are different languages)

  5. you have called new[] so it would be nice and appropriate to call delete[] as well and instead of relying on OS cleaning it up after the program terminates: (otherwise follow the point 6)

    Molecule *molecule = new Molecule[n];
    ...
    delete[] molecule;
    
  6. If you are allowed to use vectors, replace Molecule *molecule = new Molecule[n]; with std::vector<Molecule> molecules(n);

  7. If you are allowed to use std::string1) objects, replace char[21] / char* with std::string objects

Other suggestions:

  • use meaningful names for variables, if you want to explicitly distinguish private members from other local variables, good convention is to use _ at the end of the name:

    class Molecule {
    private:
        std::string symbol_;
        std::string description_;
        double weight_;
    

1) Basically what you need to know about std::string is that it is a template that wraps raw char* and it already contains well-defined copying, concatenation using operator + and most important: you don't need to bother with memory management. Just #include <string>

OTHER TIPS

In the call

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

you are passing a pointer to a char array. This does not match the char* that set expects.

The easiest/best fix is to change this to

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

relying on the symbol and description char arrays automatically decaying to pointers.

Alternatively, you could also write

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

to explicitly pass char*

[Note that there are many other errors in the code posted. Based on the question, I'm guessing they are just typos. Please update your question if you'd like more info onn any of the other errors.]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top