Ok so you want to create a class Vec which will allow the code in your first block to work?
One problem I have with this is that the width value in line 11 above is absolutely essential, but you don't use it. This is the one thing I need to change in the above code.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
/* start of my contribution */
using std::istream;
using std::ostream;
template <class T>
class Vec {
T* data;
int size;
public:
// The constructor needs the size of the array to allocate it
// The destructor cleans up the memory
Vec(int width) : size(width) { data = new T[size]; }
~Vec() { delete[] data; }
// These are friend definitions to give the functions access to
// the private members: data and size
template<class U> friend istream& operator>>(istream& sin, Vec<U>& v);
template<class U> friend ostream& operator<<(ostream& o, Vec<U>& v);
};
// This function allows us to do cin >> x;
template <class T>
istream& operator>>(istream& sin, Vec<T>& v) {
for (int i = 0; i < v.size; ++i)
sin >> v.data[i];
return sin;
}
// This function allows us to do cout << x;
template <class T>
ostream& operator<<(ostream& o, Vec<T>& v) {
for (int i = 0; i < v.size; ++i)
o << v.data[i] << ' ';
return o; // returning o so that we can chain cout << x << endl << whatevs
}
/* end of my contribution */
void myVector() {
int width = 10; // Had to change these two lines
Vec<int> x(width); // Width is needed to define the size of the Vec
cout << "Vector is\n";
cin >> x;
cout << "Vector is: " << x << endl;
}
int main() {
myVector();
return 0;
}
So what we're doing here is defining a templated class Vec. It is defined with the width of the vector and will allocate the memory for you. I've overloaded the >> and << operators to work with cin and cout. These are friend functions because we have our cin/cout on the left side of the operator instead of this, yet they still need to access the private members of the Vec.
I'm still getting error in my main on line 46 that :
'Vec' was not declared in this scope
expected ';' before int
expected primary-expression before 'int'
If you want cin>>x to define the size of the array, we can do that like so (I'm removing the template just to make it a little easier for you to read):
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
/* start of my contribution */
using std::istream;
using std::ostream;
class Vec {
int* data;
int size;
public:
Vec() : size(0), data(0) {} // constructor does not much
~Vec() { if (data) delete[] data; } // destructor cleans up if req
friend istream& operator>>(istream& sin, Vec& v);
friend ostream& operator<<(ostream& o, Vec& v);
};
istream& operator>>(istream& sin, Vec& v) {
sin >> v.size; // First input size
if (v.data) delete[] v.data;
v.data = newint[v.size]; // allocate memory
for (int i = 0; i < v.size; ++i)
sin >> v.data[i]; // write to the array
return sin;
}
ostream& operator<<(ostream& o, Vec& v) {
for (int i = 0; i < v.size; ++i)
o << v.data[i] << ' ';
return o;
}
/* end of my contribution */
void myVector() { // This function is identical to yours
Vec x; // Except for the <int> which I took out to make this easier to read
int width = 10;
cout << "Vector is\n";
cin >> x;
cout << "Vector is: " << x << endl;
}
int main() {
myVector();
return 0;
}
So just throw the class definition and the operator functions into the header and just include that header in your main.cpp.
If templates are not required we could use another source file, but we'd have to make some changes to your main.cpp which I think you said cannot be touched.
#include <iostream>
#include <iomanip>
#include "Vec.h" // <-- This is the only change to main.cpp from your original one
using std::cout;
using std::cin;
using std::endl;
using std::setw;
void myVector() {
Vec<int> x;
int width = 10;
cout << "Vector is\n";
cin >> x;
cout << "Vector is: " << x << endl;
}
int main() {
myVector();
return 0;
}
That's because I changed the definition of the istream& operator>>(istream& sin, Vec<T>& v); function.
Your main function defines int width = 15; but doesn't do anything with that. It is not defining the size of the vector.
Above you said:
Stephaniei492 wrote:
input x which is a vector with size followed by numbers like "5 2 3 4 5 6"
So I updated this function to:
1 2 3 4 5 6 7 8 9 10
template <class T>
std::istream& operator>>(std::istream& sin, Vec<T>& v) {
sin >> v.size;
if (v.data) delete[] v.data;
v.data = new T[v.size];
for (int i = 0; i < v.size; ++i)
sin >> v.data[i];
return sin;
}
Now when I run the program this happens:
Vector is
2 5 10
Vector is: 5 10
We input 2 (the size), then the next 2 numbers (5, 10). Then we are able to output all numbers.