-1

I have this code at the beginning :

#include<iostream>

using namespace std;

struct mehmandar{

    char name[30];
    char last_name[30];
    long national_code;
    long personal_code;
    date birthday;
    date employ_date;
    mehmandar * nxt;
};

struct time{

    int hour;
    int min;
};


struct flight{

    long flight_serial;
    long plane_serial;
    char from[30];
    char to[30];
    int traveller_num;
    char pilot_name[30];
    char pilot_lastnam[30];
    mehmandar* mehmandar_majmue=NULL;
    long total_price;
    time flight_time;
    flight * nxt;
};

int main() {

    flight* temp=new flight;

    cin >>temp->flight_time.hour;

    return 0;
}

but then I get the error that struct flight has no member flight_time for main part and time does not name a type in the struct flight part.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 2
    Why are you heap-allocating the object when you could just say `flight temp;`? – cdhowie Apr 12 '17 at 15:46
  • 4
    What happens if you get rid of `using namespace std;`? – NathanOliver Apr 12 '17 at 15:46
  • 2
    Try to rename your type `time` to something like `time_type`. – KonstantinL Apr 12 '17 at 15:48
  • I had to define `date` to get it to compile, but then it was OK. I'm using Visual Studio. Try defining `date`? – Topological Sort Apr 12 '17 at 15:49
  • 3
    Also, look at the compiler output. You should see some lines like this: `error: must use 'struct' tag to refer to type 'time' in this scope. note: struct 'time' is hidden by a non-type declaration of 'time' here: extern time_t time (time_t *__timer) __THROW;` Probably a bad idea to name your struct the same as a function provided by the environment. – cdhowie Apr 12 '17 at 15:49
  • because I want to make a linked list and use dynamic memory allocation – Leila Saeedy Apr 12 '17 at 15:49
  • `time` is actually a [function](http://www.cplusplus.com/reference/ctime/time/). When trying to compile with GCC on Linux I needed to rename `time` to something else for it to work. – Gizmo Apr 12 '17 at 15:51
  • this is one of the reasons to use names like Date and Time for c++ classes and structs - to prevent collision with existing names – pm100 Apr 12 '17 at 15:54
  • https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Galik Apr 12 '17 at 16:32

1 Answers1

3

Don't use time as a global type. It conflicts with the standard library function of the same name.

Change it to my_time or put it in your own namespace. Example:

#include<iostream>

namespace MyApp
{
   struct time
   {
      int hour;
      int min;
   };

   struct flight
   {
      long flight_serial;
      long plane_serial;
      char from[30];
      char to[30];
      int traveller_num;
      char pilot_name[30];
      char pilot_lastnam[30];
      long total_price;
      time flight_time;
      flight * nxt;
   };
}

int main()
{
   using namespace MyApp;
   flight* temp=new flight;

   std::cin >>temp->flight_time.hour;

   return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270