-3

I can't find anything online about making a linked list with individual nodes that hold arrays.

struct node{
    string list[]; 
    string var;
    node* next = NULL; 
    node* previous = NULL; 

}; 

void insertion(node*& start, string name, string words[]){
  node* temp = new node;
  temp->var = name;
  temp->list = words;

  if (!start) { 
    start = temp;
    return;
  } else { 
    node* tail = start;
    while(tail->next) {
      tail=tail->next;
    }
    tail->next = temp;
    temp->previous = tail;
 }

The code above gives me the error:

web.cpp: In function ‘void insertion(variable*&, std::__cxx11::string, std::__cxx11::string*)’: web.cpp:18:16: error: incompatible types in assignment of ‘std::__cxx11::string* {aka std::__cxx11::basic_string*}’ to ‘std::__cxx11::string [0] {aka std::__cxx11::basic_string [0]}’ temp->list = words;

1 Answers1

1

First of all, string list[] is not allowed as a data member definition, since it is an incomplete type. So your code actually should give you an error already in the definition of struct node.

In addition to that issue, in C++, array types are not assignable (regardless of their size). So even if you wrote:

struct node{
    string list[4];
    string var;
    node* next = NULL;
    node* previous = NULL;

};

int main(){

    node n1;
    node n2;

    n1.list = n2.list;  // error: array types are not assignable
}

you'd get an error indicating that array types cannot be assigned in the sense of array1 = array2.

If you used a vector<string>, in contrast, you could assign as well as letting your vector dynamically grow as needed.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58