y1 Redeclared??

This is a discussion on y1 Redeclared?? within the C++ Programming forums, part of the General Programming Boards category; I"m having a bit of an issue with variable redeclaration..and frankly i'm quite confused on this one..because the variable in ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    220

    y1 Redeclared??

    I"m having a bit of an issue with variable redeclaration..and frankly i'm quite confused on this one..because the variable in question is in math.h..and i'm not including math.h and to my knowledge neither is any header file that i'm including in my source..

    Heres the beginning part of my code:
    Code:
    #include <iostream>//For use of cout and cin for output and input.
    #include <fstream>//For use of ofstream for text file write.
    #include <conio.h>//For use of getch() function for keyboard input.
    #include <stdlib.h>//For use of exit() function for exiting program.
    
    int x1, x2, x3, x4;
    int y1, y2, y3, y4;
    Now for some reason I get the error:
    int 'y1' redeclared.

    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    it is really a really good practice to use the #ifndef and #define pair when including multiple header files...it keeps these kind-of problems from coming up.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Show the rest of the code and the exact error. Most compilers tells the line where a variable is beeing redeclared!
    Or you could use extern if these variables are in a header file...
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    >it is really a really good practice to use the #ifndef and #define pair when including multiple header files...it keeps these kind-of problems from coming up.

    Could you provide an example?

    >Show the rest of the code and the exact error. Most compilers tells the line where a variable is beeing redeclared!

    As far as what line it is, the line was:
    Code:
    int y1, y2, y3, y4;
    And no I didn't declare it twice in this particular source..I only have one source file. That line was right after the #include preprocessor directives, there global variables so..

    I'll try my best to recover the code(I'm recoding it right now...kind of a hassle, but I had to reinstall windows on this machine and fast..).

    >Or you could use extern if these variables are in a header file...

    I'm assuming that there in a header file, but i'm not quite sure. But in any case could you provide an example of this as well?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    To ue global variables in multiple source files, you do this way:
    Code:
    //code.h
    extern int myInt;
    Code:
    //code1.cpp
    #include "code.h"
    int myInt;
    //use int where you want
    Code:
    //code2.cpp
    #include "code.h"
    //use int where you want
    Nothing more to tell about me...
    Happy day =)

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    thank you It's fixed.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a fast way to check if a point is within a line.
    By mike_g in forum Game Programming
    Replies: 5
    Last Post: 08-05-2008, 12:24 PM
  2. Frying my system
    By Brad0407 in forum Windows Programming
    Replies: 7
    Last Post: 06-15-2007, 09:56 AM
  3. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 03:14 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. Drawing lines and ellipses with DirectDraw
    By Hunter2 in forum Game Programming
    Replies: 9
    Last Post: 01-08-2003, 12:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21