Home > Enterprise >  Different structure names but same structure variables - How to make each structure variable unique?
Different structure names but same structure variables - How to make each structure variable unique?

Time:01-18

I have 4 files. 3 of them are headers and one of it is something you would call a main handler. These are the content of the file that are causing the problem.

main.cpp

#include "NPSJF.cpp"
#include "PSJF.cpp"
#include "RR.cpp"


int main(){
NPSJFmain();
PSJFmain();
RRmain();

return 0;
}

NPSJF.cpp

//Created struct to store the values
struct process{
    char pid[50];                                                                                       //declare variables 
    int arrival_time;
    int burst_time;
}a[50]

PSJF.cpp

struct node {
    char pname;
    int btime;
    int atime;
    int ctime=0;
    int wtime;
}a[1000],b[1000],c[1000]

RR.cpp

struct pro{
    char process;
    int burst;
    int arrival;
    int ctime=0;
    int wait=-1;
}a[100],b[100],c[100];

All of them has the same structure variable, but they have different structure names. Compiling and running the main.cpp would show the error :

PSJF.cpp [Error] conflicting declaration 'node a [1000]'

NPSJF.cpp [Note] previous declaration as 'process a [50]'

I've tried putting typedef infront of each structure like so.

NPSJF.cpp

//Created struct to store the values
typedef struct process{
    char pid[50];                                                                                       //declare variables 
    int arrival_time;
    int burst_time;
}a[50];

and for the other files but it didn't work.

CodePudding user response:

The reason the “something” you read says you can declare different objects with the same name in different “.cpp” files but not “.h” files is that we use different “.cpp” files in separate compilations. Files named “.cpp” are used as primary source code files, whereas files named “.h” are used as header files. Source code files are used to separate source code, whereas header files are used to publish common things, thus connecting source code.

To do this correctly, you need to compile each source code file separately, making an object file. Then you link the object files together to make an executable file. If you have not done this before, you may need to learn new commands for compiling and linking, or you may need to learn new arrangements in your IDE (Integrated Development Environment) software.

In each source code file, you will need to put one group of functions and data that work together. Separate groups will go into each source code file. The header file associated with each source code file will declare only the things that are defined inside the source code file but intended to be used from outside of it, in other source code files. Each source code file will include the header files that provides the declarations it needs.

Further, when you define objects outside of any function that are not intended to be used from other source files, you can declare them static to prevent the linker from attempting to link them with other objects of the same name in other files. There are other C features for this, too, and you also should avoid declaring objects outside of functions. There are other design patterns that are better for various reasons.

CodePudding user response:

you cannot have 3 things with the same name in scope at the same time (even if they are different types), you must give them different names , like this

struct pro{
    char process;
    int burst;
    int arrival;
    int ctime=0;
    int wait=-1;
}pro_a[100],pro_bb[100],pro_c[100];

BTW, file names make no difference (.h.cpp. foo. bar)

  • Related