Interseba5
Interseba5

Reputation: 27

Can't include struct in another file

I'm making a multiple file project in c++. I have this code:

lista.h

struct elem
{
    account info;
    elem* next;
};

typedef elem* lista;

The error is showed here, were "lista* a" is declared.

login.h:

struct account
{
    string user = "";
    int hash_pass = 0;
};

struct list
{
   lista* a;
   int size;
};

login.cc:

#include "login.h"
#include "lista.h"
....

lista.cc

#include "login.h"
#include "lista.h"
....

In lista.cc and login.cc i have included login.h and lista.h but in login.h doesnt recognize lista as name of a type.

Upvotes: 0

Views: 1019

Answers (4)

fractals
fractals

Reputation: 856

Circular dependency! Assuming string type is well-defined somewhere else in the header files (maybe std::string?), this is because you included the files in the wrong order.

#include "login.h"
#include "lista.h" 
....

This is basically equivalent to:

struct account
{
    string user = "";
    int hash_pass = 0;
};

struct list
{
   lista* a;
   int size;
};

struct elem
{
    account info;
    elem* next;
};

typedef elem* lista;
....

As you can see, lista appears even before the typedef, which is why you're getting an error.

Obviously you do not want to care about in which order you are including the header files, so the right solution here would be to include lista.h in login.h with proper header guards. But that is not enough in this case: there is a circular dependency here, as lista.h needs struct account from login.h and login.h needs lista from lista.h. Therefore, we add a forward declaration as well. See this link for more info. Your final code would then be:

lista.h:

#ifndef LISTA_H_
#define LISTA_H_
struct account; // forward declaration

struct elem
{
    account* info; // notice that `account` now has to be a pointer
    elem* next;
};

typedef elem* lista;
#endif

login.h :

#ifndef LOGIN_H_
#define LOGIN_H_
#include "lista.h"

struct account
{
    string user = "";
    int hash_pass = 0;
};

struct list
{
   lista* a;
   int size;
};
#endif

Upvotes: 3

Jabberwocky
Jabberwocky

Reputation: 50775

Your problem boils down to this:

struct elem
{
  account info;   // <<< account is not known here
  elem* next;     // elem is not known here
};

typedef elem* lista;   

struct account
{
  std::string user = "";
  int hash_pass = 0;
};

struct list
{
  lista* a;
  int size;
};    

typedef elem* lista;

If you correct the order of declarations it compiles fine:

struct account
{
  std::string user = "";
  int hash_pass = 0;
};

struct elem
{
  account info;
  elem* next;
};

typedef elem* lista;

struct list
{
  lista* a;
  int size;
};

Upvotes: 0

Prodigle
Prodigle

Reputation: 1797

Include lista.h in login.h as you your login header needs access to lista :)

Upvotes: 0

Ricardo Costeira
Ricardo Costeira

Reputation: 3331

If you want to use something declared on A.h inside B.h, you need to include A.h in B.h. Hence, need to include lista.h in login.h.

Upvotes: 1

Related Questions