Differences in Compilers/IDEs that result in errors/no errors

Status
Not open for further replies.

shut

Ars Praetorian
560
Alright, while doing work at home, I use GCC with TextPad for whatever coding assignments II have. While at school, I have the "luxury" of using metrowerks codewarrior 4 and/or GCC. To keep consistency, I use GCC.Textpad at school too.

Here's where a couple of weird problem arise :

code:<HR>
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;

class OutRecord {
	public :
	string first, last;
	float gpa;
	bool operator<(const OutRecord& x) {
		return gpa < x.gpa;
	}
};

float GradeReturn(char input, char input2);


int main (void) {

	ifstream in("mayers.in");
	ofstream out("mayers.out");

	int s;

	in >> s;

	OutRecord studs[1000]; 

	for (int i=0;i<s;i++) {
		int loop=0;
		float junk=0;
		float credits=0;
		float tempcreds=0;
		float tempgrade=0;

		in >> studs[i].first >> studs[i].last >> loop;


		for (int j=0;j<loop;j++) {
			int credits;
			string lgrade;
			string dummy;
			in >> dummy >> credits >> lgrade;
			if(lgrade.length()==2) {
			junk=GradeReturn(lgrade[0], lgrade[1]);
			} else {
			junk=GradeReturn(lgrade[0], 'J');
			}
			tempcreds+=credits;
			tempgrade+=(credits*junk);

		}

		studs[i].gpa=(tempgrade/tempcreds);

	}

	sort(studs, studs+s);

	for(--s;s>=0;s--) {
		out << studs[s].last << " "  << studs[s].first << " " << studs[s].gpa << endl;
		}

	return 0;

}

float GradeReturn(char input, char input2) {

	float returnval=0;

	switch(input) {
		case 'A' : returnval=4;
			break;
		case 'B' : returnval=3;
			break;
		case 'C' : returnval=2;
			break;
		case 'D' : returnval=1;
			break;
		default : return 0;
			break;
	}

	switch(input2) {
		case '+' : returnval+=.3;
			break;
		case '-' : returnval-=.3;
			break;
		default : returnval+=0;
			break;
	}

	return returnval;

}

This code compiles and runs perfectly under Metrowerks with no errors. However, under GCC/GDB , I get several errors, which to my studnet mind, do not seem to aid my debugging processes to find the error in why GCC does not work on this code, yet Metrowerks does.

THis pertains to issues with GCC, not with my code... hopefully.

code:<HR>
Code:
c:/djgpp/lang/cxx-v3/bits/stl_algo.h: In function `const _Tp& 
   std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = OutRecord]':
c:/djgpp/lang/cxx-v3/bits/stl_algo.h:1634:   instantiated from `void std::__introsort_loop(_RandomAccessIter, _RandomAccessIter, _Tp*, _Size) [with _RandomAccessIter = OutRecord*, _Tp = OutRecord, _Size = int]'
c:/djgpp/lang/cxx-v3/bits/stl_algo.h:1672:   instantiated from `void std::sort(_RandomAccessIter, _RandomAccessIter) [with _RandomAccessIter = OutRecord*]'
Z:/code/junk/mayers.cpp:66:   instantiated from here
c:/djgpp/lang/cxx-v3/bits/stl_algo.h:77: passing `const OutRecord' as `this' 
   argument of `bool OutRecord::operator<(const OutRecord&)' discards 
   qualifiers
c:/djgpp/lang/cxx-v3/bits/stl_algo.h:78: passing `const OutRecord' as `this' 
   argument of `bool OutRecord::operator<(const OutRecord&)' discards 
   qualifiers
c:/djgpp/lang/cxx-v3/bits/stl_algo.h:80: passing `const OutRecord' as `this' 
   argument of `bool OutRecord::operator<(const OutRecord&)' discards 
   qualifiers
c:/djgpp/lang/cxx-v3/bits/stl_algo.h:84: passing `const OutRecord' as `this' 
   argument of `bool OutRecord::operator<(const OutRecord&)' discards 
   qualifiers
c:/djgpp/lang/cxx-v3/bits/stl_algo.h:86: passing `const OutRecord' as `this' 
   argument of `bool OutRecord::operator<(const OutRecord&)' discards 
   qualifiers

What is the difference within Metrowerks and GCC (I know, probably a world of differences, but this is.. queer) that would cause these problems to arise?
 

shut

Ars Praetorian
560
Originally posted by euzeka:
The Metrowerks 4 compiler is old and severely broken. It'll let through all kinds of bad code. There was even a patch for it (for some other bug) that disabled the effect of 'const' in a lot of circumptances.

Then how come such a simple use of classes/operator overload gets passed, even when shown that it does work for the purposes (parsing a text full of grades and sorting by GPA).. maybe I could be educated on why my stuff is wrong.. oh, Borland command line compiler breaks it as well..
 

technophile

Ars Legatus Legionis
23,768
Just to complicate the situation:
Microsoft Visual C++ 6.0 Enterprise Edition, Service Pack 5 gives the following:
code:<HR>
Code:
Compiling...
test.cpp
c:\test.cpp(85) : warning C4305: '+=' : truncation from 'const double' to 'float'
c:\test.cpp(88) : warning C4305: '-=' : truncation from 'const double' to 'float'

test.obj - 0 error(s), 2 warning(s)

So my guess is that it's a bug in gcc (believe me, they're not unknown... I ran headfirst into one involving, IIRC, templates last year) and not one in CodeWarrior.
 
code:<HR>
Code:
bool operator<(const OutRecord& x)
{
    return gpa < x.gpa;
}
That operator needs to be a const method, so it can be used to compare two const objects. That's what I mean by Metrowerks 4 not caring much about const. It doesn't mean the resulting code will not work (although sometimes it can), it just means that it's not flagging things it should be. The correct version would be:
code:<HR>
Code:
bool operator<(const OutRecord& x) const
{
    return gpa < x.gpa;
}
If you get other errors, get back here :)
 
Status
Not open for further replies.