Static Members...

This is a discussion on Static Members... within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I'm writing a class that has as pair of static int members...pretty easy, I know, but it's not ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    80

    Static Members...

    Hi guys,

    I'm writing a class that has as pair of static int members...pretty easy, I know, but it's not working. The linker tells me I have unresolved external symbols associated with my static members. Any ideas?

    Also, does anyone know of some good online documentation on Hash Tables? SGIs info is pretty cryptic sometimes.

    Thanks!

    Code:
    job.h
    
    #ifndef JOB_H
    #define JOB_H
    
    #include<iostream>
    #include<vector>
    
    using namespace std;
    
    class Job
    {
    private:
        static int machPerJob, numOfMach;
        int startTime, endTime;
        vector<char> machines;
        char schedule;
        
    public:
        Job();
        Job(int, int, vector<char>);
        static void setMachPerJob(int);
        static int getMachPerJob();
        static void setNumOfMach(int);
        void setSchedule(char sched);
        friend ostream& operator<<(ostream& os, const Job& job);    
        friend istream& operator>>(istream& is, Job& job);
    };
    
    #endif
    
    job.cpp
    
    #include<vector>
    #include"job.h"
    
    using namespace std;
    
    Job::Job()
    {
        startTime = endTime = -1;
        schedule = '-';
    }
    
    Job::Job(int start, int end, vector<char> mach)
    {
        startTime = start;
        endTime = end;
        machines = mach;
        schedule = '-';
    }
    
    void Job::setMachPerJob(int mpj)
    {
        machPerJob = mpj;
    }
    
    int Job::getMachPerJob()
    {
        return machPerJob;
    }
    
    void Job::setNumOfMach(int nom)
    {
        numOfMach = nom;
    }
    
    void Job::setSchedule(char sched)
    {
        schedule = sched;
    }
        
    ostream& operator << (ostream& os, const Job& job)
    {
        os << job.startTime << " " << job.endTime << " " << job.schedule;
        return os;
    }
    
    istream& operator >> (istream& is, Job& job)
    {
        int start, end;
        char tempMach;
        is >> start >> end;
        job.startTime = start;
        job.endTime = end;
        for (int i = 0; i < Job::getMachPerJob(); i++)
        {
            is >> tempMach;
            job.machines.push_back(tempMach);
        }
        return is;
    }
    
    main.cpp
    
    #include<iostream>
    #include<fstream>
    #include<vector>
    //#include<unistd.h>
    #include"job.h"
    
    //void parseCommandLine(int, char**, char*&, char*&, char*&, bool&)
    
    int main(int argc, char** argv)
    {
        char *algor = "Recursive", *cacheFile = "testcase1.txt", *schedFile = "schedule.txt";
        bool helpFlag = false;
        int tempMachPerJob, tempNumOfMach, i;
        Job tempJob;
        vector<Job> jobs;
    
        //parseCommandLine(argc, argv, algor, cacheFile, schedFile, helpFlag);
    
        ifstream inFile(cacheFile);
        if (inFile.fail())
        {
            cout << "Error opening input file!" << endl;
            exit(1);
        }
    
        inFile >> tempMachPerJob >> tempNumOfMach;
        
        Job::setMachPerJob(tempMachPerJob);
        Job::setNumOfMach(tempNumOfMach);
        
    
        inFile >> tempJob;
        while (inFile)
        {
            jobs.push_back(tempJob);
            inFile >> tempJob;
        }
    
        for(i = 0; i < jobs.size(); i++)
            cout << jobs[i] << endl;
    
        return 0;
    }
    
    /*void parseCommandLine(int argc, char** argv, char*& algor, char*& cacheFile, 
                          char*& schedFile, bool& helpFlag)
    {
        int c;
        while ((c = getopt(argc, argv, "a::i::o::h")) != -1)
        {
            switch (c)  // Switch on flags, etc
            {
                case 'a':
                    algor = optarg;
                    break;
                case 'i':
                    cacheFile = optarg;
                    break;
                case 'o':
                    shcedFile = optarg;
                    break;
                case 'h':
                    helpFlag = true;
                    break;
                case '?':
                    cerr << "Argument Error!" << endl;
                    exit(1);
                default:
                    break;
            }   // Switch on getopt return value
        }
        return;
    }*/

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You have to redeclare them outside of the class.

    So, in job.cpp put

    Code:
    int Job::machPerJob,
        Job::numOfMach;
    You can also initialize them there as well.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Heh, Thanks pal.

    So, I knew that I had to do that, but I had it in job.h earlier, and it was giving me tons of problems there, so I took it out...what a moron.

    Ok, all works now,

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  5. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM

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