Some C++ source code I wrote compiled and ran fine on Windows 7 using Visual Studio 2010. But when I tried it on Mac OS X 10.8 Mountain Lion using XCode 4.4.1, it would not compile. I was able to fix it as detailed below, but am interested to find out what I was doing wrong in the first place and why the fix was needed.
Specifically, consider the following class declaration:
class airline {
vector <passenger> persons;
int npassengers;
public:
airline () {};
airline (int np);
~airline () {};
// other functions
} ;
The constructor for airline
that takes an int parameter is:
airline::airline (int np) {
npassengers = np;
persons.resize (npassengers);
return;
}
On the Windows machine, this compiled and ran with no problems. However, XCode complained bitterly that I did not have a viable constructor for "passenger". I have a default constructor for passenger, so that wasn't the issue. Based on something I found in an earlier question on this site I also tried using persons.erase instead of resize, with no luck.
Ultimately I was able to get the code to compile on the Mac by using persons.vector::resize (npassengers);
instead of persons.resize (npassengers);
. I am wondering why it was necessary to do that on the Mac side, and why it worked without a problem on the Windows side.
A related question is, is it bad practice to have using namespace std;
at the top of your code, and instead, should I be specifying std::cout
etc.? If so, why?
Please bear with my lack of knowledge, and thanks for your help!
Edit: Sean suggested I show my code for passenger, so here goes:
class passenger {
string plast;
string pfirst;
int row;
char seatno;
int flightno;
public:
passenger ();
passenger(string line );
passenger (passenger const &rhs);
~passenger() {};
} ;
passenger::passenger (passenger const &rhs) {
plast = rhs.plast;
pfirst = rhs.pfirst;
row = rhs.row;
seatno = rhs.seatno;
flightno = rhs.flightno;
return;
}
passenger::passenger( string line ) {
// tokenizes string to obtain passenger data
// seems to work fine, actual code is too long to include
// and doesn't seem relevant here
}
passenger::passenger() {
plast.clear();
pfirst.clear();
row = 0;
seatno = '\0';
flightno = 0;
return;
}
Plus of course the usual accessors and overloaded insertion operator, etc. Thanks!