Having trouble trying to use a global structure

Thread Solved

Join Date: Mar 2005
Posts: 22
Reputation: Auto is an unknown quantity at this point 
Solved Threads: 0
Auto Auto is offline Offline
Newbie Poster

Having trouble trying to use a global structure

 
0
  #1
Mar 28th, 2005
After fixing some pointer problems with my structures, I decided to move the functions performed on them to another .cpp file.

Here's the definition in my main.cpp above the int main() of course

struct employee
{
   char name[20];
   int schedule;
   int level;
};

struct employee salesdept[10];

then in my tables.cpp file I try to initialize these structures

extern struct  employee salesdept[10];  // forward declaration of `struct characters'

int show_employee_data(void)
{
   struct employee *ptr_sales;

   ptr_sales=&salesdept[0];
   for(int i=0; i<10; i++)
      {
      printf("Employee: %s\n",ptr_sales->name); // invalid use of undefined type `struct characters'
      printf("Shift:  %d\n",ptr->schedule); // invalid use of undefined type `struct characters'
      printf("Level: %d\n\n\n",ptr->level); // invalid use of undefined type `struct characters'
      }
   // more unrelated code follows, mostly text displaying
}

I don't think this is a linking problem, because if I comment out the three printf() statements show_employee_data() the rest of the show_employee_data() runs and displays on the screen(not shown in my code above).

By the way in main.cpp I'm currently just assigning constants to the structures just to try and make the show function work.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Having trouble trying to use a global structure

 
0
  #2
Mar 28th, 2005
umm correct me if im wrong but in main as no reference to the other cpp file? should you not want to include it?

this may work / may not, baring in mind I've not played with structs much I prefare class's much easier!!

  1. :
  2. #include <iostream>
  3. ...
  4.  
  5. using namespacestd;
  6.  
  7. #include "tables.cpp"
  8.  
  9. .....//rest of code....
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 22
Reputation: Auto is an unknown quantity at this point 
Solved Threads: 0
Auto Auto is offline Offline
Newbie Poster

Re: Having trouble trying to use a global structure

 
0
  #3
Mar 28th, 2005
I tried using #include"tables.cpp" and got the same errors. I'm using a project manager that links them, so I'm not sure if I need to place it there, but I'll leave it there just in case.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Having trouble trying to use a global structure

 
0
  #4
Mar 28th, 2005
Why dont you put everything in 1 .cpp file and see if it makes a differece? If so then you are missing a few includes of files???

Just a random thought!
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 22
Reputation: Auto is an unknown quantity at this point 
Solved Threads: 0
Auto Auto is offline Offline
Newbie Poster

Re: Having trouble trying to use a global structure

 
0
  #5
Mar 28th, 2005
Okay, I'll try that after I get back from work, hopefully that will clear something up.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,348
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Having trouble trying to use a global structure

 
0
  #6
Mar 28th, 2005
For starters, I'd recommend putting the following into a header, say "tables.h".
struct employee
{
   char name[20];
   int schedule;
   int level;
};

extern struct employee salesdept[10];
Then in "tables.cpp" have this.
#include "tables.h"

int show_employee_data(void)
{
   struct employee *ptr_sales;

   ptr_sales=&salesdept[0];
   for(int i=0; i<10; i++)
      {
      printf("Employee: %s\n",ptr_sales->name);
      printf("Shift:  %d\n",ptr->schedule);
      printf("Level: %d\n\n\n",ptr->level);
      }
   // ...
}
And then change "main.cpp" to this.
#include "tables.h"

struct employee salesdept[10];
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 22
Reputation: Auto is an unknown quantity at this point 
Solved Threads: 0
Auto Auto is offline Offline
Newbie Poster

Re: Having trouble trying to use a global structure

 
0
  #7
Mar 28th, 2005
Okay, thanks Dave I'll start there when I get off work. But I do have one question pertaining to your recommendation. Do I need to prototype show_employee_data() in main() or is one not needed for .h because it's handled differently?
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 22
Reputation: Auto is an unknown quantity at this point 
Solved Threads: 0
Auto Auto is offline Offline
Newbie Poster

Re: Having trouble trying to use a global structure

 
0
  #8
Mar 29th, 2005
Many thanks Dave, compiled everything okay and I was able to run the .exe with an expected output.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC