Reputation: 21
I'm working with this templated stack in C++:
Node:
template <typename TS>
struct spNodoPila {
TS info;
spNodoPila<TS>* next;
};
stack ADT struct with push and pop functions:
template <typename TS>
struct ADTPila {
spNodoPila <TS>* head = NULL;
spNodoPila <TS>* pos = NULL;
void push(TS dato) {
spNodoPila<TS>* nodo = new spNodoPila<TS>();
nodo->info = dato;
nodo->next = NULL;
if (head == NULL) {
head = nodo;
}
else {
nodo->next = head;
head = nodo;
}
}
void pop(spNodoPila<TS>*& nodo) {
nodo = new spNodoPila<TS>();
if (head == NULL) {
}
else if (head->next != NULL) {
nodo = head;
pos = head->next;
delete(head);
head = pos;
pos = NULL;
}
else {
nodo = head;
delete(head);
head = NULL;
}
}
};
and in this piece of code at the beggining of the previous struct i get the error "array type TS is not assignable" at the last line:
spNodoPila<TS>* nodo = new spNodoPila<TS>();
nodo->info = dato;
Something important to highlight is that later in the code i instance that stack as a str (char[60] typedef), because i want that stack to work with that type of data. Removing those instances of "str", the program compiles, so i guess there may be a problem allocating memory of char arrays, or in the way i'm doing something. Example of instance:
Stak template ADTPila instanced with "str" (char[60] typedef) into a list node:
struct spNodoLista {
sInfoLista info;
ADTPila <str> cand;
spNodoLista* prev;
spNodoLista* next;
spNodoVotos* vlink;
};
would appreciate your help!
Upvotes: 0
Views: 182
Reputation: 4249
For historical reasons, raw array types are consider4ed refence types.: They cannot be explicitly copied or assigned. But you can wrap your string in a struct as a workaround:
struct str{
char value[60];
};
or use std::array
instead which essentially does the same with more convenience and ease:
#include <array>
using str= std::array<char,60>;
but do not change the base implementation of your template. You may want to specialize the template for raw array types or use traits to increase flexibility of the template, but that is a separate long story.
cheers, FM.
Upvotes: 1
Reputation: 21
like pointed in comment section, the problem was pretty simple: i was trying to assign directly a char array to another in "nodo->info = dato;", so i solved it with strcpy(dato->info,dato).
Upvotes: 0