i have problem only declaring struct variable. i declare a struct in struct.h file and i wan main.c and class.h also using the same variable. But the class.h file say the struct is undeclare..

example:
struct.h

struct car{
int year;};

main.c

#include <cstdlib>
#include <iostream>
#include "struct.h"
#include "class.h"

using namespace std;

int main(int argc, char *argv[])
{
    car c;
    system("PAUSE");
    return EXIT_SUCCESS;
}
}

class.h

#include <cstdlib>
#include <iostream>
using namespace std;

extern car c;

class test{
c.year=10;};

Recommended Answers

All 6 Replies

If you have not included class.h in your project, include "struct.h" in "class.h" instead of main.

i got problem if include .h file in a .h file

i got problem if include .h file in a .h file

You can always include .h files in .h files. & always use include-guards for your headers. Did you remove #include stament in main? If no remove it as it will create an error of multiple declaration of struct car.

err.. do you mind give me some example?

Sure.

#ifndef _MYHEADER_H
#define _MYHEADER_H
/*
Code
*/
#endif

This ensures that header file is "included" only once in a project.

thanks..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.