954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Having trouble trying to use a global structure

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

<strong>struct</strong> employee
{
   <strong>char</strong> name[20];
   <strong>int</strong> schedule;
   <strong>int</strong> level;
};

<strong>struct</strong> employee salesdept[10];


then in mytables.cpp file I try to initialize these structures

<strong>extern struct </strong> employee salesdept[10];  <em>// forward declaration of `struct characters'</em>

<strong>int</strong> show_employee_data(<strong>void</strong>)
{
   <strong>struct</strong> employee *ptr_sales;

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


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.

Auto
Light Poster
33 posts since Mar 2005
Reputation Points: 11
Solved Threads: 1
 

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!!

:
#include <iostream>
...

using namespacestd;

#include "tables.cpp"

.....//rest of code....
Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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.

Auto
Light Poster
33 posts since Mar 2005
Reputation Points: 11
Solved Threads: 1
 

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!

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

Okay, I'll try that after I get back from work, hopefully that will clear something up.

Thanks

Auto
Light Poster
33 posts since Mar 2005
Reputation Points: 11
Solved Threads: 1
 

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];
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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?

Auto
Light Poster
33 posts since Mar 2005
Reputation Points: 11
Solved Threads: 1
 

Many thanks Dave, compiled everything okay and I was able to run the .exe with an expected output.

Auto
Light Poster
33 posts since Mar 2005
Reputation Points: 11
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You