Search code examples
c++classpush-back

Print Implementation fails right after pushback (vector)


I am new to C++ and vectors and so, I am facing some trouble with this function not printing the desired values after a push back. I am receiving what I believe to be garbage values. Could it be because I am working with unsigned char and not integer types? I don't know. I need to get it to work for my school assignment that is to be due soon and I am really stuck on this. Could anyone here be able to resolve this problem? Thanks!

Desired Output:

********** TestSubscript1 **********
Construct from unsigned char array:
2  4  6  6  8  10  6  12  234  14  16  6  6  (size=13, capacity=16)

My Output:

********** TestSubscript1 **********
Construct from unsigned char array:

  ê     (size=13 capacity=16)

Header file(My implementation):

template <typename T>
vec 
{

private:
T* v;
int count;
int capacity;
public:

vector(){
    capacity=0;
    v = nullptr;
    count = 0;
}
void push_back(const T& t)
{
    if(count+1>capacity)
{
    capacity = std::max(2*capacity, 1);

    T* newData = new T[capacity];
    for(int i=0; i <count; i++)
    {
        newData[i] = v[i];
    }
    delete[] v;
    v = newData;
}
v[count++] = t;
}
T& operator[](size_t num) const
{   
    return v[num];
}

template <typename T1>

friend void Print(const vector<T1>& s);

};
template <typename T1>
void Print(const vector<T1>& s)
{
    for(int i = 0; i < s.count; i++) 
    {
    std::cout<<s.v[i]<<" ";
    }
    std::cout<< "(size=" << s.count << " " << "capacity=" << s.capacity << 
 ")";
    std::cout<<std::endl; 
    }

main file(Remains unchanged):

#include <iostream>
#include <cstdlib>              // atoi
#include "cs150_vect.h"

void TestSubscript1(void)
{
std::cout << "\n********** TestSubscript1 **********\n";
unsigned char ia[] = { 2, 4, 6, 6, 8, 10,
                       6, 12, 234, 14, 16, 6, 6
                     };
int size = sizeof(ia) / sizeof(*ia);
std::cout << "Construct from unsigned char array:\n";
cs150::vector<unsigned char> temp_vec;
for(int i=0; i<size; ++i)
    temp_vec.push_back(ia[i]);

const cs150::vector<unsigned char> a(temp_vec);
Print(a);
}

Solution

  • Yes it's because you are working with char data. operator<< has overloads for char and unsigned char which print these values as characters not numbers. Since you want numbers just switch to int from unsigned char.

    int ia[] = { 2, 4, 6, 6, 8, 10,
                           6, 12, 234, 14, 16, 6, 6
                         };
    int size = sizeof(ia) / sizeof(*ia);
    std::cout << "Construct from int array:\n";
    cs150::vector<int> temp_vec;
    

    If you've done your template right, then no other changes should be necessary.

    If you really want to stick with char data, then one way would be to write a specialization of your Print function for unsigned char, so that it prints chars as numbers.

    template <>
    inline void Print(const vector<unsigned char>& s)
    {
        for(int i = 0; i < s.count; i++) 
        {
            std::cout<<(unsigned)s.v[i]<<" "; // print chars as numbers
        }
        std::cout<< "(size=" << s.count << " " << "capacity=" << s.capacity << ")";
        std::cout<<std::endl; 
    }
    

    This specialization is in addition to the generic version. It says that when T is unsigned char use this version instead of the generic version.

    Not completely sure whether you need to declare this version as a friend or not. No doubt you'll find that out.