Call to functions nested in STRUCT within a CLASS

1

I have this implementation of a class that handles linked lists.

The issue is that I'm using primitive functions in the struct I use within the class, however I do not know much about it, and I do not know how to invoke the functions. Here I reproduce my problem. thank you very much

#include <iostream>
#include <stdlib.h>
using namespace std;

class linkedList
{
private:

    struct node
    {
        char valor;
        node *next;
        node( char nuevoValor, node * nuevoNext)
         : valor( nuevoValor ), next( nuevoNext )
         {   }
         // prinmitivos
         static node * stringALista(const char *s); // para el constructor
    };

    node *head;

public:
    explicit linkedList( const char * s = "");
    //linkedList( const linkedList & s );

};


int main()
{
    linkedList list1 ("hola mundo");
}

linkedList::linkedList(const char *s)
{
    // como llamar a la funcion stringALista???
}

// aquí   ↓ dice 45 11  Error] expected unqualified-id before '.' token
linkedList.node * linkedList::stringALista(const char *s)
{
    node * aux = NULL;
    node * cur = NULL;
    int i = 0;
    while ( ( s + i * sizeof(char)) != '
#include <iostream>
#include <stdlib.h>
using namespace std;

class linkedList
{
private:

    struct node
    {
        char valor;
        node *next;
        node( char nuevoValor, node * nuevoNext)
         : valor( nuevoValor ), next( nuevoNext )
         {   }
         // prinmitivos
         static node * stringALista(const char *s); // para el constructor
    };

    node *head;

public:
    explicit linkedList( const char * s = "");
    //linkedList( const linkedList & s );

};


int main()
{
    linkedList list1 ("hola mundo");
}

linkedList::linkedList(const char *s)
{
    // como llamar a la funcion stringALista???
}

// aquí   ↓ dice 45 11  Error] expected unqualified-id before '.' token
linkedList.node * linkedList::stringALista(const char *s)
{
    node * aux = NULL;
    node * cur = NULL;
    int i = 0;
    while ( ( s + i * sizeof(char)) != '%pre%' )
    {

        if ( cur == NULL)
        {
            cur = new node;
            cur->valor = &s;
            cur->next = NULL
            aux = cur;
        }
        else 
        {
            while (cur->next != NULL) 
                cur = cur->next;
            cur->next = new node;
            cur->next->valor = &s;
            cur->next->next = NULL;
        }

    }
    return aux;
}
' ) { if ( cur == NULL) { cur = new node; cur->valor = &s; cur->next = NULL aux = cur; } else { while (cur->next != NULL) cur = cur->next; cur->next = new node; cur->next->valor = &s; cur->next->next = NULL; } } return aux; }

What I want is to be able to use the function of the structure in the linkedList class.

    
asked by Jacobo Córdova 11.05.2017 в 01:30
source

1 answer

1
linkedList.node * linkedList::stringALista(

node is a nested structure that is within class linkedList . The operator . is used to access own functionality of a specific instance of the class and this is not the case ... the structure node does not belong to an instance of linkedList , then the operator to use is :: . In addition, the stringALista method belongs to the class node , then it is necessary to indicate that scope as well:

linkedList::node * linkedList::node::stringALista(

On the other hand, this is wrong:

while ( ( s + i * sizeof(char)) != '
char* ptr1 = 0;
std::cout << (void*)(ptr1+1); // imprime 1

int* ptr2 = 0;
std::cout << (void*)(ptr2+1); // imprime 4
' )

s is a pointer and the compiler is smart enough to apply appropriate offsets to that pointer. So:

while ( ( s + i ) != '
for( int i=0; s[i] != '
for( int i=0; s[i]!='
linkedList.node * linkedList::stringALista(
'; i++ ) { if ( cur == NULL) { cur = new node(s[i],0); aux = cur; } else { cur->next = new node(s[i],0); cur = cur->next; } }
'; i++ )
' ) // opción 1 while ( s[i] != '
linkedList::node * linkedList::node::stringALista(
' ) // opción 2

In pointer arithmetic you should not consider the size of each pointer record. That is a responsibility of the compiler. Therefore, that line should look more like this:

while ( ( s + i * sizeof(char)) != '
char* ptr1 = 0;
std::cout << (void*)(ptr1+1); // imprime 1

int* ptr2 = 0;
std::cout << (void*)(ptr2+1); // imprime 4
' )

In any case, note that you are not increasing the value of i so the loop will repeat indefinitely.

For loops with a defined range a loop for

is usually more practical and clean
while ( ( s + i ) != '
for( int i=0; s[i] != '
for( int i=0; s[i]!='%pre%'; i++ )
{
  if ( cur == NULL)
  {
    cur = new node(s[i],0);
    aux = cur;
  }
  else 
  {
    cur->next = new node(s[i],0);
    cur = cur->next;
  }
}
'; i++ )
' ) // opción 1 while ( s[i] != '%pre%' ) // opción 2

Also, the content of the loop has certain problems. If you notice you will see that the loop will create as many nodes as characters have the string s and in each node you try to store a pointer to the string , when what you want is to store each individual character. The loop should look like this:

%pre%     
answered by 11.05.2017 / 07:13
source