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

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

[B]struct[/B] employee salesdept[10];

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

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

[B]int[/B] show_employee_data([B]void[/B])
{
   [B]struct[/B] employee *ptr_sales;

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

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.

Recommended Answers

All 7 Replies

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

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.

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!

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

Thanks

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];

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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.