Thread: Extern Struct from .h file not finding elements

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    Extern Struct from .h file not finding elements

    Hi,

    I have declared an exten struct 'linked_list' in a header file, this header file is included in the c file, and the necessary elements from the h file are declared. but when I try to access the members of an instance of the struct from the h file, there are errors stating that the struct has no member of that name

    the code is as follows

    h file:
    Code:
    typedef struct linked_list {
    thread_t thread;
    linked_list *next;
    int isNull;
    }linked_list;
    
    extern linked_list *threads_head;
    extern linked_list *threads_tail;
    
    int totalSize = 0;
    int hasHead = 0;
    and some of the code for the c file (not all included,as same error thrown throughout the file):
    Code:
    #include <stdio.h>
    #include "context.h"
    #include "sched.h"
    #include "thread.h"
    #include <stdlib.h>
    
    
    thread_t newThread;
    linked_list threads;
    linked_list *threads_head;
    linked_list *threads_tail;
    int totalSize;
    
    
    context_t context_1;
    context_t context_2;
    
    
    void remove_thread(){
    	int pos;
    	pos = newThread.ID;
    	linked_list x, temp;
    
    	temp = *threads_head;
    	for(int i=0;i<totalSize;i++)
    	{
    		if(i==pos){
    			x=*temp.next;
    			temp.next=*temp.next.next;
    			free(&x);
    		}
    	temp=*temp.next;
    	}
    }
    
    void add_thread(thread_t* newThread){
    if (totalSize==0){
     		threads_head->thread = &newThread;
    		threads_head->next = &threads_tail; 
    		threads_head->isNull = 1;
    		totalSize++;
    		
    	} else{
    		//threads_tail->next = *newThread;
    		linked_list newNullElement;
    		newNullElement.isNull =0;
    
    		linked_list newElement;
    		newElement.thread = newThread;
    		newElement.next = newNullElement;
    		newElement.isNull = 1;
    		
    		threads_tail->next = newElement; //thread tail = new linked list
    
    		totalSize++;
    		threads_tail = &newElement;
    
    	}
    	
    }
    Im probably making a silly little mistake, but I cant for the life of me figure it out!

    I'd be super grateful for any help

    Fiona

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Could you print out the errors you're getting as well? Maybe not all of them if there are a lot, but certainly the first few (make sure it's the very first few though).

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    In file included from thread.c:3:
    sched.h:6: error: expected specifier-qualifier-list before 'thread_t'
    thread.c: In function 'remove_thread':
    thread.c:35: error: 'linked_list' has no member named 'next'
    thread.c:36: error: 'linked_list' has no member named 'next'
    thread.c:36: error: 'linked_list' has no member named 'next'
    thread.c:39: error: 'linked_list' has no member named 'next'
    thread.c: In function 'add_thread':
    thread.c:46: error: 'linked_list' has no member named 'thread'
    thread.c:47: error: 'linked_list' has no member named 'next'
    thread.c:48: error: 'linked_list' has no member named 'isNull'
    thread.c:54: error: 'linked_list' has no member named 'isNull'
    thread.c:57: error: 'linked_list' has no member named 'thread'
    thread.c:58: error: 'linked_list' has no member named 'next'
    thread.c:59: error: 'linked_list' has no member named 'isNull'
    thread.c:61: error: 'linked_list' has no member named 'next'


    etc

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Fillis52 View Post
    In file included from thread.c:3:
    sched.h:6: error: expected specifier-qualifier-list before 'thread_t'

    thread.c: In function 'remove_thread':
    thread.c:35: error: 'linked_list' has no member named 'next'
    thread.c:36: error: 'linked_list' has no member named 'next'
    thread.c:36: error: 'linked_list' has no member named 'next'
    thread.c:39: error: 'linked_list' has no member named 'next'
    thread.c: In function 'add_thread':
    thread.c:46: error: 'linked_list' has no member named 'thread'
    thread.c:47: error: 'linked_list' has no member named 'next'
    thread.c:48: error: 'linked_list' has no member named 'isNull'
    thread.c:54: error: 'linked_list' has no member named 'isNull'
    thread.c:57: error: 'linked_list' has no member named 'thread'
    thread.c:58: error: 'linked_list' has no member named 'next'
    thread.c:59: error: 'linked_list' has no member named 'isNull'
    thread.c:61: error: 'linked_list' has no member named 'next'


    etc
    Always always always resolve the first error first. Here, you're chasing after "no member" errors because they're more numerous, but they're all caused by the previous error, "expected specifier-qualifier-list before 'thread_t'". This means the compiler doesn't know what thread_t is (presumably you need to typedef it or include an appropriate header file first) and so can't construct "linked_list", which means it doesn't recognise if for the rest of the compilation unit, and so will naturally complain about basically any use of it. Solve this problem, and then either you'll be able to compile it or you'll get a new, different first (very first - cannot stress that enough) error you can resolve.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    my defenition for thread is in another h file called thread.h. when i #include "thread.h" in the other h file i get the following errors

    thread.h:4: error: redefinition of 'struct thread_t'
    thread.h:8: error: redefinition of typedef 'thread_t'
    thread.h:8: error: previous declaration of 'thread_t' was here


    the original code for the definition of the thread struct was as follows:
    Code:
    typedef struct thread_t {
    context_t context; // Machine context of the thread.
    int ID;
    int state;
    }thread_t;

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Fillis52 View Post
    my defenition for thread is in another h file called thread.h. when i #include "thread.h" in the other h file i get the following errors

    thread.h:4: error: redefinition of 'struct thread_t'
    thread.h:8: error: redefinition of typedef 'thread_t'
    thread.h:8: error: previous declaration of 'thread_t' was here
    You want to #include your thread.h header file anywhere it's needed, but you also want to use an include-guard so it (effectively) only gets included once per compilation unit.

    Using an include-guard on all your headers and then #include "thread.h" at the top of the original header file you posted should solve this problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM