0

I've been working on a project for the past few days that involves three linked lists.

Here is an example of the header with the nodes and the lists themselves:

class nodeA
{
   int data1;
   double data2;


    nodeA* next;

}
class listA
{
    nodeA* headA;
    ...
}
class nodeB
{
   int data3;
   double data4;


    nodeB* nextB;

}
class listB
{
    nodeB* headB;
    ...
}
class nodeC
{
   int data5;
   double data6;


    nodeC* nextC;

}
class listC
{
    nodeC* headC;
    ...
}

What i'd like to know is how can i save the lists that i declare in my main so that if i close the program and then open it again i can recover the lists data

josilva00
  • 35
  • 5
  • Please copy the literal error message as text into your question. This way the answers you might get are not only useful to you but also others (and the error is readable for everyone without clicking the link). Also try to provide an [MWE](https://stackoverflow.com/help/minimal-reproducible-example) to increase the chance of receiving helpful answers. – bitmask May 06 '20 at 11:30
  • my IDE doesnt allow me to copy every error, i have to copy one by one – josilva00 May 06 '20 at 11:44

2 Answers2

0

This won't help you with your concrete problem at hand but I think you should use standard containers like std::vector<>. Implementing your own linked list is a nice finger exercise but seldom really necessary. That said, you should use std::vector<no_produto> instead of lista_produto:

#include <vector>

std::vector<no_produto> my_lista_produto;

// fill the vector:
my_lista_produto.push_back(my_no_produto_1);
my_lista_produto.push_back(my_no_produto_2);
// ...

// retrieve one item:
const no_produto &p = my_lista_produto[1];

// clear all items:
my_lista_produto.clear();

A complete list of all available methods can be found here.

Concerning your question: The question title mentions inheritance but there isn't any inheritance used in your example. In order to derive class B from class A you have to write

class A {
public:
    void func();
};

class B : public A {
public:
    void gunc();
};

This means essentially, B can be treated as an A. B contains the content of A and exposes the public interface of A by itself. Thus we can write:

void B::gunc() {
    func();
}

even though B never defines the method func(), it inherited the method from A. I suspect, that you didn't inherit your classes properly from each other.

In addition to my initial thoughts about writing you own linked lists, please consider also composition instead of inheritance. You can find more information about the topic at Wikipedia or on Stack Overflow.

phlipsy
  • 2,899
  • 1
  • 21
  • 37
0

So lista_vendas::novaVenda needs to call lista_produto::escolheProduto.

To call lista_produto::escolheProduto you need a lista_produto object. Where are you going to get that lista_produto object from?

There's really only three ways this can be done. I don't know which way is the correct way for you, but I'll list them and you can decide.

1) Have a lista_produto global variable, a global list of products. Then lista_vendas::novaVenda can use the global lista_produto to call lista_produto::escolheProduto. This is the simple solution, but global variables are rightly considered bad style. It also means that you program can only have one list of products, is that a problem? Think carefully before trying this solution.

2) Have a lista_produto as a member variable of lista_vendas or no_vendas. I guessing here but perhaps something like this

class no_vendas
{
public:
    unsigned int codigoVenda, dia, mes, ano, numeroItens;
    double precoTotal;
    lista_produto productList; // list of products
    no_vendas* proxi; //referencia para o proximo no
};

Now each vendor has a list of products, which makes sense. So lista_vendas::novaVenda now has access to a lista_produto in each no_vendas and it can use that to call lista_produto::escolheProduto. If this makes sense then this is problably the best solution.

3) Pass a lista_produto as a parameter to lista_vendas::novaVenda. Like this

void novaVenda(unsigned int codigoVenda, lista_produto* productList);

So whatever code calls lista_vendas::novaVenda must also supply the lista_produto that it needs.

As I said I don't know which of these possibilities is correct, because I don't know what you are trying to do (and I don't speak Spanish). But this is a problem in the relationships between your different objects. It's up to you to design your classes so that they can access the different objects that they need to work.

You mentioned inheritance in your title, but this doesn't feel like the right thing to do in this case.

john
  • 85,011
  • 4
  • 57
  • 81
  • I have tried your third way, but now i get new errors, i've edited the post so you can understant better what i'm trying to do. – josilva00 May 06 '20 at 11:28
  • Well, it would help to know what the errors are. I can see this `void novaVenda(unsigned int codigoVenda,lista_produto* aux, lista_itens_vendas* aux1);` novaVende has **three** parameters and this `cout<<"Type a new sales code:"; cin>>cV; b.novaVenda(cV);` novaVende is being called with **one** parameter. That's definitely an error, is that the new error you are talking about? – john May 06 '20 at 12:11
  • Sorry, i´ve edited the post, but i still get errors, the errors are on the link to the image at the bottom – josilva00 May 06 '20 at 12:20
  • It's simple enough, your declarations are in the wrong order, you use `lista_itens_vendas` before you declare it. You must declare classes **before** you use them. Move `class lista_itens_vendas` so that it is before `class lista_vendas` in the header file. – john May 06 '20 at 13:23
  • manage to lower the number of errors , but i this get one, updated the image with the errors – josilva00 May 06 '20 at 13:34
  • You've declared `novaVenda` like this `void novaVenda(unsigned int codigoVenda,lista_produto* aux, lista_itens_vendas* aux1);`. The second and third parameters are pointers. You've called it like this `b.novaVenda(cV,b,d);` the second and third parameters are **not** pointers. This would compile `b.novaVenda(cV,&b,&d);` – john May 06 '20 at 14:19
  • If you read the error message carefully, this is is exactly what it is telling you. – john May 06 '20 at 14:19
  • Still gives me this error:|108|error: no matching function for call to 'lista_vendas::novaVenda(unsigned int&, lista_vendas*, lista_itens_vendas*)'| – josilva00 May 06 '20 at 14:20
  • @josilva00 So look carefully, what types have you declared the function with, and what types have you called the function with? I'm sure you can work it out. – john May 06 '20 at 14:22
  • i'm not following, could you elaborate? – josilva00 May 06 '20 at 14:23
  • Really? Declaration `void novaVenda(unsigned int codigoVenda,lista_produto* aux, lista_itens_vendas* aux1);` second parameter is `lista_produto*`. Call `b.novaVenda(cV,&b,&d);` second parameter is `lista_vendas*`. – john May 06 '20 at 14:25
  • Probably it should be `b.novaVenda(cV,&a,&d);`. – john May 06 '20 at 14:28
  • Thanks, solved that problem but now i have another, but i will try to solve it alone:) – josilva00 May 06 '20 at 14:33