header file and ifstream

hello this is my header file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

const unsigned short int numbersAdd=100;
const unsigned short int chr_len=30;

struct address{
	char name[chr_len];
	char street[chr_len];
	char state[chr_len];
	int post;

	};
void readFile(char[], ifstream&);
int checkXtension(char[]);
void displayAddress(char[]);
void displayAddresses(int,int);


when I try to compile it says:

gcc -Wall -o "xmlAddress" "xmlAddress.h" (in directory: /home/ashari/Desktop/csci124/Lab Task 2)
xmlAddress.h:7:7: error: variably modified ‘name’ at file scope
xmlAddress.h:8:7: error: variably modified ‘street’ at file scope
xmlAddress.h:9:7: error: variably modified ‘state’ at file scope
xmlAddress.h:13:23: error: unknown type name ‘ifstream’
Compilation failed.

pls help me.
Is this really the code? Are you sure chr_len is assigned a compile-time constant?
yee
Aaah, now I see what's wrong. You are compiling the program as C. Use the g++ command instead of gcc.
i am doing that already..still now work :( I use geany and using ubuntu
No but you should get a different error message when you do that. Your program will still not compile because it doesn't know what ifstream is, and you can't produce an executable file from just this file because there is no main function.

You should include <fstream> and write std::ifstream (because using namespace std; is not recommended in header files).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<fstream>

const unsigned short int numbersAdd=100;
const unsigned short int chr_len=30;

struct address{
	char name[chr_len];
	char street[chr_len];
	char state[chr_len];
	int post;

	};
void readFile(char[], std::ifstream&);
int checkXtension(char[]);
void displayAddress(char[]);
void displayAddresses(int,int);


i did this still says fstream no such file
Geany treat .h files as C files, but there shouldn't be a need to compile header files separately (except maybe for your own convenience to see if there are any errors). The header files are included by source files, and it's the source files that you should compile.
Last edited on
so what do you suggest..could you pls show me the modified code above?
I don't think there is anything wrong with the code above. The problem is with the way you are trying to compile it.
I also did from cmd line g++ -o filename name.cpp name1.cpp but still gives same error
thanks its working in codeblocks...crap Geany...btw if u could tell me to make a project from cmd line...thanks
Topic archived. No new replies allowed.