Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am wirting a program in visual c++.I made one header file named as basic.h which contains global variables and functions,and its source file basic.cpp contains their definitions.I have made another header file matrixB.h and its source file matrixB.cpp.Main program and matrixB.cpp uses the global variables from basic.h.
when I try to compile my code I got the errors of redefinition and missing type specifier.If anyone knows please help me.
I have also uploaded my project at media fire.http://www.mediafire.com/?o8leol9q92v1d8x


thanks in advance

Ishtiaq
Posted
Comments
Albert Holguin 15-Mar-11 19:28pm    
although the comments below answer the question... you should try to add code snippets here instead of linking to a compressed file on the net... can't speak for anyone but myself, but I'm not about to download and extract files from the internet from an unknown source...

In addition to what Niklas said, make sure your .h files are protected against multiple includes by using a header guard or #pragma once.
 
Share this answer
 
Comments
Albert Holguin 15-Mar-11 19:25pm    
...or #ifndef guards...
Hans Dietrich 15-Mar-11 19:30pm    
aka header guard. :)
Albert Holguin 15-Mar-11 19:34pm    
my 5!
smishtiaqhussain 15-Mar-11 19:42pm    
Thanks a lot for ur help and quick reply.....as you people suggested I have declared the global variables in header file with a keyword extern.and then I have define my these variables in cpp file.I have also added guard header to my header file using these command at start of header file

#ifndef BASIC_H
#define BASIC_H

and this at end of header file
#endif
.I have these commands in both header files.Now I am getting 6 errors of this kind.
Error   4   error LNK2001: unresolved external symbol "int oilvariables" (?oilvariables@@3HA)   basic.obj


I have also posted updated project at the link.http://www.mediafire.com/?whzjagw3waio9qi
You should not declare global variables in a header file, unless you use the keyword extern and then define them in one cpp file. If you do, you might end up with multiple defined variables as you describe when two different cpp files are compiled and put the variables in their object code for linking. The linker will see the same variable twice.

You could get around this in a better way by putting your global variables as class members (declared static) in a class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900