Im practicing creating a class like this:
Implement a vector replacement that operates only on integers, vectorOfInt (you don't need to
use templates like the normal STL). Your class should have the following interface:
• A no-argument constructor that allocates a 32 element vector
• A constructor that takes an initial size as the argument
• A method get, taking an index as returning the value at that index
• A method set, that takes an index and a value, and sets the value at that index
• A method pushback that adds an element to the end of the array, resizing if necessary
• A method pushfront that adds an element to the beginning of the array
• A Copy constructor and assignment operator
I compiled and it keep saying that:
passing 'const vectorOfInt' as 'this' argument of 'int vectorOfInt::get(int)' discards qualifiers [-fpermissive] on line 65
This is the vectorOfInt.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef VECTOROFINT_H
#define VECTOROFINT_H
class vectorOfInt
{
public:
vectorOfInt();
vectorOfInt(int inputSize);
int get(int v_index);
void set(int v_index, int value);
void pushback(int value);
void pushfront(int value);
vectorOfInt& operator= (const vectorOfInt& aVector);
vectorOfInt(vectorOfInt& aVector);
int size();
private:
int v_size;
int* p_vector;
};
#endif // VECTOROFINT_H
|
This is the vectorOfInt.cpp
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream>
#include "vectorOfInt.h"
//initiating all the elements in the vector to 0
vectorOfInt::vectorOfInt()
{
v_size = 32;
p_vector = new int[v_size];
for (int i = 0; i < v_size; i++)
p_vector[i] = 0;
}
vectorOfInt::vectorOfInt(int inputSize)
{
v_size = inputSize;
p_vector = new int[v_size];
for (int i = 0; i < v_size; i++)
p_vector[i] = 0;
}
int vectorOfInt::get(int v_index)
{
return p_vector[v_index];
}
void vectorOfInt::set(int v_index, int value)
{
p_vector[v_index] = value;
}
void vectorOfInt::pushback(int value)
{
for (int i = 0; i < v_size; i++)
{
if (p_vector[i] == 0)
{
p_vector[i] = value;
return;
}
}
int* p_new = new int[++v_size];
for (int i = 0; i < v_size-1; i++) //copy all elements from p_vector to p_new;
{
p_new[i] = p_vector[i];
}
p_new[v_size-1] = value;
delete[] p_vector;
p_vector = p_new;
}
void vectorOfInt::pushfront(int value)
{
if (p_vector[v_size-1] != 0)
v_size++;
int* p_new = new int[v_size];
p_new[0] = value;
for (int i = 1; i < v_size; i++)
{
p_new[i] = p_vector[i];
}
delete[] p_vector;
p_vector = p_new;
}
vectorOfInt& vectorOfInt::operator=(const vectorOfInt& aVector)
{
delete p_vector;
for (int i = 0; i < v_size; i++)
{
p_vector[i] = aVector.get(i);
}
return *this;
}
vectorOfInt::vectorOfInt(vectorOfInt& aVector)
{
p_vector = new int[aVector.size()];
for (int i = 0; i < v_size; i++)
{
p_vector[i] = aVector.get(i);
}
}
int vectorOfInt::size()
{
return v_size;
}
|
Plz help.