I'm facing problem with operator overloading during constructor call - c++

Here I've implemented a class called Vector and tried to overload = operator with a parameter initialized_list.
#include<iostream>
using namespace std;
template<class T>
class Vector
{
private:
T* arr;
int _size;
int capacity;
public:
Vector();
~Vector();
Vector<T>& operator = (const initializer_list<T>& l);
};
template<class T>
Vector<T>& Vector<T>::operator = (const initializer_list<T>& l)
{
_size=capacity=l.size();
arr=new T[l.size()];
return *this;
}
template<class T>
Vector<T>::Vector()
{
_size=0;
capacity=0;
}
template<class T>
Vector<T>::~Vector()
{
delete[] arr;
}
In this implementation if I run with the code below it works fine.
int main()
{
Vector<int>v;
v={1,2,3};
}
But if I want to run with this code below it shows error: could not convert '{1, 2, 3}' from '<brace-enclosed initializer list>' to 'Vector<int>'
int main()
{
Vector<int>v={1,2,3};
}
How can I fix this problem to make it work as the code above?

Related

error use of deleted function when trying to pass rvalue to a tuple

Original context:
I am trying to pass a tuple of (object, expected_value_of_some_property) to a test function
I created a simple class to reproduce the error I am facing:
template <typename T>
class Vector
{
private:
size_t m_size;
std::unique_ptr<std::vector<int>> m_vector;
public:
Vector();
Vector(const Vector<int>&);
~Vector() = default;
void push_back(T);
T get(int) const;
size_t size() const;
};
template <typename T>
Vector<T>::Vector()
:m_size(0),
m_vector(new std::vector<T>)
{}
template <typename T>
Vector<T>::Vector(const Vector<int>& v)
:m_size(v.size()),
m_vector(new std::vector<T>)
{
for (size_t i = 0; i < v.size(); i++)
{
this->push_back(v.get(i));
}
}
template <typename T>
void Vector<T>::push_back(T value)
{
m_vector->push_back(value);
m_size ++;
}
template <typename T>
T Vector<T>::get(int index) const
{
return m_vector->operator[](index);
}
template <typename T>
size_t Vector<T>::size() const
{
return m_size;
}
The error is triggered when trying to produce a tuple of Vector object and an int somewhere in a test code:
int main(int argc, char const *argv[])
{
std::tuple<Vector<int>, int> vector_test;
vector_test = std::make_tuple(Vector<int>{}, 0);
int size = std::get<0>(vector_test);
Vector<int> vector = std::get<1>(vector_test);
// .. test code
return 0;
}
Output:
error: use of deleted function ‘std::tuple<Vector<int>, int>& std::tuple<Vector<int>, int>::operator=(const std::tuple<Vector<int>, int>&)’
20 | vector_test = std::make_tuple(Vector<int>{}, 0);
| ^ ^
What am I doing wrong here?
Since your Vector class has a unique_ptr member, that means your class itself has the copy-assignment implicitly deleted. Therefore this assignment will fail
std::tuple<Vector<int>, int> vector_test;
vector_test = std::make_tuple(Vector<int>{}, 0); // <--- this
Instead you can just directly initialize in one step
std::tuple<Vector<int>, int> vector_test = std::make_tuple(Vector<int>{}, 0);

Error: Class A is not a base of Class B C++

When I try to call the function in line **
through this function:
template<class T,int SIZE>
T const& Array<T,SIZE>::const_iterator::operator*() const {
return this->Array<T,SIZE>::iterator::operator*();
}
It gives me the following error:
error: 'Array<int, 3>::iterator' is not a base of 'const Array<int, 3>::const_iterator'
Can anyone explain why?
template <class T, int SIZE>
class Array {
T data[SIZE];
public:
explicit Array();
Array(const Array& a); //copy constructor
~Array(); //destructor
class iterator {
Array<T,SIZE>* array;
int index;
public:
iterator(Array<T,SIZE>* array,int index);
T& operator*() const; //**
};
class const_iterator {
const Array<T,SIZE>* array;
int index;
public:
T const& operator*() const;
};
}

How to create a class which inheritates from std::vector

I need to inherit all functions from std::vector and I want to overload operators to make a complete matrix class.
There is not too much documentation on this topic.
Matriz.h
#include <vector>
#include <iostream>
using namespace std;
template<typename T>
class Matriz:vector<T>
{
public:
using vector<T>::vector;
private:
}
Matriz.cpp
int main()
{
Matriz<int> dani;
dani.push_back(2); //Here is the error and I don`t know what it happens
}
When I want to initialize it, I got an error.
Severity Code Description Project File Line Suppression State
Error C2247 'std::vector<int,std::allocator<_Ty>>::push_back' not accessible because 'Matriz<int>' uses 'private' to inherit from 'std::vector<int,std::allocator<_Ty>>'
This should work:
#include <vector>
#include <iostream>
template<typename T>
class Matriz: public std::vector<T>
{
public:
using std::vector<T>::vector;
private:
};
int main()
{
Matriz<int> dani;
dani.push_back(2);
dani.push_back(3);
for(const auto& it: dani)
std::cout << it << " ";
}
Inherit from vector class is a mistake
It is very difficult and produces a lot of errors as it is said here.
I have made a class which has a vector and inlcudes:
template
operator overloading
matrix operations
LINK : vector.h
#pragma once
#include <vector>
#include <iostream>
template<class T>
class Vector
{
public:
Vector();
Vector(int);
Vector(int, int);
~Vector();
std::vector<std::vector<T>> v;
bool Check_Size() ;
template<typename T1> bool Check_Size(std::vector<T1>&) ;
template<typename T1> bool Check_Size_Fast(std::vector<T1>&);
void Print();
void Transponse();
void Inverse();
void Inverse2();
void Inverse3();
template<class T,class Q>
friend std::vector<std::vector<T>> operator* (const Q , Vector<T> );
template<class T,class Q>
friend std::vector<std::vector<T>> operator* (Vector<T> , const Q );
template<class T>
friend std::vector<std::vector<T>> operator*(Vector<T>& , Vector<T>&);
template<typename T>
friend std::vector<std::vector<T>> operator+(Vector<T> &, Vector<T> &);
template<typename T>
friend std::vector<std::vector<T>> operator-(Vector<T> &, Vector<T> &);
Vector<T>& operator = (const std::vector<std::vector<T>>& v)
{
this->v = v;
return *this;
}
std::vector<std::vector<T>>& operator +=( Vector<T>&v) {
return v + (*this);
}
std::vector<std::vector<T>>& operator -=(Vector<T>&v) {
return v - (*this);
}
std::vector<std::vector<T>>& operator *=(Vector<T>&v) {
return v * (*this);
}
private:
void Recursive_Check(std::vector<T>&);
};

C++ constructor overloading error unable to match function definition

I am using Microsoft Visual Studios and I have create a generic class List_Array. There is no problem with the default constructor, but the other two (overloaded) constructors are generating the error.
//List_Array.h
template<typename T>
class List_Array {
private:
int size; ...
T* data;
public:
List_Array<T>::List_Array();
List_Array<T>::List_Array(int);
List_Array<T>::List_Array(const T&, int);
...
};
template<typename T>
List_Array<T>::List_Array() { }
template<typename T>
List_Array<T>::List_Array(int s) {
this->size = s
this->data = new T[s];
}
template<typename T>
List_Array<T>::List_Array(const T& init, int s){
this->size = s;
this->data = new T[s];
for (int i = 0; i < s; i++){
this->data[i] = init;
}
}
I get a C2244 'List_Array::List_Array': unable to match function definition to an existing declaration
Any help is much appreciated!
The problem has nothing to do with template or overloading. You just don't need List_Array<T>:: part for the member function declaration inside class definition. i.e.
template<typename T>
class List_Array {
private:
int size; ...
T* data;
public:
List_Array();
List_Array(int);
List_Array(const T&, int);
...
};
LIVE

Custom Vector Class: Failure to Return After Sum

Related to this question that I asked yesterday.
I'm building my own vector class to learn a little more about C++, and I'm having some difficulty implementing an overloaded operator+ that I can use to add vectors. Here's what my class looks like so far:
template<typename T>
class vector;
template<typename T>
vector<T> operator+(const vector<T>&, const vector<T>&);
template<typename T>
class vector
{
private:
T* pointer_;
unsigned long size_;
public:
// Constructors and destructors.
vector();
template<typename A> vector(const A&);
template<typename A> vector(const A&, const T&);
vector(const vector<T>&);
~vector();
// Methods.
unsigned long size();
T* begin();
T* end();
vector<T>& push_back(const T&);
// Operators.
T operator[](unsigned long);
vector<T>& operator=(const vector<T>&);
friend vector<T> operator+<>(const vector<T>&, const vector<T>&);
};
template<typename T>
vector<T> operator+(const vector<T>& lhs, const vector<T>& rhs)
{
vector<T> result(lhs.size_);
for (unsigned long i = 0; i != result.size_; i++)
{
result.pointer_[i] = lhs.pointer_[i] + rhs.pointer_[i];
}
return result;
}
The constructor vector(const A& size, const T& value) operates as expected, returning a vector with size elements initialized to value. operator= appears to be working, as well. When I try something like this, however, my program doesn't perform as expected:
#include <iostream>
#include "vector.h"
int main()
{
vector<int> test(5, 2); // Create a vector with five elements initialized to 2.
vector<int> test2(5, 3); // Create a vector with five elements initialized to 3.
std::cout << test.size() // Prints 5, as expected.
std::cout << test2.size() // Prints 5, as expected.
vector<int> try_result = test + test2;
std::cout << try_result.size() // Prints 0--why?
}
My question: if operator+ returns a copy of result (and not by reference, a problem that originally existed in my code), why does it appear that try_result still does not point to the proper location in the heap?
EDIT: Adding the constructor code (I've tested this, and it seems to work, but maybe for the wrong reasons):
template<typename T> template<typename A>
vector<T>::vector(const A& size)
{
this->pointer_ = new T[size];
this->size_ = size;
}
template<typename T> template<typename A>
vector<T>::vector(const A& size, const T& initial_value)
{
this->pointer_ = new T[size];
this->size_ = size;
for (unsigned long i = 0; i != this->size_; i++)
{
pointer_[i] = initial_value;
}
}
EDIT2: Adding the copy constructor.
template<typename T>
vector<T>::vector(const vector<T>& replicate_vector)
{
delete[] this->pointer_;
this->pointer_ = new T[replicate_vector.size_];
this->size_ = replicate_vector.size_;
for (unsigned long i = 0; i != this->size_; i++)
{
this->pointer_[i] = replicate_vector.pointer_[i];
}
}