Thread: variable redefinition error

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    variable redefinition error

    Ok in my project i Have the following three files:
    Main.cpp:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <wincon.h>
    #include "GKinfo.h"
    
    using namespace std;
    //main function to find out options
    
    int main()
    {
        std::cout<<"\n\n::::::::::                                           ::::::::::\n"
                 <<":::::::::: G   R   A   D   E   K   E   E   P   E   R ::::::::::\n"
                 <<"::::::::::                                           ::::::::::\n";
           
        while(1)
        {     
            cout<<"\n\n\nM   E   N   U"
                <<"\n[1]:Add a course"
                <<"\n[2]:Add points (assignment)"
                <<"\n[3]:View a course's information"
                <<"\n[4]:Exit\nChoice:";
            int choice;
            std::cin>>choice;
    
            if(choice==1)
            {       
                    getdata(c);
            }
            if(choice==3)
            {   
                    std::cout<<"\nPlease enter the course hour:";
                    std::cin>>selecthour;
                    readdata(c, selecthour);
            }
            if(choice==2)
            {
                cout<<"\nPlease enter the hour this assignment was for:";
                cin>>inputhour;         
                addassign(c, inputhour);
           }
            if(choice==4)
            {
                    return 0;
            }
                    
        }
    return 0;
    }
    gkfunc.cpp:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <wincon.h>
    #include "GKinfo.h"
    using namespace std;
    
    void addassign(course* c, int inputhour)
    {
         cout<<"\nPlease enter the points you got on the assignment:";
         cin>>yourscore;
         cout<<"\nPlease enter the total points from the assignment:";
         cin>>totalscore;
         c[inputhour].nCurrentPoints+=yourscore;
         c[inputhour].nMaxPoints+=totalscore;
         gradevar=(c[inputhour].nCurrentPoints/c[inputhour].nMaxPoints);
         if(gradevar>=0.9)
         {
             newgrade='A';
         }
         else if(gradevar>=0.8)
         {
              newgrade='B';
         }
         else if(gradevar>=0.7)
         {
              newgrade='C';
    	 }
    	 else if(gradevar>=0.6)
    	 {
    		  newgrade='D';
    	 }
    	 else if(.59>=gradevar)
    	 {
              newgrade='E';
         }
         c[inputhour].cGrade=newgrade;
    
    }
    
    void readdata(course* c, int selecthour)
    {
        double pcent=100*(c[inputhour].nCurrentPoints/c[inputhour].nMaxPoints);
        cout<<"\nCourse name:"<<c[selecthour].CourseName<<"\nHour:"<<c[selecthour].nHour;
        cout<<"\nCurrent points:"<<c[selecthour].nCurrentPoints<<"\nTotal Points:"<<c[selecthour].nMaxPoints;
        cout<<"\nCurrent gade:"<<c[selecthour].cGrade;
        cout<<"\nPercent:"<<pcent<<"%";
        //cin.get();
    }
    
    void getdata(course* x)
    {
                    std::cout<<"\nPlease enter an hour:";
                    std::cin>>inputhour;
                    c[inputhour].nHour=inputhour;
                    std::cout<<"\nPlease enter a course name:";
                    std::cin>>c[inputhour].CourseName;
                    //system("PAUSE");
    }
    and gkinfo.h:
    Code:
    #ifndef _GKINFO_H
    #define _GKINFO_H
    #include <iostream>
    #include <stdlib.h>
    #include <wincon.h>
    class course
    {
        public:
            int nHour;
            char cGrade;
            double nMaxPoints;
            double nCurrentPoints;
            char CourseName[255];
    
    };
    ///////////////////////////////////////////////////////////////
    void addassign(course* c, int inputhour);
    void readdata(course* c, int selecthour);
    void getdata(course* x);
    ///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////
    //////VARIABLE DECLARATIONS////////////////////////////////////
    double yourscore;               //Used to tell what you got on the assignment
    double totalscore;              //Used to tell what the total points of the assignment was
    int selecthour;              //used to bring up the selected hour
    int inputhour=1;             //used to insert the hour
    double gradevar;                //Used to help decide the grade
    char newgrade;
    course c[25];
    ///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////
    #endif
    Ok it appears my error is coming from the variable declarations. The errors I get when I try to compile is the same: "gkfunc.cpp multiple definition of `*insertvariablenamehere*' ", "main.cpp first defined here ".

    Now I know these variables aren't found in any other file so I don't know WHY they would be getting multiple definitions. Does anybody know why I'm getting this error?
    Last edited by 7smurfs; 12-14-2004 at 03:42 PM.
    To code is divine

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int inputhour=1;
    You can't have initialisers in header files.
    Put the initialisation in ONE of the source files.

    Additionally, it's probably a good idea to say
    extern int inputhour;
    in the header file.

    Your class isn't much of a class, if all it has is public data members.

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    >>Your class isn't much of a class, if all it has is public data members.

    Im just using this to get used to using classes, and it keeps me more organized

    Anyways I tried the extern thing and I get the same error


    EDIT: Ok so now i'm getting linker errors to the variables, anybody know what I can do to stop that?
    Last edited by 7smurfs; 12-14-2004 at 04:59 PM.
    To code is divine

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    extern int foo; // in the .h file

    int foo = 1; // in ONE .cpp file

  5. #5
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Quote Originally Posted by Salem
    extern int foo; // in the .h file

    int foo = 1; // in ONE .cpp file
    I already did that (well, I didn't put any value to it becuase I don't want a value to it (that was old code I didn't need anymore but didn't feel like removing it yet).

    Here is the erros I'm getting now:
    " [Linker error] undefined reference to `variablenamehere' " in every function.

    Wow...I can't believe it...Salem I'm sorry but I mis-read your post, and now that I re-read it I managed to get it to work o-O.
    Thank you so so much!
    Last edited by 7smurfs; 12-14-2004 at 06:28 PM.
    To code is divine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM