C++Question2005-09-09 22:55:40
For the following code, running Solaris gives proper output but errors on windows on for_loop cout in the try block.
Any idea?

//main file
#include
#include "vector.h"
using namespace std;

void f(Vector a);

int main() {

Vector v; // vectors of dafault (initial) capacity

// Add elements to v.
for (int i=0; i<20; i++) v.add(i);
cout << endl << "v is:" << endl;
for (int i=0; i cout << endl;

cout << "\nPassing v to f by value:" << endl;
f(v); // Note: passing by value.
cout << "After return, v is:" << endl;
try { // using exception catch to teminate loop
for (int i=0; ; i++) cout << v[i] << " ";
} catch (out_of_bounds) { }
cout << endl;

return 0;
}

void f(Vector a) { // Note that a is by value.

a[a.size()-5] = 62;
cout << "After a[a.size()-5] = 62, a is:" << endl;
for (int i=0; i cout << endl;
}

///////////////////////////// .cpp file//////////////////////////////////
#include "stdafx.h"

#include
#include "vector.h"
using namespace std;

// Constructor:

Vector::Vector(int initCapacity) {
// Construct a Vector with the indicated initial capacity.
// (Implicit conversion not allowed.)
count = 0;
capacity = initCapacity;
data = new int[capacity];
}


// Accessors:

const int & Vector::operator[](int i) const {
// Return a const reference to element [i] (for R-value).
// Throw out_of_bounds if i is out of bounds.

if (i<0 || i>=count) throw (out_of_bounds)0;
else return data[i];
}


int Vector::size() const { return count; }
// Return the current number of elements.


// Mutators:

void Vector::add(int x) {
// Add x to the vector (at the end).

if (count >= capacity) expand();
data[count++] = x;
}


int & Vector::operator[](int i) {
// Return a reference to element [i] (for L-value).
// Throw out_of_bounds if i is out of bounds.

if (i<0 || i>=count) throw (out_of_bounds)0;
else return data[i];
}


void Vector::expand() {
// (private) Double the capacity of the vector.

capacity *= 2;
int *newData = new int[capacity];
for (int i=0; i delete [] data;
data = newData;
}

// Destructor

Vector::~Vector() {

delete [] data;
}

/////////////////// .h file////////////////////////////
#ifndef _VECTOR_H
#define _VECTOR_H

using namespace std;

enum out_of_bounds { };

class Vector {

private:

int *data; // vector elements
int count; // number of elements currently in vector
int capacity; // current capacity

void expand();

public:

// Constructor:

explicit Vector(int capacity = 10);
// Construct a Vector with the indicated initial capacity.
// (Implicit conversion not allowed.)


// Accessors:

const int & operator[](int i) const;
// Return a const reference to element [i] (for R-value).
// If i is out of bounds, throw out_of_range exception.

int size() const; // Return the current number of elements.

// Mutators:

void add(int x);
// Add x to the vector (at the end).
// If x cannot be added (out of space) a bad_alloc
// exception is thrown.

int & operator[](int i);
// Return a reference to element [i] (for L-value).
// If i is out of bounds, throw out_of_range exception.

// Destructor:

~Vector();


};



#endif
Grant2005-09-10 00:17:09
They have different compilors