CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2009
    Posts
    63

    Header files w/ c++, specifically data structures

    hey guys, so i am doing a project right now that requires that a specific data structure (there are a few actually, but its all the same case) be available/defined in multiple source files. i used to do my program via just including the .cpp source files in the main file, but now i am doing an overhaul- i am adding in header files and just including those. so what i have is:
    2 .cpp files, main.cpp and functions.cpp. i made a header file for each, include both in main.cpp like so:
    #include "main.h"
    #include "functions.h"

    in functions.h, i want to define the data structure. i did it like this:
    struct people {
    int age;
    bool gender;
    }

    extern struct people person_1; /*an instance i want available across all source files*/
    extern struct people person[5];

    then, in main.cpp, i went like this:
    struct people person_1;
    struct people person[5];

    it gave me a whole slew of errors, including:
    error: aggregate ‘people person1’ has incomplete type and cannot be defined
    error: elements of array ‘people person [5]’ have incomplete type
    error: storage size of ‘person’ isn't known

    whats up with it? how to fix? thanks!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Header files w/ c++, specifically data structures

    Looks like you forgot the semicolon at the end of the struct definition:
    Code:
    struct people {
        int age;
        bool gender;
    };
    A couple of things, too. First, "data structure" has a slightly different connotation than you're using it to mean here. A data structure is a generic concept like a linked list, binary tree, trie, hash table, etc. Something like "people" you should simply refer to as a struct for clarity.

    It's also unnecessary to use the struct keyword when declaring the global variables. That was necessary in C (unless you typedef'd it away), but not in C++.

  3. #3
    Join Date
    Jun 2009
    Posts
    63

    Re: Header files w/ c++, specifically data structures

    hmm... even with the semi-colon, i get the same error. any ideas? thanks!

  4. #4
    Join Date
    Apr 2008
    Posts
    725

    Re: Header files w/ c++, specifically data structures

    arent you meant to switch around where you have declared extern?

    do you use people inside main? becuase you include main first so maybe that is causing a problem too.

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Header files w/ c++, specifically data structures

    do you really include function.h in main.cpp?
    Your code fractions do not clearly show this.

  6. #6
    Join Date
    Jun 2009
    Posts
    63

    Re: Header files w/ c++, specifically data structures

    here, ill give the full files:
    functions.h:
    Code:
    #ifndef FUNCTIONS_H
    #define FUNCTIONS_H
    
    struct people {
         int age;
         bool gender;
    };
    
    
    #endif
    functions.cpp:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "functions.h"
    
    extern struct people person1;
    extern struct people person_array[5];
    
    person1.age = 20;
    person1.gender = 10;
    
    int print() {
    	printf("hello world!\n");
    	return 0;
    }
    main.cpp:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include "functions.h"
    
    int main() {
    	std::cout << "person1.age: " << person1.age << "\n";
    	std::cout << "person1.gender: " << person1.gender << "\n";
    	print();
    	return 0;
    }
    obviously thats not my actual project, but its all the relevant stuff. now when i go to compile this, i get the error:

    error: expected constructor, destructor, or type conversion before ‘.’ token

    on both lines in functions.cpp where i attempt to assign person1.age and person1.gender. whats up? thanks guys!

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Header files w/ c++, specifically data structures

    The "extern" keyword should only be used in header files.

    You can't do sequential statements (such as assigning values to age and gender) outside of a function. You can only do that in an initialization, such as
    Code:
    people person1 = {20, true};
    Or, if you gave the people struct a constructor, you could do
    Code:
    people person1(20, true);

  8. #8
    Join Date
    Apr 2004
    Posts
    102

    Re: Header files w/ c++, specifically data structures

    As of your last posting, there are several things wrong.

    First of all, you are mis-using the extern keyword. extern does not define a variable, it only lets you re-use a global declared in some other cpp file within your project. Therefore, person1 is undeclared in main.cpp.

    Second, there is really no good reason for person1 or person_array to be global in this project (generally avoid globals unless there is a really good reason to use them). They should be declared inside main(), then passed to other functions as needed, by reference if neccessary. Example:

    functions.h:
    Code:
    #ifndef FUNCTIONS_H
    #define FUNCTIONS_H
    
    struct people {
         int age;
         bool gender;
    };
    
    void printPerson(const struct people &thePerson);
    
    #endif
    functions.cpp:
    Code:
    #include <stdio.h>
    #include "functions.h"
    
    extern const int someGlobal; //for extern illustration only
    
    using namespace std;
    
    void printPerson(const struct people &thePerson)
    {
    	cout<<"age: "<<thePerson.age<<", gender: "<<thePerson.gender;
    
    }
    main.cpp
    Code:
    #include <stdio.h>
    #include "functions.h"
    
    const int someGlobal=2; //for extern illustration only
     
    using namespace std;
    
    int main()
    {
    	struct people person1;
    
    	person1.age=20;
    	person1.gender=true;
    
    	cout<<"Person1 - ";
    	printPerson(person1);
    	cout<<endl;
    
    	return 0;
    }
    Last edited by jefranki; August 27th, 2009 at 11:34 AM. Reason: Corrected a semantic error. (Thanks for the catch, Laserlight.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured