开发者

C++ Linking error

开发者 https://www.devze.com 2023-02-21 05:09 出处:网络
I have multiple classes connected by hxx header files and inheritance.This is all about OS simulation.

I have multiple classes connected by hxx header files and inheritance.This is all about OS simulation. I compiled them individually to see if i get any syntax errors but that was not the case.instead when i compile my main driver osTester.cxx i get linking error like this

#include <iostream>
#include <ostream>
#include <vector>
#include<string>
//#include <boost/shared_ptr.hpp>
#include "OS.hxx"
#include "Ram.hxx"
#include "Cpu.hxx"
#include "HardDisk.hxx"
#include "JobCreationFactory.hxx"
#include "Mouse.hxx"
#include "Monitor.hxx"
#include "Keyboard.hxx"

using namespace std;
typedef vector<int>LISTOFINT;
typedef LISTOFINT::iterator INT_ITER;

//namespace bo=boost;
//using bo::shared_ptr;
/* This is the main driver of the simulation,it makes all simulated devices available 
 and ready for use before operating system takes over(osTakeOver())
 */

int main(){
    
    int numberOfJobs = 0;
    bool simulate = false;
    string start;
    LISTOFINT schedulingAlgorithms; // 1 for SJF(Shortest Job first) and 2:for RR(Round Robbin)
    LISTOFINT numberOfResourcesAvailable;
    int mouseAvailable;
    int monitorAvailable;
    int keyboardAvailable;
    HardDisk HD;
    OS myOS;
    Cpu myCpu;
    Ram myRam;
    Mouse myMouse;
    Monitor myMonitor;
    Keyboard myKeyboard;
    JobCreationFactory j;
    //j.test();
    j.createFiles(HD);
    j.createJobs(HD);
    
    //set defaut number of jobs to be used in the simulation .. allow user to input number of jobs
    cout << "Enter number of Jobs to be used in the simulation (eg 40000) followed by space then  'S' for Start:";
    while (cin >> numberOfJobs >> start && !simulate) {
        simulate = true;
        HD.setNumberOfJobs(numberOfJobs);
    }   
    cout << "Enter the number of processes that a mouseQ can handle:(say 6)";
    cin >> mouseAvailable;
    cout << "Enter the number of processes that a monitoQ can handle:(say 7)";
    cin >> monitorAvailable;
    cout << "Enter the number of processes that a keyboardQ can handle:(say 8)";
    cin >> keyboardAvailable;
    numberOfResourcesAvailable.push_back(mouseAvailable);
    numberOfResourcesAvailable.push_back(monitorAvailable);
    numberOfResourcesAvailable.push_back(keyboardAvailable);
    //setAvailable Resources
    myOS.setAvailable(numberOfResourcesAvailable);
    
    /*simulate based on SJF then RR using the same number of resources then compare results
     *repeating the same algorithm a few more times would give a better comparison since this simulation
     *is entirely based on random number generation
    */
    schedulingAlgorithms.push_back(1);
    schedulingAlgorithms.push_back(2);
    INT_ITER sBegin = schedulingAlgorithms.begin();
    INT_ITER sEnd = schedulingAlgorithms.end();
    
    //at this point all devices are ready so an assumption can be made that 
    //BIOS has successfully checked devices hence BIOSdone = true
    //once everything is ready BIOS Loads OS
    
    for (; sBegin != sEnd; ++sBegin) {// for both algorithms
        myOS.setSchedulingAlgorithm(*sBegin);
        myOS.osTakeOver(HD,myRam,myCpu,myMouse,myMonitor,myKeyboard);
    }
    //for now the smulation progress will be seen on the console
    
    return 0;
    
}

I have tried using CMake but i still get the following error

Linking CXX executable osTester
Undefined symbols:

  "JobCreationFactory::createFiles(HardDisk)", referenced from:
      _main in osTester.cxx.o
  "OS::setAvailable(std::vector<int, std::allocator<int> >)", referenced from:
      _main in osTester.cxx.o
  "OS::osTakeOver(HardDisk, Ram, Cpu, Mouse, Monitor, Keyboard)", referenced from:
      _main in osTester.cxx.o
  "HardDisk::setNumberOfJobs(int)", referenced from:
      _main in osTester.cxx.o
  "OS::setSchedulingAlgorithm(int)", referenced from:
      _main in osTester.cxx.o
  "JobCreationFactory::createJobs(HardDisk)", referenced from:
      _main in osTester.cxx.old: symbol(s) not found

collect2: ld returned 1 exit status
make[2]: *** [src/osTester] Error 1
make[1]: *** [src/CMakeFiles/osTester.dir/all] Error 2
make: *** [all] Error 2

Please help!!!


Update:

I have JobCreationFactory::createFiles in cxx and prototype in .hxx.

I really don't understand the main problem. Please refer to the following Jobcreationfactory.cxx and .hxx respectively, and osTester.cxx (included in earlier post). I tried invoking a method from different class say OS and I get the same linking error.

#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include "random.hxx"
#include "Jobs.hxx"
#include "HardDisk.hxx"
#include "File.hxx"

//using namespace std;
//typedef vector<int> LISTOFINT;
//LISTOFINT::iterator INT_ITER;

class JobCreationFactory{
    int numOfJobs;
    int numOfFiles;
    int time;
    int size;
    LISTOFINT header;
    
    JobCreationFactory()
    :numOfJobs(0),time(0),size(0)
    {
    }
    
     int getNumberOfJobs() 
    {
        return numOfJobs;
    }
    
     void createFiles(HardDisk hd)
    {   string file = "File";
        string intval;
        stringstream ss (stringstream::in | stringstream::out);
        RandomSeq r1(10000, 20000);
        numOfFiles = r1();
        RandomSeq r2(10, 100);
        for(int j = 0; j<=numOfFiles; j++ )
        {
            ss << j+1;
            ss >> intval;
            file.append(intval);
            hd.storeFiles(File(file,r2()));
        }
    }
    
     void createJobs(HardDisk hd)
    {
        string job = "Job";
        string intval;
        stringstream ss (stringstream::in | stringstream::out);
        RandomSeq r1(60000,70000);
        numOfJobs = r1();
        int instructionException = (int)(0.1 * numOfJobs);
        
        for (int i = 1; i < getNumberOfJobs()+ 1; i++) 
        {
            RandomSeq r1(0, 2);
            RandomSeq r2(0, numOfFiles);
            size = assignSize(); 
            time = assignTime(size);
            setHeader();
            ss << i;
            ss >> intval;
            job.append(intval);
            Jobs newJob(job,size, time, header,r1());
            if(i%instructionException == 0)
            {
                newJob.setInstructionException(true);
            }
            for(int k = 0; k<newJob.getNumberOfFiles();k++)
            {
                newJob.associateJobWithFiles(k, r2());
            }
            hd.storeJobs(newJob);
        }
    }
    
     int assignSize()
    {
        RandomSeq r1(500,5000);
        int size = r1();
        return size;
    }
    
     int assignTime(int size)
    {
        int time =0;
        RandomSeq r1(10,20);
        RandomSeq r2(20,30);
        RandomSeq r3(30,40);
        RandomSeq r4(40,50);
        RandomSeq r5(50,60);
        RandomSeq r6(60,70);
        RandomSeq r7(70,80);
        RandomSeq r8(80,90);
        RandomSeq r9(90,100);
        
        if(size>=500&&size < 1000)
        {
            time = r1();
        }
        else if(size>=1000&&size < 1500)
        {
            time = r2();
        }
        else if(size>=1500&&size < 2000)
        {
            time = r3();
        }
        else if(size>=2000&&size < 2500)
        {
            time = r4();
        }
        else if(size>=2500&&size < 3000)
        {
            time = r5();
        }
        else if(size>=3000&&size < 3500)
        {
            time = r6();
        }
        else if(size>=3500&&size < 4000)
        {
            time = r7();
        }
        else if(size>=4000&&size < 4500)
        {
            time = r8();
        }
        else
        {
            time = r9();
        }
        
        return time;
        
    }
    
     string assignIO()
    {
        RandomSeq r1(1,5);
        int num = r1();
        string need="";
        
        if(num == 1)
        {
            need = "keyboard";
        }
        else if(num == 2)
        {
            need = "mouse";
        }
        else if(num == 3)
        {
            need = "Both"; // keyboard and mouse
        }
        else if(num == 4)
        {
            need ="file";
        }
        else
        {
            need = "nothing";
        }
        return need;
    }
    
    
     void setHeader()
    {
        RandomSeq r1(0,4);
        RandomSeq r2(0,3);
        RandomSeq r3(0,2);
        INT_ITER hIter = header.begin();
        header.insert(hIter,r1());
        header.insert(hIter+1,r2());
        header.insert(hIter+2,r3());
    }
    
     int getNumberOfFiles() {
        return numOfFiles ;
    }
    void test(){
        cout << "Testing JobCreationFactory\n";
    }
        
};
int main(){
    return 0;
}

//.hxx follows below:

#ifndef JobCreationFactory_hxx__
#define JobCreationFact开发者_运维百科ory_hxx__
#include "HardDisk.hxx"
class JobCreationFactory {
public:
    //JobCreationFactory(){
    //}
    int getNumberOfJobs();
    void createFiles(HardDisk hd);
    void createJobs(HardDisk hd);
    int assignSize();
    int assignTime(int size);
    string assignIO();
    void setHeader();
    int getNumberOfFiles();
    void test();
};

#endif // JobCreationFactory_hxx_


Compiling individually is a reasonable test for syntax errors, but there's no point in linking individually, things WILL be missing.

So find the compiler option to compile without linking (Microsoft Visual C++: /c, g++: -c) and use it.


This error is telling you that there's no definition for the method JobCreationFactory::createFiles -- so either you forgot to define that in any file anywhere, or the file defining it is not being linked along with osTester.cxx.o (which is the file that calls it).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号