-
April 6th, 2009, 09:08 PM
#1
problem compiling with g++
The code below compiles and runs without any errors in Visual C++ 2008 but I have to show it to my professor on computers running Solaris. I try to compile with g++ and get the mess below
classes.h
Code:
#ifndef _CLASSES_H_
#define _CLASSES_H_
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class Job{
private:
string name;
int startTime;
int endTime;
int runTime;
int priority;
int turnAround;
public:
Job():name(""),startTime(0),endTime(0),runTime(0),priority(0),turnAround(0){}
Job(string NAME, int START):name(NAME),startTime(START),endTime(0),runTime(0),priority(0),turnAround(0){}
Job(string NAME, int START, int PRIORITY):name(NAME),startTime(START),endTime(0),
runTime(0),priority(PRIORITY),turnAround(0){}
string getName(){return name;}
void setName(string NAME){name=NAME;}
int getStart(){return startTime;}
void setStart(int START){startTime=START;}
int getEnd(){return endTime;}
void setEnd(int end){endTime=end;}
int getRunTime(){return runTime;}
void setRunTime(int RT){runTime=RT;}
int getPriority(){return priority;}
void setPriority(int PRIORITY){priority=PRIORITY;}
int getTurnAround(){return turnAround;}
void setTurnAround(int TA){turnAround=TA;}
};
#endif
mainprog.cpp
Code:
#include"classes.h"
void display(vector<Job>);
void FCFS(vector<Job>);
void SJN(vector<Job>);
void Priority(vector<Job>);
bool sizeSort(Job & lhs, Job & rhs){return lhs.getRunTime() < rhs.getRunTime();} //for SJN sort
bool prioritySort(Job & lhs, Job & rhs){return lhs.getPriority() < rhs.getPriority();} //for priority sort
int main(){
vector<Job> jobs;
int jobNumber = 0;
//get number of jobs
cout << "Enter the number of jobs: ";
cin >> jobNumber;
jobs.resize(jobNumber);
//get job info
for(int i=0;i<jobs.size();i++){
string tempName;
int tempStart;
int tempPriority;
int tempRun;
cout << "Enter the name for job number " << i+1 << ": ";
cin >> tempName;
jobs[i].setName(tempName);
cout << "Enter the start time for " << jobs[i].getName() << ": ";
cin >> tempStart;
jobs[i].setStart(tempStart);
cout << "Enter time required for job execution: ";
cin >> tempRun;
jobs[i].setRunTime(tempRun);
cout << "Enter job priority: ";
cin >> tempPriority;
jobs[i].setPriority(tempPriority);
}
FCFS(jobs);
SJN(jobs);
Priority(jobs);
return 0;
}
void display(vector<Job> jobVec){
cout << "Job:\tStart:\tEnd:\tJob Turnaround:"<< endl;
cout << "____\t______\t____\t_______________"<<endl;
for(int j=0;j<jobVec.size();j++){
cout << jobVec[j].getName() << " \t" << jobVec[j].getStart() <<
" \t" << jobVec[j].getEnd() << " \t" << jobVec[j].getTurnAround() << endl;
}
}
void FCFS(vector<Job> jobList){
int clock = 0;
double avgTurn =0;
for(int i =0;i<jobList.size();i++){
clock += jobList[i].getRunTime();
jobList[i].setEnd(clock);
jobList[i].setTurnAround(jobList[i].getEnd() - jobList[i].getStart());
avgTurn += jobList[i].getTurnAround();
}
avgTurn /= jobList.size();
cout << "\t\t\tFCFS" << endl;
display(jobList);
cout << "The average turnaround time is: " << avgTurn << endl;
}
void SJN(vector<Job> jobList){
sort(jobList.begin()+1,jobList.end(),sizeSort);
int clock = 0;
double avgTurn =0;
for(int i =0;i<jobList.size();i++){
clock += jobList[i].getRunTime();
jobList[i].setEnd(clock);
jobList[i].setTurnAround(jobList[i].getEnd() - jobList[i].getStart());
avgTurn += jobList[i].getTurnAround();
}
cout << "\t\t\tSJN" << endl;
display(jobList);
cout << "The average turnaround time is: " << avgTurn/jobList.size() << endl;
}
void Priority(vector<Job> jobList){
sort(jobList.begin(),jobList.end(),prioritySort);
int clock = 0;
double avgTurn =0;
for(int i =0;i<jobList.size();i++){
clock += jobList[i].getRunTime();
jobList[i].setEnd(clock);
jobList[i].setTurnAround(jobList[i].getEnd() - jobList[i].getStart());
avgTurn += jobList[i].getTurnAround();
}
cout << "\t\t\tPriority" << endl;
display(jobList);
cout << "The average turnaround time is: " << avgTurn/jobList.size() << endl;
}
errors:
Code:
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h: In function `const class Job & __median<Job, bool (*)(Job &, Job &)>(const Job &, const Job &, const Job &, bool (*)(Job &, Job &))':
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:1304: instantiated from `__introsort_loop<Job *, Job, int, bool (*)(Job &, Job &)>(Job *, Job *, Job *, int, bool (*)(Job &, Job &))'
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:1332: instantiated from here
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:64: conversion from `const Job' to `Job &' discards qualifiers
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:64: conversion from `const Job' to `Job &' discards qualifiers
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:65: conversion from `const Job' to `Job &' discards qualifiers
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:65: conversion from `const Job' to `Job &' discards qualifiers
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:67: conversion from `const Job' to `Job &' discards qualifiers
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:67: conversion from `const Job' to `Job &' discards qualifiers
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:71: conversion from `const Job' to `Job &' discards qualifiers
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:71: conversion from `const Job' to `Job &' discards qualifiers
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:73: conversion from `const Job' to `Job &' discards qualifiers
/usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/../../../../include/g++-3/stl_algo.h:73: conversion from `const Job' to `Job &' discards qualifiers
I cant find the error. I know its happening in the algorithm STL because Im passing a the wrong data type but I cant find it.
-
April 6th, 2009, 09:24 PM
#2
Re: problem compiling with g++
I don't know if this is the problem ... but your code is not const correct
in many places. For example:
Code:
bool sizeSort( const Job & lhs, const Job & rhs){return lhs.getRunTime() < rhs.getRunTime();} //for SJN sort
bool prioritySort( const Job & lhs, const Job & rhs){return lhs.getPriority() < rhs.getPriority();} //for priority sort
-
April 6th, 2009, 09:31 PM
#3
Re: problem compiling with g++
I dont really see how that would cause a compile error unless I specified the function definition to be a const. I though using const is more for the programmers protection?
I will look through the code and add it as needed because it is good programming practice.
-
April 7th, 2009, 12:17 AM
#4
Re: problem compiling with g++
Originally Posted by lmpb17
I dont really see how that would cause a compile error
Here is your code compiled with Comeau:
Code:
Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !
Your Comeau C/C++ test results are as follows:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"concept_checks.h", line 329: error: qualifiers dropped in binding
reference of type "Job &" to initializer of type "const Job"
return __f(__first, __second);
^
detected during:
instantiation of "_Ret _STL_BINARY_FUNCTION_ERROR<_Func, _Ret,
_First,
_Second>::__binary_function_requirement_violation(_Func
&, const _First &, const _Second &) [with _Func=bool
(*)(Job &, Job &), _Ret=bool, _First=Job, _Second=Job]"
at line 1552 of "stl_algo.h"
instantiation of "void std::sort(_RandomAccessIter,
_RandomAccessIter, _Compare) [with _RandomAccessIter=Job
*, _Compare=bool (*)(Job &, Job &)]" at line 102 of
"ComeauTest.c"
"concept_checks.h", line 329: error: qualifiers dropped in binding
reference of type "Job &" to initializer of type "const Job"
return __f(__first, __second);
^
detected during:
instantiation of "_Ret _STL_BINARY_FUNCTION_ERROR<_Func, _Ret,
_First,
_Second>::__binary_function_requirement_violation(_Func
&, const _First &, const _Second &) [with _Func=bool
(*)(Job &, Job &), _Ret=bool, _First=Job, _Second=Job]"
at line 1552 of "stl_algo.h"
instantiation of "void std::sort(_RandomAccessIter,
_RandomAccessIter, _Compare) [with _RandomAccessIter=Job
*, _Compare=bool (*)(Job &, Job &)]" at line 102 of
"ComeauTest.c"
"stl_algo.h", line 135: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
if (__comp(__a, __b))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
"stl_algo.h", line 135: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
if (__comp(__a, __b))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
"stl_algo.h", line 136: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
if (__comp(__b, __c))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
"stl_algo.h", line 136: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
if (__comp(__b, __c))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
"stl_algo.h", line 138: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
else if (__comp(__a, __c))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
"stl_algo.h", line 138: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
else if (__comp(__a, __c))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
"stl_algo.h", line 142: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
else if (__comp(__a, __c))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
"stl_algo.h", line 142: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
else if (__comp(__a, __c))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
"stl_algo.h", line 144: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
else if (__comp(__b, __c))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
"stl_algo.h", line 144: error: qualifiers dropped in binding reference of
type "Job &" to initializer of type "const Job"
else if (__comp(__b, __c))
^
detected during:
instantiation of "const _Tp &std::__median(const _Tp &, const _Tp
&, const _Tp &, _Compare) [with _Tp=Job, _Compare=bool
(*)(Job &, Job &)]" at line 1525
instantiation of "void std::__introsort_loop(_RandomAccessIter,
_RandomAccessIter, _Tp *, _Size, _Compare) [with
_RandomAccessIter=Job *, _Tp=Job, _Size=int,
_Compare=bool (*)(Job &, Job &)]" at line 1556
12 errors detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
The std::sort is expecting your function to be using const arguments. Your functions violated this rule. The reason why Visual C++ doesn't give you an error is either the compiler is wrong, or it is strictly pure luck of how the VC++ std::sort is implemented when it calls your function. If you compiled with the highest warning level, you may see the same diagnostic.
As you can clearly see that other compilers (Comeau) give similar errors. Possibly, the next version of VC++ will also not let you get away with it, and will reject your code.
Second, do not put "using namespace std" in a header file. The reason is that any module that includes that header has the whole std namespace forced upon it. Instead, prepend "std::" to those identifiers in the header file.
Regards,
Paul McKenzie
-
April 7th, 2009, 10:30 AM
#5
Re: problem compiling with g++
Originally Posted by lmpb17
I dont really see how that would cause a compile error unless I specified the function definition to be a const. I though using const is more for the programmers protection?
Right. In this case, the implementation of std::sort protects you from accidently having your vector members being modified during the sort, probably by using a const_iterator. That's why it refuses to call your compare method(s) because, judging from the signature, they do modify the members passed to it.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
April 29th, 2009, 01:08 PM
#6
Re: problem compiling with g++
Havent had a chance to get back to this thread, but I get an error when changing the above functions to const. None of the other code has changed.
Code:
bool sizeSort(const Job & lhs, const Job & rhs){return lhs.getRunTime() < rhs.getRunTime();} //for SJN sort
bool prioritySort(const Job & lhs, const Job & rhs){return lhs.getPriority() < rhs.getPriority();} //for priority sort
errors:
Code:
1>c:\documents and settings\lenny\desktop\school\csi\cs332\lab4\lab4\mainprog.cpp(8) : error C2662: 'Job::getRunTime' : cannot convert 'this' pointer from 'const Job' to 'Job &'
1> Conversion loses qualifiers
1>c:\documents and settings\lenny\desktop\school\csi\cs332\lab4\lab4\mainprog.cpp(8) : error C2662: 'Job::getRunTime' : cannot convert 'this' pointer from 'const Job' to 'Job &'
1> Conversion loses qualifiers
where is the conversion in the error taking place?
-
April 29th, 2009, 01:11 PM
#7
Re: problem compiling with g++
One presumes that getRunTime() and getPriority() are not marked as const functions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|