When I run my code I get this error and I don't know how to fix it.
error: request for member 'push_back' in 'v', which is of non-class type 'Vector<int>()'|
Here's my updated code.
Vector.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED
template <class T>
class Vector
{
private:
unsigned int my_size;
unsigned int my_capacity;
T * buffer;
public:
//Vector;
Vector();
void push_back(const T & x);
int resize(int size);
};
#endif // VECTOR_H_INCLUDED
|
Vector.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
|
#include <iostream>
#include "Vector.h"
using namespace std;
template <class T>
Vector<T>::Vector()
{
my_size = 0;
my_capacity = 0;
buffer = nullptr;
}
template <class T>
void Vector<T>::push_back(const T &x)
{
if(my_size == my_capacity)
{
resize(my_capacity+5);
}
buffer[my_size] = x;
my_size++;
}
template <class T>
int Vector<T>::resize(int size)
{
if(buffer == nullptr)
{
buffer = new T[size];
my_capacity = size;
}
else{
T * newBuffer = new T[size];
for(int i = 0; i < my_size; i++)
{
newBuffer[i] = buffer[i];
}
delete [] buffer;
buffer = newBuffer;
}
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <string>
#include <cassert>
#include "Vector.h"
using namespace std;
int main()
{
Vector<int> v;
//v.push_back(8);
}
|
Last edited on
Vector<int> v();
Declares a function taking no arguments and returning Vector<int>
.
You probably meant Vector<int> v;
. This line return an error because you do not have a default constructor for your class.
Last edited on
That is declaration. Where is it defined?
The error is on line 69. The parameter requires const: push_back(const T &x)
You have several more errors in resize(...).
On line 48: missing *
On line 49: Remove buffer.
Line 55 and the loop on line 57 don't make sense. Replace it with buffer = newBuffer;
Last edited on
Thanks coder that makes more sense. I still don't understand my error for using this.
Here's my updated code.
Vector.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED
template <class T>
class Vector
{
private:
unsigned int my_size;
unsigned int my_capacity;
T * buffer;
public:
//Vector;
Vector();
void push_back(const T & x);
int resize(int size);
};
#endif // VECTOR_H_INCLUDED
|
Vector.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
|
#include <iostream>
#include "Vector.h"
using namespace std;
template <class T>
Vector<T>::Vector()
{
my_size = 0;
my_capacity = 0;
buffer = nullptr;
}
template <class T>
void Vector<T>::push_back(const T &x)
{
if(my_size == my_capacity)
{
resize(my_capacity+5);
}
buffer[my_size] = x;
my_size++;
}
template <class T>
int Vector<T>::resize(int size)
{
if(buffer == nullptr)
{
buffer = new T[size];
my_capacity = size;
}
else{
T * newBuffer = new T[size];
for(int i = 0; i < my_size; i++)
{
newBuffer[i] = buffer[i];
}
delete [] buffer;
buffer = newBuffer;
}
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <string>
#include <cassert>
#include "Vector.h"
using namespace std;
int main()
{
Vector<int> v;
//v.push_back(8);
}
|
Last edited on