| | |
Having trouble trying to use a global structure
Thread Solved |
•
•
Join Date: Mar 2005
Posts: 22
Reputation:
Solved Threads: 0
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
then in my tables.cpp file I try to initialize these structures
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.
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.
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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!!
this may work / may not, baring in mind I've not played with structs much I prefare class's much easier!!
C Syntax (Toggle Plain Text)
: #include <iostream> ... using namespacestd; #include "tables.cpp" .....//rest of code....
For starters, I'd recommend putting the following into a header, say "tables.h". Then in "tables.cpp" have this. And then change "main.cpp" to this.
struct employee
{
char name[20];
int schedule;
int level;
};
extern struct employee salesdept[10];#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);
}
// ...
}#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
![]() |
Similar Threads
- Trouble message winsock.scr (Viruses, Spyware and other Nasties)
- rundll32 trouble (Viruses, Spyware and other Nasties)
Other Threads in the C Forum
- Previous Thread: Enum, struct, and pointer?
- Next Thread: Confusion in string handling?
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






