Struct with Union is giving invalid usage error

Asked

Viewed 68 times

1

I need to use a union within a struct, but I’m not getting it. The error presented is

[Error] invalid use of 'Union Client::document'

Struct:

struct Cliente{
       string nome, email;
       union documento{
             long cpf,cnpj;
       };
};

Function:

void cadastrarCliente(Cliente *cli){
     fstream aCliente;
     aCliente.open("clientes.txt", ios::out|ios::app); 
     aCliente << cli->documento << " " << 
                 cli->nome << " " << 
                 cli->email << "\n";
     aCliente.close();         
}
  • Which makes you conclude that Cliente::documento is a data member and not a definition of union?

  • Hi Mario! My question is basically in this. I did a validation before but presented the same error: cli->document.Cpf or cli->document->Cpf or cli->document::Cpf gave the same error. I started to see this language this semester, and I still have some doubts (this particular one I did not find examples anywhere).

1 answer

0


First of all, you don’t seem to need a union. CNPJ and CPF are descriptive data and should be strings and not numbers, which already kills the need for union.

union with two fields of the same type does not make much sense. Just to use a different name? I see no advantage in C. C++ has better mechanisms. Anyway, it’s not even what you need.

One union without tag, one of the criticisms of the C mechanism, is not good for much, so C++ has better structures than union in the library if necessary. But of course you can always use a tag own extra on own struct to know what to take in each case, only has the problem that this is an abstraction leak.

union was created to solve issues of lower level, it seems to me that is mixing different levels of abstraction.

For all this I prefer not to even try to solve your specific problem and solve the real problem that is wider. Solving the question problem is a palliative that will still keep wrong in the general concept.

If you do it right, you’ll only have room for string of the CNPJ which is larger, and of course, a tag extra that seems to me fundamental anyway in this register. This is a case that put the inline string in struct seems appropriate.

I strongly question whether I should use a struct for a Cliente. But okay, it’s just a start, and technically it will change little use to a class, it will just give a more appropriate semantics, and it will indicate more that this should probably be a type by reference. But I won’t insist on that because right now it’s too simple a code.

That would be a better solution:

struct Cliente {
    string nome, email, tipo, documento;
};

I put in the Github for future reference.

There are other issues that probably indicate that the code is mixing C with C++ and is the last century’s way of doing in C++.

  • Vlw by the attention, Maniero. I know that Union is something outdated and I intend to never use it again, but it is a college exercise. So since I really need to use Union, you can’t use string.

  • So use it on something that needs union, not that case, use where you don’t need I already consider a wrong exercise. Find a problem that needs to union, This one doesn’t need to. I won’t teach anyone to do anything wrong. If some teacher told you to do it wrong, I’m sorry, and I hope someone else gives you an answer, but I don’t cooperate with the mistake. Even if I use it, I have cited several other errors, including related to union. What I can do is give you some things that I answered so you can see that the union is completely wrong: https://answall.com/q/137393/101e ...

  • https://answall.com/q/180783/101

  • Grateful for the attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.