|
-
August 11th, 2009, 01:17 PM
#1
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!
-
August 11th, 2009, 01:39 PM
#2
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++.
-
August 11th, 2009, 02:35 PM
#3
Re: Header files w/ c++, specifically data structures
hmm... even with the semi-colon, i get the same error. any ideas? thanks!
-
August 12th, 2009, 12:55 AM
#4
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.
-
August 12th, 2009, 01:44 AM
#5
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.
-
August 12th, 2009, 12:04 PM
#6
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!
-
August 12th, 2009, 12:33 PM
#7
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);
-
August 13th, 2009, 03:23 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|