Thread: redefinition of 'main' error

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    159

    redefinition of 'main' error

    Hi

    I have tow files with each one int main() function, the second file has an header file, when i compile i have this error

    redefinition of 'main' error

    I can't use main in both source files??


    Regards

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    A single program can only have one entry point, therefore one main.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Thanks;

    So how can the main program can call the source file added? functions??

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Presumably the "second" source file added contains implementations of the functions defined in its corresponding header file. If so, it is sufficient to include this header in the source file containing the "main". Then you will need to compile your source files and "build" the program, meaning you need to tell the linker where your different object files are so that it can link them together in one executable. If you are using an IDE, chances are it will do that for you, provided that your files are in the same project.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi

    This is my header
    Code:
    #ifndef GP2_H_
    #define GP2_H_
    struct gp2
    {
      int id;
      char ques[200];
      char res[30];
    };
    void ad_perguntasgp2();
    void ver_perguntasgp2();
    void edit_perguntasgp2();
    void remov_perguntasgp2();
    #endif /* GP2_H_ */
    and this my source file(2);
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #include "gp2.h"
    
    void menu_adgp2();
    
    
    //Estrutar do grupo de perguntas 2
    
    //funcao para adcionar perguntas num ficheiro
    void ad_perguntasgp2(struct gp2 *p)
    {
        char buf[30];
        FILE *f;
        if ((f = fopen("gp2.dat", "ab+")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        else
        {
            //Lê do teclado e grava no ficheiro
            printf("Introduza o ID da pergunta \n");
            fgets(buf, 30, stdin);
            sscanf(buf,"%d", &p->id);
            printf("Introduza a afirmacao: \n");
            fgets(p->ques,200, stdin);
            printf("Introduza resposta certa (marque com # + ficcao ou facto) \n");
            fgets(p->res,30, stdin);
            fwrite(p, sizeof(struct gp2), 1 , f);
            fclose(f);
        }
    }
    
    void ver_perguntasgp2(struct gp2 *p)
    {
        int retorno, cont = 0;
        FILE *f;
        if ((f= fopen("gp2.dat", "rb")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        retorno = fread(p, sizeof *p, 1, f);
        while ( retorno == 1)
        {
            cont++;
            printf("\nId : %d \n", p->id);
            printf("\nPergunta : %s", p->ques);
            printf("\nResposta Correta : %s",p->res);
            printf("\n");
            retorno= fread(p, sizeof *p, 1, f);
        }
        printf(" \n\n %d perguntas registadas \n", cont);
        fclose(f);
    }
    
    void edit_perguntasgp2(struct gp2 *p)
    {
        int recno, nofrec=0;
        char ch;
        char buf[100];
        FILE *fp;
        fp=fopen("perguntas.dat", "rb+");
        printf("Introduza o Id a modificar : ");
        fgets(buf, sizeof buf, stdin);
        sscanf(buf, "%d", &recno);
        while(fread(p, sizeof *p, 1, fp))
        {
            nofrec++;
            printf("sizeof(p) = %d; sizeof *p = %d\n", (int)sizeof(p), (int)sizeof *p);
            if(p->id==recno)
            {
                printf("\nId : %d \n", p->id);
                printf("\nPergunta : %s", p->ques);
                printf("\nResposta Correta : %s",p->res);
                printf("\n");
                printf("Quer modificar este registo: ? (S/N)");
                fgets(buf, sizeof buf, stdin);
                sscanf(buf, "%c", &ch);
                fseek(fp, (nofrec - 1) * sizeof *p, SEEK_SET);
                if(ch=='S'|| ch=='s')
                {
                    printf("Introduza a pergunta: \n");
                    fgets(p->ques,200, stdin);
                    printf("Introduza resposta certa (marque com # + ficcao ou facto) \n");
                    fgets(p->ques,200, stdin);
                    fwrite(p, sizeof *p, 1, fp);
                    printf("Registo modificado \n");
                }
                else
                {
                    printf("Sem modificacoes efetuadas \n");
                }
                fclose(fp);
            }
        }
    }
    void remov_perguntasgp2(struct gp2 *p)
    {
        int id_reg;
        FILE *orig_file, *temp_file;
        temp_file=fopen("temp.dat", "wb");
        if ((orig_file= fopen("perguntas.dat", "rb")) == NULL)
        {
            printf("Erro ao abrir ficheiro \n");
        }
        printf("Introduza o Id a apagar: \n");
        scanf("%d",&id_reg);
        while(fread(p, sizeof *p, 1, orig_file));
        {
            if(id_reg == (*p).id)
            {
            }
            else
            {
                fwrite(p,sizeof *p,1,temp_file);
                printf("Registo apagado \n");
            }
        }
        fclose(orig_file);
        fclose(temp_file);
        remove("perguntas.dat");
        rename("temp.dat", "perguntas.dat");
        return;
    }
    
    
    void menu_adgp2()
    {
        struct gp2 *p;
        int x;
        do
        {
            printf("1 - Inserir perguntas: \n");
            printf("2 - Ver as perguntas: \n");
            printf("3 - Editar as perguntas: \n");
            printf("4 - Apagar as perguntas: \n");
            scanf("%d", &x);
            getchar();
            switch (x)
            {
            case 1:
                ad_perguntasgp2(p);
                break;
            case 2:
                ver_perguntasgp2(p);
                break;
            case 3:
                edit_perguntasgp2(p);
                break;
            case 4:
                remov_perguntasgp2(p);
                break;
            default:
                printf("Escolha invalida");
                break;
            }
        }
        while ( x != 5 );
    }
    int menu(void)
    {
        printf("Menu Grupo 2 \n");
        putchar('\n');
        menu_adgp2();
        system("pause");
        return 0;
    }
    i have several errors

    Description Resource Path Location Type
    redefinition of ‘ad_perguntasgp2’ gp2.c /Buzz1/src line 21 C/C++ Problem
    redefinition of ‘edit_perguntasgp2’ gp2.c /Buzz1/src line 66 C/C++ Problem
    redefinition of ‘menu_adgp2’ gp2.c /Buzz1/src line 137 C/C++ Problem
    redefinition of ‘menu’ gp2.c /Buzz1/src line 170 C/C++ Problem
    redefinition of ‘remov_perguntasgp2’ gp2.c /Buzz1/src line 107 C/C++ Problem
    redefinition of ‘ver_perguntasgp2’ gp2.c /Buzz1/src line 44 C/C++ Problem

    don't know why....

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The header file and the source file should(maybe must) define each function the same way.

    From header
    Code:
    void ad_perguntasgp2();
    From source file

    Code:
    void ad_perguntasgp2(struct gp2 *p)
    The source file says it takes a single parameter of type "struct gp2".
    This does not match the header file.

    Change the header file line to
    Code:
    void ad_perguntasgp2(struct gp2 *p);
    Tim S.
    Last edited by stahta01; 05-16-2012 at 11:16 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Thanks for your help but it's the same

    I changed to.

    Code:
    /*
     * gp2.h
     *
     *  Created on: 15 de Mai de 2012
     *      Author: gil
     */
    
    #ifndef GP2_H_
    #define GP2_H_
    
    struct gp2
    {
      int id;
      char ques[200];
      char res[30];
    };
    
    
    void ad_perguntasgp2(struct gp2 *p);
    void ver_perguntasgp2(struct gp2 *p);
    void edit_perguntasgp2(struct gp2 *p);
    void remov_perguntasgp2(struct gp2 *p);
    
    #endif /* GP2_H_ */
    And i still have the same errors

    Description Resource Path Location Type
    redefinition of ‘ad_perguntasgp2’ gp2.c /Buzz1/src line 14 C/C++ Problem
    redefinition of ‘edit_perguntasgp2’ gp2.c /Buzz1/src line 59 C/C++ Problem
    redefinition of ‘menu_adgp2’ gp2.c /Buzz1/src line 130 C/C++ Problem
    redefinition of ‘menu’ gp2.c /Buzz1/src line 163 C/C++ Problem
    redefinition of ‘remov_perguntasgp2’ gp2.c /Buzz1/src line 100 C/C++ Problem
    redefinition of ‘ver_perguntasgp2’ gp2.c /Buzz1/src line 37 C/C++ Problem

    Any sugestions??

    Thanks

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Is that all the messages your are getting from the Compiler/Linker?

    What source files are you NOT us showing the code?

    Edit: I am starting to believe the problem is being caused by a file; for which the source code has not been posted.

    Tim S.
    Last edited by stahta01; 05-16-2012 at 11:49 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Try getting rid of this warning

    Code:
    redefinition of ‘menu_adgp2’ gp2.c /Buzz1/src line 130 C/C++ Problem
    First change the prototype to

    Code:
    void menu_adgp2(void);
    And the function header to
    Code:
    void menu_adgp2(void)
    {
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    I have changed now to

    header

    Code:
    /*
     * gp2.h
     *
     *  Created on: 15 de Mai de 2012
     *      Author: gil
     */
    
    #ifndef GP2_H_
    #define GP2_H_
    
    struct gp2
    {
      int id;
      char ques[200];
      char res[30];
    };
    void menu_adgp2(void)
    {
    void ad_perguntasgp2(struct gp2 *p);
    void ver_perguntasgp2(struct gp2 *p);
    void edit_perguntasgp2(struct gp2 *p);
    void remov_perguntasgp2(struct gp2 *p);
    }
    
    #endif /* GP2_H_ */
    c file

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #include "gp2.h"
    
    void menu_adgp2();
    
    
    //Estrutar do grupo de perguntas 2
    
    //funcao para adcionar perguntas num ficheiro
    void ad_perguntasgp2(struct gp2 *p)
    {
        char buf[30];
        FILE *f;
        if ((f = fopen("gp2.dat", "ab+")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        else
        {
            //Lê do teclado e grava no ficheiro
            printf("Introduza o ID da pergunta \n");
            fgets(buf, 30, stdin);
            sscanf(buf,"%d", &p->id);
            printf("Introduza a afirmacao: \n");
            fgets(p->ques,200, stdin);
            printf("Introduza resposta certa (marque com # + ficcao ou facto) \n");
            fgets(p->res,30, stdin);
            fwrite(p, sizeof(struct gp2), 1 , f);
            fclose(f);
        }
    }
    
    void ver_perguntasgp2(struct gp2 *p)
    {
        int retorno, cont = 0;
        FILE *f;
        if ((f= fopen("gp2.dat", "rb")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        retorno = fread(p, sizeof *p, 1, f);
        while ( retorno == 1)
        {
            cont++;
            printf("\nId : %d \n", p->id);
            printf("\nPergunta : %s", p->ques);
            printf("\nResposta Correta : %s",p->res);
            printf("\n");
            retorno= fread(p, sizeof *p, 1, f);
        }
        printf(" \n\n %d perguntas registadas \n", cont);
        fclose(f);
    }
    
    void edit_perguntasgp2(struct gp2 *p)
    {
        int recno, nofrec=0;
        char ch;
        char buf[100];
        FILE *fp;
        fp=fopen("perguntas.dat", "rb+");
        printf("Introduza o Id a modificar : ");
        fgets(buf, sizeof buf, stdin);
        sscanf(buf, "%d", &recno);
        while(fread(p, sizeof *p, 1, fp))
        {
            nofrec++;
            printf("sizeof(p) = %d; sizeof *p = %d\n", (int)sizeof(p), (int)sizeof *p);
            if(p->id==recno)
            {
                printf("\nId : %d \n", p->id);
                printf("\nPergunta : %s", p->ques);
                printf("\nResposta Correta : %s",p->res);
                printf("\n");
                printf("Quer modificar este registo: ? (S/N)");
                fgets(buf, sizeof buf, stdin);
                sscanf(buf, "%c", &ch);
                fseek(fp, (nofrec - 1) * sizeof *p, SEEK_SET);
                if(ch=='S'|| ch=='s')
                {
                    printf("Introduza a pergunta: \n");
                    fgets(p->ques,200, stdin);
                    printf("Introduza resposta certa (marque com # + ficcao ou facto) \n");
                    fgets(p->ques,200, stdin);
                    fwrite(p, sizeof *p, 1, fp);
                    printf("Registo modificado \n");
                }
                else
                {
                    printf("Sem modificacoes efetuadas \n");
                }
                fclose(fp);
            }
        }
    }
    void remov_perguntasgp2(struct gp2 *p)
    {
        int id_reg;
        FILE *orig_file, *temp_file;
        temp_file=fopen("temp.dat", "wb");
        if ((orig_file= fopen("perguntas.dat", "rb")) == NULL)
        {
            printf("Erro ao abrir ficheiro \n");
        }
        printf("Introduza o Id a apagar: \n");
        scanf("%d",&id_reg);
        while(fread(p, sizeof *p, 1, orig_file));
        {
            if(id_reg == (*p).id)
            {
            }
            else
            {
                fwrite(p,sizeof *p,1,temp_file);
                printf("Registo apagado \n");
            }
        }
        fclose(orig_file);
        fclose(temp_file);
        remove("perguntas.dat");
        rename("temp.dat", "perguntas.dat");
        return;
    }
    
    
    void menu_adgp2(void)
    {
        struct gp2 *p;
        int x;
        do
        {
            printf("1 - Inserir perguntas: \n");
            printf("2 - Ver as perguntas: \n");
            printf("3 - Editar as perguntas: \n");
            printf("4 - Apagar as perguntas: \n");
            scanf("%d", &x);
            getchar();
            switch (x)
            {
            case 1:
                ad_perguntasgp2(p);
                break;
            case 2:
                ver_perguntasgp2(p);
                break;
            case 3:
                edit_perguntasgp2(p);
                break;
            case 4:
                remov_perguntasgp2(p);
                break;
            default:
                printf("Escolha invalida");
                break;
            }
        }
        while ( x != 5 );
    }
    int menu(void)
    {
        printf("Menu Grupo 2 \n");
        putchar('\n');
        menu_adgp2();
        system("pause");
        return 0;
    }
    all the errors

    Description Resource Path Location Type
    make: *** [src/Buzz1.o] Error 1 Buzz1 C/C++ Problem
    redefinition of ‘menu_adgp2’ gp2.c /Buzz1/src line 130 C/C++ Problem
    previous definition of ‘menu_adgp2’ was here gp2.h /Buzz1/src line 17 C/C++ Problem

    void menu_adgp2(void)

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    void menu_adgp2(void)
    {
    void ad_perguntasgp2(struct gp2 *p);
    void ver_perguntasgp2(struct gp2 *p);
    void edit_perguntasgp2(struct gp2 *p);
    void remov_perguntasgp2(struct gp2 *p);
    }
    What are those curly brackets doing in there? That's defining a function in your .h file, which does nothing and only declares 4 function prototypes. Try
    Code:
    void menu_adgp2(void);
    void ad_perguntasgp2(struct gp2 *p);
    void ver_perguntasgp2(struct gp2 *p);
    void edit_perguntasgp2(struct gp2 *p);
    void remov_perguntasgp2(struct gp2 *p);
    EDIT: I see you were trying to implement Tim's suggestion, but he meant the header of the function in the .c file. Slightly confusing terms perhaps.
    Last edited by anduril462; 05-16-2012 at 12:08 PM.

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi

    Still the same, now

    Description Resource Path Location Type
    redefinition of ‘ver_perguntasgp2’ gp2.c /Buzz1/src line 37 C/C++ Problem
    redefinition of ‘remov_perguntasgp2’ gp2.c /Buzz1/src line 100 C/C++ Problem
    redefinition of ‘menu’ gp2.c /Buzz1/src line 163 C/C++ Problem
    redefinition of ‘menu_adgp2’ gp2.c /Buzz1/src line 130 C/C++ Problem
    redefinition of ‘edit_perguntasgp2’ gp2.c /Buzz1/src line 59 C/C++ Problem
    redefinition of ‘ad_perguntasgp2’ gp2.c /Buzz1/src line 14 C/C++ Problem
    make: *** [src/gp2.o] Error 1 Buzz1 C/C++ Problem
    Field 'res' could not be resolved gp2.c /Buzz1/src line 31 Semantic Error
    Field 'res' could not be resolved gp2.c /Buzz1/src line 51 Semantic Error
    Field 'res' could not be resolved gp2.c /Buzz1/src line 77 Semantic Error
    Field 'ques' could not be resolved gp2.c /Buzz1/src line 29 Semantic Error
    Field 'ques' could not be resolved gp2.c /Buzz1/src line 50 Semantic Error
    Field 'ques' could not be resolved gp2.c /Buzz1/src line 76 Semantic Error
    Field 'ques' could not be resolved gp2.c /Buzz1/src line 86 Semantic Error
    Field 'ques' could not be resolved gp2.c /Buzz1/src line 88 Semantic Error
    Field 'id' could not be resolved gp2.c /Buzz1/src line 27 Semantic Error
    Field 'id' could not be resolved gp2.c /Buzz1/src line 49 Semantic Error
    Field 'id' could not be resolved gp2.c /Buzz1/src line 73 Semantic Error
    Field 'id' could not be resolved gp2.c /Buzz1/src line 75 Semantic Error
    Field 'id' could not be resolved gp2.c /Buzz1/src line 113 Semantic Error

    I think i can't "connect" to my structure...and other errors

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #include "gp2.h"
    
    void menu_adgp2();
    
    
    //Estrutar do grupo de perguntas 2
    
    //funcao para adcionar perguntas num ficheiro
    void ad_perguntasgp2(struct gp2 *p)
    {
        char buf[30];
        FILE *f;
        if ((f = fopen("gp2.dat", "ab+")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        else
        {
            //Lê do teclado e grava no ficheiro
            printf("Introduza o ID da pergunta \n");
            fgets(buf, 30, stdin);
            sscanf(buf,"%d", &p->id);
            printf("Introduza a afirmacao: \n");
            fgets(p->ques,200, stdin);
            printf("Introduza resposta certa (marque com # + ficcao ou facto) \n");
            fgets(p->res,30, stdin);
            fwrite(p, sizeof(struct gp2), 1 , f);
            fclose(f);
        }
    }
    
    void ver_perguntasgp2(struct gp2 *p)
    {
        int retorno, cont = 0;
        FILE *f;
        if ((f= fopen("gp2.dat", "rb")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        retorno = fread(p, sizeof *p, 1, f);
        while ( retorno == 1)
        {
            cont++;
            printf("\nId : %d \n", p->id);
            printf("\nPergunta : %s", p->ques);
            printf("\nResposta Correta : %s",p->res);
            printf("\n");
            retorno= fread(p, sizeof *p, 1, f);
        }
        printf(" \n\n %d perguntas registadas \n", cont);
        fclose(f);
    }
    
    void edit_perguntasgp2(struct gp2 *p)
    {
        int recno, nofrec=0;
        char ch;
        char buf[100];
        FILE *fp;
        fp=fopen("perguntas.dat", "rb+");
        printf("Introduza o Id a modificar : ");
        fgets(buf, sizeof buf, stdin);
        sscanf(buf, "%d", &recno);
        while(fread(p, sizeof *p, 1, fp))
        {
            nofrec++;
            printf("sizeof(p) = %d; sizeof *p = %d\n", (int)sizeof(p), (int)sizeof *p);
            if(p->id==recno)
            {
                printf("\nId : %d \n", p->id);
                printf("\nPergunta : %s", p->ques);
                printf("\nResposta Correta : %s",p->res);
                printf("\n");
                printf("Quer modificar este registo: ? (S/N)");
                fgets(buf, sizeof buf, stdin);
                sscanf(buf, "%c", &ch);
                fseek(fp, (nofrec - 1) * sizeof *p, SEEK_SET);
                if(ch=='S'|| ch=='s')
                {
                    printf("Introduza a pergunta: \n");
                    fgets(p->ques,200, stdin);
                    printf("Introduza resposta certa (marque com # + ficcao ou facto) \n");
                    fgets(p->ques,200, stdin);
                    fwrite(p, sizeof *p, 1, fp);
                    printf("Registo modificado \n");
                }
                else
                {
                    printf("Sem modificacoes efetuadas \n");
                }
                fclose(fp);
            }
        }
    }
    void remov_perguntasgp2(struct gp2 *p)
    {
        int id_reg;
        FILE *orig_file, *temp_file;
        temp_file=fopen("temp.dat", "wb");
        if ((orig_file= fopen("perguntas.dat", "rb")) == NULL)
        {
            printf("Erro ao abrir ficheiro \n");
        }
        printf("Introduza o Id a apagar: \n");
        scanf("%d",&id_reg);
        while(fread(p, sizeof *p, 1, orig_file));
        {
            if(id_reg == (*p).id)
            {
            }
            else
            {
                fwrite(p,sizeof *p,1,temp_file);
                printf("Registo apagado \n");
            }
        }
        fclose(orig_file);
        fclose(temp_file);
        remove("perguntas.dat");
        rename("temp.dat", "perguntas.dat");
        return;
    }
    
    
    void menu_adgp2(void)
    {
        struct gp2 *p;
        int x;
        do
        {
            printf("1 - Inserir perguntas: \n");
            printf("2 - Ver as perguntas: \n");
            printf("3 - Editar as perguntas: \n");
            printf("4 - Apagar as perguntas: \n");
            scanf("%d", &x);
            getchar();
            switch (x)
            {
            case 1:
                ad_perguntasgp2(p);
                break;
            case 2:
                ver_perguntasgp2(p);
                break;
            case 3:
                edit_perguntasgp2(p);
                break;
            case 4:
                remov_perguntasgp2(p);
                break;
            default:
                printf("Escolha invalida");
                break;
            }
        }
        while ( x != 5 );
    }
    int menu(void)
    {
        printf("Menu Grupo 2 \n");
        putchar('\n');
        menu_adgp2();
        system("pause");
        return 0;
    }
    The problem must be here since the .h file is ok.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    redefinition of ‘ver_perguntasgp2’ gp2.c /Buzz1/src line 37 C/C++ Problem
    redefinition of ‘remov_perguntasgp2’ gp2.c /Buzz1/src line 100 C/C++ Problem
    redefinition of ‘menu’ gp2.c /Buzz1/src line 163 C/C++ Problem
    redefinition of ‘menu_adgp2’ gp2.c /Buzz1/src line 130 C/C++ Problem
    redefinition of ‘edit_perguntasgp2’ gp2.c /Buzz1/src line 59 C/C++ Problem
    redefinition of ‘ad_perguntasgp2’ gp2.c /Buzz1/src line 14 C/C++ Problem
    These messages imply there are other source files you aren't showing us. Where are they? You need to post them.

    I've never come across the "Field could not be resolve...Semantic error" message before, but Google suggest it is related to Eclipse CDT. Perhaps you have something configured incorrectly in your project (which could also be the source of your redefinition errors). Unfortunately, I don't use Eclipse, so I can't help you there, but somebody else here may know.

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi

    The main file is this

    Code:
    /*
     ============================================================================
     Name        : Trabalho.c
     Author      : gmc
     Version     : 1.0
     Copyright   :
     Description : Buzz
     ============================================================================
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    //#include "gp2.h"
    
    void ad_perguntas();
    void ver_perguntas();
    void edit_perguntas();
    void remov_perguntas();
    void estrelas();
    void menu_principal();
    void jogo();
    void menu_ad();
    void adm();
    
    
    //******************************************************************************************************
    //Estruturas a serem usadas
    
    struct perg           /* Estrutura Questoes */
    {
      int id;
      char ques[200];
      char op1[15];
      char op2[15];
      char op3[15];
      char op4[15];
      int res;
    };
    
    struct jogador              /* Estrutura Utilizadores */
    {
      char nome[100];
      char id[100];
      char pass[100];
      int log;
      char buf[30];
    };
    
    /*struct gp2
    {
        int id;
        char ques[200];
        char res[30];
    };
    */
    
    //*************************Inicio da funções para o Grupo1************************************************
    
    void ad_perguntas(struct perg *p)
    {
        char buf[30];
        FILE *f;
        if ((f = fopen("Atividades/perguntas.dat", "wb")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        else
        {
    //Lê do teclado e grava no ficheiro
        printf("Introduza o ID da pergunta \n");
        fgets(buf, 30, stdin);
        sscanf(buf,"%d", &p->id);
        printf("Introduza a pergunta: \n");
        fgets(p->ques,200, stdin);
        printf("Opcao 1:  \n");
        fgets(p->op1, 15, stdin);
        printf("Opcao 2:  \n");
        fgets(p->op2, 15, stdin);
        printf("Opcao 3:  \n");
        fgets(p->op3, 15, stdin);
        printf("Opcao 4:  \n");
        fgets(p->op4, 15, stdin);
        printf("Introduza numero da resposta certa \n");
        fgets(buf, 30, stdin);
        sscanf(buf,"%d", &p->res);
        fwrite(p, sizeof(struct perg), 1 , f);
        fclose(f);
        }
    }
    
    void ver_perguntas(struct perg *p)
    {
        int retorno, cont = 0;
        FILE *f;
        if ((f= fopen("Atividades/perguntas.dat", "rb")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        retorno = fread(p, sizeof *p, 1, f);
        while ( retorno == 1)
        {
            cont++;
            printf("\nId : %d \n", p->id);
            printf("\nPergunta : %s", p->ques);
            printf("\nOpcao 1 : %s",p->op1);
            printf("\nOpcao 2 : %s",p->op2);
            printf("\nOpcao 3 : %s",p->op3);
            printf("\nOpcao 4 : %s",p->op4);
            printf("\nResposta Correta : %d",p->res);
            printf("\n");
            retorno= fread(p, sizeof *p, 1, f);
        }
        printf(" \n\n %d perguntas registadas \n", cont);
        fclose(f);
    }
    
    void edit_perguntas(struct perg *p)
    {
        int recno, nofrec=0;
        char ch;
        char buf[100];
        FILE *fp;
        fp=fopen("Atividades/perguntas.dat", "rb+");
        printf("Introduza o Id a modificar : ");
        fgets(buf, sizeof buf, stdin);
        sscanf(buf, "%d", &recno);
        while(fread(p, sizeof *p, 1, fp))
        {
            nofrec++;
            printf("sizeof(p) = %d; sizeof *p = %d\n", (int)sizeof(p), (int)sizeof *p);
            if(p->id==recno)
            {
                printf("\nId : %d \n", p->id);
                printf("\nPergunta : %s", p->ques);
                printf("\nOpcao 1 : %s",p->op1);
                printf("\nOpcao 2 : %s",p->op2);
                printf("\nOpcao 3 : %s",p->op3);
                printf("\nOpcao 4 : %s",p->op4);
                printf("\nResposta Correta : %d",p->res);
                printf("\n");
                printf("Quer modificar este registo: ? (S/N)");
                fgets(buf, sizeof buf, stdin);
                sscanf(buf, "%c", &ch);
                fseek(fp, (nofrec - 1) * sizeof *p, SEEK_SET);
                if(ch=='S'|| ch=='s')
                {
                    printf("Introduza a pergunta: \n");
                    fgets(p->ques,200, stdin);
                    printf("Opcao 1:  \n");
                    fgets(p->op1, 15, stdin);
                    printf("Opcao 2:  \n");
                    fgets(p->op2, 15, stdin);
                    printf("Opcao 3:  \n");
                    fgets(p->op3, 15, stdin);
                    printf("Opcao 4:  \n");
                    fgets(p->op4, 15, stdin);
                    printf("Introduza numero da resposta certa \n");
                    fgets(buf, sizeof buf, stdin);
                    sscanf(buf, "%d", &p->res);
                    fwrite(p, sizeof *p, 1, fp);
                    printf("Registo modificado \n");
                }
                else
                {
                    printf("Sem modificacoes efetuadas \n");
                }
                fclose(fp);
            }
        }
    }
    
    void remov_perguntas(struct perg *p)
    {
       int id_reg;
       FILE *orig_file, *temp_file;
       temp_file=fopen("temp.dat", "wb");
       if ((orig_file= fopen("Atividades/perguntas.dat", "rb")) == NULL)
       {
          printf("Erro ao abrir ficheiro \n");
       }
       printf("Introduza o Id a apagar: \n");
       scanf("%d",&id_reg);
       while(fread(p, sizeof *p, 1, orig_file));
       {
          if(id_reg == (*p).id)
          {
    
          }
          else
          {
              fwrite(p,sizeof *p,1,temp_file);
              printf("Registo apagado \n");
          }
       }
       fclose(orig_file);
       fclose(temp_file);
       remove("Atividades/perguntas.dat");
       rename("temp.dat", "Atividades/perguntas.dat");
       return;
    }
    
    //******************************Fim Perguntas GP1********************************************************
    
    
    
    //Funcoes a serem usadas
    void estrelas()
    {
        int i;
        for (i=0; i<=20; i++)
        {
            putchar('*');
        }
        //Poe caracter vazio
        putchar('\n');
    }
    
    void menu_principal()
    {
        int op;
        char buf[10];
        printf("BEMVINDOS AO BUZ \n");
        printf("Escolha uma das seguintes opcoes \n");
        printf("1 - Entrar como Administrador \n");
        printf("2 - Entrar com Jogador \n");
        printf("3 - Sair da aplicacao \n");
        if(fgets(buf, sizeof buf, stdin)==NULL)
        {
        }
        else
        {
            if(sscanf(buf,"%d",&op)!=1)
            {
    
            }
        switch (op)
        {
        case 1:
            adm();
            break;
        case 2:
            //
            break;
        case 3:
            exit(EXIT_SUCCESS);
            break;
        default:
            printf("Opcao invalida");
            break;
        }
        }
    }
    
    void jogo()
    {
        int op1;
        do
        {
            printf("1 - Jogo curto (2 perguntas)\n");
            printf("2 - Jogo medio (4 perguntas)\n");
            printf("3 - Jogo longo (8 perguntas)\n");
            printf("4 - Voltar ao menu inicial \n");
            printf("Escolha o tipo de jogo desejado:\n");
            scanf("%d", &op1);
            switch (op1)
            {
            case 1:
                //chama estrutura 1
                menu_adgp2();
                break;
            case 2:
                //chama estrutura 2
                printf("escolheu 2");
                break;
            case 3:
                //chama estrutura 3
                printf("escolheu 3");
                break;
            case 4:
                //chama estrutura 3
                menu_principal();
                break;
            default:
                printf("Valor invalido");
                break;
            }
        }
        while (op1<=5);
        getchar();
    }
    
    void menu_ad()
    {
        struct perg p;
        int x;
        do
        {
            printf("1 - Inserir perguntas: \n");
            printf("2 - Ver as perguntas: \n");
            printf("3 - Editar as perguntas: \n");
            printf("4 - Apagar as perguntas: \n");
            scanf("%d", &x);
            getchar();
            switch (x)
            {
            case 1:
                ad_perguntas(&p);
                break;
            case 2:
                ver_perguntas(&p);
                break;
            case 3:
                edit_perguntas(&p);
                break;
            case 4:
                remov_perguntas(&p);
                break;
            default:
                printf("Escolha invalida");
                break;
            }
          }
        while ( x != 5 );
    }
    
    void adm()
    {
        char login [10], pass [10];
        int x, a=1 , b=1;
        for (x=0; x<3; x++)
        {
            printf("Utilizador: \n");
            scanf("%s", login);
            printf("Password: \n");
            scanf("%s", pass);
            a= strcmp(login,"gil");
            b= strcmp(pass, "123");
            if (a==0 && b==0)
            {
                printf("Bem vindo \n");
                //Chamar um menu de escolha para o Admin
                menu_ad();
                x=5;
                break;
            }
            else
            {
                printf("Dados Utilizador ou Pass errados....Tente de novo \n");
            }
        }
        if (x==3)
        {
            printf("Tentativas exedidas, vai voltar ao menu inicial..\n");
            menu_principal();
        }
    }
    
    int main()
    {
        estrelas();
        printf("BEMVINDOS AO BUZZ \n");
        estrelas();
        putchar('\n');
        menu_principal();
        system("pause");
        return 0;
    }

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Remove from the main file all of the function prototypes that should NOT be there!!!!

    The function prototypes that should NOT be in the main source code file include
    1. The functions in the header file
    2. Any functions you are getting warnings/errors remove from the main source code file
    (Note: The option 2 ones might need to be re-added but removing them might give easier to understand error message)

    Tim S.

    Code snippet of prototypes from your main source file.
    Code:
    void ad_perguntas();
    void ver_perguntas();
    void edit_perguntas();
    void remov_perguntas();
    void estrelas();
    void menu_principal();
    void jogo();
    void menu_ad();
    void adm();

    Also, remove the comment in front of the header file include statement!

    Code:
    //#include "gp2.h"
    Last edited by stahta01; 05-16-2012 at 05:03 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Redefinition error
    By derder in forum C++ Programming
    Replies: 1
    Last Post: 11-06-2011, 08:55 PM
  2. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  3. function redefinition error
    By pepelepew in forum C Programming
    Replies: 2
    Last Post: 12-01-2009, 03:41 PM
  4. Should call redefinition error but isn't.
    By drb2k2 in forum C++ Programming
    Replies: 0
    Last Post: 04-16-2003, 05:18 AM
  5. Redefinition Error
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2002, 04:11 PM