W
W
WizardW2019-02-17 12:06:02
C++ / C#
WizardW, 2019-02-17 12:06:02

Why is it not possible to write a string type variable into a structure field?

Itm* CrItm(int Id, std::string Inf)
{
  Itm *t = new Itm;
  t->next = t->prev = NULL;
  t->id = Id; 
  t.Info = Inf;  // здесь ошибка
  return t;
}

The Itm structure itself
struct Itm {
  int id;
  std::string Info[Max_ch];
  Itm *next, *prev;
};

In error: "The expression must have a class type."

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-02-17
@WizardW

Because the Info field is an array of strings, not a single string. Those. you are in fact trying to write the value of a variable of one type to a variable of another type. Suppose this code should already compile: This means: assign the value of the variable Inf to the first element of the Info array. However, I'm not sure if that's what you want. Perhaps you just need to make the Info variable a regular string, not an array, and then your code will work too.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question