help

Pages: 12
Can you at least format your code and not spam "help" all the time?

No one understands what you're saying.
...................................................................................
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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.
Last edited on
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'
Last edited on
I re-read this again...

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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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 = new int[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;
 }


Last edited on
That error means that you probably didn't copy the code directly.

The error would occur if class Vec { /* ... */ }; is not present. This could be if you changed the name of Vec from the definition I gave you.

It's compiling fine for me.

I'm going for a moment. I'll check back in 20 min to see if you're still having problems.
Last edited on
.........................................................................................
Last edited on
................................................................................
Last edited on
Templated stuff must be in headers.

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.

Vec.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

template <class T>
class Vec {
    int size;
    T* data;
public:
    Vec() : size(0), data(0) {}
    ~Vec() { if (data) delete[] data; }

    template <class U> friend std::istream& operator>>(std::istream& sin, Vec<U>& v);
    template <class U> friend std::ostream& operator<<(std::ostream& o  , Vec<U>& v);
};

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;
}

template <class T>
std::ostream& operator<<(std::ostream& o, Vec<T>& v) {
    for (int i = 0; i < v.size; ++i)
        o << v.data[i] << ' ';
    return o; 
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
 }
Last edited on
.............................................................................................
Last edited on
That won't happen if your files are organized like I show above. Can you show me the contents of each file.

My guess is that you used the simpler non-templated class which I gave as an example, but you still call Vec<int> x; instead of Vec x; in your main.
Last edited on
......................................................................................
Last edited on
Ah,

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.
...............................................................................
Last edited on
Just post more on this forum. I check it frequently.
OP PM'd me to ask how to sort the numbers in ascending order before printing them.

This solution will use std::sort() from <algorithm> when we insert the numbers. This ensures that the numbers are sorted at all times.


Here's the new Vec.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <algorithm>

template <class T>
class Vec {
    int size;
    T* data;
public:
    Vec() : size(0), data(0) {}
    ~Vec() { if (data) delete[] data; }

    template <class U> friend std::istream& operator>>(std::istream& sin, Vec<U>& v);
    template <class U> friend std::ostream& operator<<(std::ostream& o  , Vec<U>& v);
};

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];

    std::sort(v.data, v.data+v.size);
    return sin;
}

template <class T>
std::ostream& operator<<(std::ostream& o, Vec<T>& v) {
    for (int i = 0; i < v.size; ++i)
        o << v.data[i] << ' ';
    return o; 
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12