Thread: Why it give warning message, how will it remove

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    1

    Why it give warning message, how will it remove

    Code:
    Test1.h
    ---------------------
    #ifndef TEST1_H_
    #define TEST1_H_
    
    #include "Test2.h"
    
    typedef struct _Test1{
        int a;
        int b;
    
        int (*add)(struct _Test1 *test1, struct _Test2 *test2);
    }Test1;
    
    Test1 newTest1();
    
    #endif /* TEST1_H_ */
    
    Test1.c
    -----------------------------
    
    #include "Test1.h"
    
    static int test1_add(Test1 *test1, Test2 *test2){
        return (test1->a + test1->b) + (test2->c + test2->d);
    }
    
    Test1 newTest1(){
        Test1 test1;
        test1.a = 5;
        test1.b =6;
        test1.add = &test1_add;
        return test1;
    }
    
    Test2.h
    ------------------------
    
    #ifndef TEST2_H_
    #define TEST2_H_
    
    #include "Test1.h"
    
    
    typedef struct _Test2{
        int c;
        int d;
    
        int (*substract)(struct _Test2 *test2, struct _Test1 *test1);
    }Test2;
    
    Test2 newtest2();
    
    #endif /* TEST2_H_ */
    
    Test2.c
    ----------------------------------
    #include "Test2.h"
    
    static int test2_substract(Test2 *test2, Test1 *test1){
        return (test2->c + test2->d) - (test1->a + test1->b);
    }
    
    Test2 newtest2(){
        Test2 test2 ;
        test2.c = 15;
        test2.d = 25;
        test2.substract = &test2_substract;
        return test2;
    }
    
    MakeFile
    --------------------------------
    all:
    gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Test1.d" -MT"Test1.d" -o "Test1.o" "Test1.c"
    gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Test2.d" -MT"Test2.d" -o "Test2.o" "Test2.c"
    gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "main.c"
    
    warning message:
    -------------------------------
    make all 
    gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Test1.d" -MT"Test1.d" -o "Test1.o" "Test1.c"
    gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Test2.d" -MT"Test2.d" -o "Test2.o" "Test2.c"
    In file included from Test1.h:12,
                     from Test1.c:10:
    Test2.h:18: warning: ‘struct _Test1.h declared inside parameter list
    Test2.h:18: warning: its scope is only this definition or declaration, which is probably not what you want
    In file included from Test2.h:12,
                     from Test2.c:9:
    Test1.h:17: warning: ‘struct _Test2.h declared inside parameter list
    Test1.h:17: warning: its scope is only this definition or declaration, which is probably not what you want
    gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "main.c"
    gcc -o RountStruct Test1.o Test2.o main.o
    In file included from Test1.h:12,
                     from main.c:9:
    Test2.h:18: warning: ‘struct _Test1.h declared inside parameter list
    Test2.h:18: warning: its scope is only this definition or declaration, which is probably not what you want
    main.c: In function ‘main’:
    main.c:21: warning: passing argument 2 of ‘test2.substract’ from incompatible pointer type
            #gcc -o RountStruct Test1.o Test2.o main.o

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have a circular inclusion: Test1.h includes Test2.h which includes Test1.h

    Neither of the headers actually needs the struct definition within the other header. Therefore, you should use forward declarations instead, e.g.,
    Code:
    #ifndef TEST1_H_
    #define TEST1_H_
    
    struct _Test2;
    
    typedef struct _Test1{
        int a;
        int b;
    
        int (*add)(struct _Test1 *test1, struct _Test2 *test2);
    }Test1;
    
    Test1 newTest1(void);
    
    #endif /* TEST1_H_ */
    Code:
    #ifndef TEST2_H_
    #define TEST2_H_
    
    struct _Test1;
    
    typedef struct _Test2{
        int c;
        int d;
    
        int (*substract)(struct _Test2 *test2, struct _Test1 *test1);
    }Test2;
    
    Test2 newtest2(void);
    
    #endif /* TEST2_H_ */
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I remove multi character constant warning?
    By johnjrgs in forum C Programming
    Replies: 2
    Last Post: 02-20-2013, 09:35 AM
  2. remove warning message multi dimensional pointers
    By sandbucket in forum C Programming
    Replies: 28
    Last Post: 11-30-2012, 04:19 AM
  3. Remove message queues
    By zooro in forum Linux Programming
    Replies: 1
    Last Post: 04-17-2012, 12:27 PM
  4. Free Windows C Compile that give good warning?
    By stahta01 in forum C Programming
    Replies: 2
    Last Post: 11-24-2010, 09:20 PM
  5. How to remove an error message from a p C++ program
    By tweetybrd22 in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2010, 07:00 PM