Error C2280 : Class::Class(void) : Attempting to reference a deleted function - c++

So, I am working on a project, and I have two files in this project:
main.cpp, matrix.h
The problem is that My code seemed to work perfectly a few hours ago, and now it doesn't
main.cpp:
#include <iostream>
#include "matrix.h"
#include <vector>
int main() {
Matrix f;
f.create(10, 1, {3, 4, 5, 6, 7, 8, 9});
}
matrix.h:
#pragma once
#include <iostream>
#include <string>
#include <Windows.h>
#include <vector>
class Matrix {
public:
const size_t N;
bool ifMatrixCreated;
const char* NOTENOUGH = "The size of the array should match to the width*height elements";
std::vector<int> arr;
int w, h;
void create(int width, int height, const std::vector<int> a) {
w = width;
h = height;
if (a.size() != width * height) {
ifMatrixCreated = false;
std::cout << "bello";
}
else {
ifMatrixCreated = true;
arr = a;
std::cout << "hello";
}
}
};
And when I compile, it generates this error (Using VS2019):
Error C2280 | 'Matrix::Matrix(void)': attempting to reference a deleted function Matrix | Line 5
It keeps saying that "The default constructor of Matrix cannot be referenced - It is a deleted function"
Can you help solve this error?
Thanks in advance.

Here is the correct working example. The error happens because every const data member must be initialized. And
The implicitly-declared or defaulted default constructor for class T is undefined (until C++11)defined as deleted (since C++11) if any of the following is true:
T has a const member without user-defined default constructor or a brace-or-equal initializer (since C++11).
#pragma once
#include <iostream>
#include <string>
//#include <Windows.h>
#include <vector>
class Matrix {
public:
//const size_t N;//this const data member must be initialised
const size_t N = 6;
bool ifMatrixCreated;
const char* NOTENOUGH = "The size of the array should match to the width*height elements";
std::vector<int> arr;
int w, h;
void create(int width, int height, const std::vector<int> a) {
w = width;
h = height;
if (a.size() != width * height) {
ifMatrixCreated = false;
std::cout << "bello";
}
else {
ifMatrixCreated = true;
arr = a;
std::cout << "hello";
}
}
};

Related

overloading assignment operator and header file in c++

i want to add a overloaded assignment operator to my object class in c++ but when I do this
Cabinet& Cabinet::operator=( const Cabinet& right ) {
if(&right != this){
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
this->chemicals[i][j] = right.chemicals[i][j];
}
}
}
return *this;
}
and with a header file like this
using namespace std;
#include <stdlib.h>
#include <string>
#include <iostream>
#include "Chemical.h"
class Cabinet{
private:
int rows;
int id_cabinet;
int columns;
Chemical*** chemicals;
string alphabet [9];
public:
Cabinet(int id = 0, int rows = 0, int columns = 0);
~Cabinet();
int getRow();
int getColumn();
int plusCount();
};
when compiling I get a compile error that says:
Cabinet.cpp:146:19: error: definition of implicitly declared copy assignment operator
You need to declare the function in your header file so you can define it later on.
using namespace std;
#include <stdlib.h>
#include <string>
#include <iostream>
#include "Chemical.h"
class Cabinet{
private:
int rows;
int id_cabinet;
int columns;
Chemical*** chemicals;
string alphabet [9];
public:
Cabinet(int id = 0, int rows = 0, int columns = 0);
Cabinet& operator=( const Cabinet& right );
~Cabinet();
int getRow();
int getColumn();
int plusCount();
};

Overloading [] operator c++

I am trying to overload the indexing operator for a c++ class but I am not able to do so. When I try to index my Matrix class, I get the following error:
error: cannot convert 'Matrix' to 'double*' in initialization
This error occurs on the 9th line of my main.cpp. Does it seem that the indexing does not seem to be recognized by the compiler?
Below is my code:
Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std;
class Matrix
{
public:
/** Default constructor */
Matrix(unsigned int num_cols, unsigned int num_rows);
/** Default destructor */
virtual ~Matrix();
/** Access num_cols
* \return The current value of num_cols
*/
unsigned int getCols() { return _num_cols; }
/** Access num_rows
* \return The current value of num_rows
*/
unsigned int getRows() { return _num_rows; }
double operator[](unsigned int index);
protected:
private:
unsigned int _num_cols; //!< Member variable "num_cols"
unsigned int _num_rows; //!< Member variable "num_rows"
double ** _base;
};
#endif // MATRIX_H
Matrix.cpp
#include "Matrix.h"
Matrix::Matrix(unsigned int num_cols, unsigned int num_rows){
_num_cols = num_cols;
_num_rows = num_rows;
if(_num_cols > 0) {
_base = new double*[_num_cols];
for(unsigned int i = 0; i < _num_cols; i++) {
_base[i] = arr;
cout << _base[i] << endl;
}
}
}
double* Matrix::operator[](int index) {
if (index >= _num_cols) {
cout << "Array index out of bound, exiting";
exit(0);
}
return _base[index];
}
Matrix::~Matrix()
{
//dtor
}
main.cpp
#include <iostream>
#include "Matrix.h"
using namespace std;
int main()
{
Matrix * m = new Matrix(1,2);
double * d = m[1];
delete m;
return 0;
}
The declaration of the overloaded operator member function doesn't match the definition.
You defined the operator as:
double* Matrix::operator[](int index) {
But declared it as:
double operator[](unsigned int index);
The declaration should be:
double *operator[](int index);
Also, the problem with this line:
double * d = m[1];
Is that m is a pointer to a Matrix and the [] operator works on the class instance, not a pointer to it, so you need to dereference m:
double * d = (*m)[1];
Or you can define m as an instance of a Matrix:
Matrix m(1,2);
double * d = m[1];

How to access class variable inside a vector iterator?

I'd like to access a public variable of a class instance, where the instances are kept in a vector of the class type. I have to run through all elements of vector using an iterator, but it confuses me as to how I get the variables with the iterator present. I'm using C++98.
source.cpp:
#include <iostream>
#include <vector>
#include "Rectangle.h"
using namespace std;
int main() {
int len = 2, hen = 5;
int len2 = 4, hen2 = 10;
Rectangle rect1(len, hen);
Rectangle rect2(len2, hen2);
vector<Rectangle> Rects;
Rects.push_back(rect1);
Rects.push_back(rect2);
for (std::vector<Rectangle>::iterator it = Rects.begin(); it != Rects.end(); ++it) {
//how to access length and height here?
}
system("pause");
return 0;
}
Rectangle.h:
#pragma once
class Rectangle
{
private:
public:
int length;
int height;
Rectangle(int& length, int& height);
~Rectangle();
};
Rectangle.cpp:
#include "Rectangle.h"
Rectangle::Rectangle(int& length, int& height)
: length(length), height(height)
{ }
Rectangle::~Rectangle() {}
Add the rectangle to vector first, dereference iterator and access the elements.
int main() {
int len = 2, hen = 5;
int len2 = 4, hen2 = 10;
Rectangle rect1(len, hen);
Rectangle rect2(len2, hen2);
vector<Rectangle> Rects;
Rects.push_back(rect1);
Rects.push_back(rect2);
for (std::vector<Rectangle>::iterator it = Rects.begin(); it != Rects.end(); ++it) {
std::cout << "length " <<(*it).length<<std::endl;
std::cout << "height " <<(*it).height<<std::endl;
}
system("pause");
return 0;
}

Pointing an array in c++?

I want to point an array in c++ , is it possible ?
My main code :
#include "ArrayPointerClass.h"
#include "stdafx.h"
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
float arr[2];
ArrayPointerClass::pointingArray(&arr);
return 0;
}
ArrayPointerClass.h
#pragma once
static class ArrayPointerClass
{
public:
ArrayPointerClass();
~ArrayPointerClass();
static void pointingArray(float* arr[2]);
};
ArrayPointerClass.cpp
#include "stdafx.h"
#include "ArrayPointerClass.h"
ArrayPointerClass::ArrayPointerClass()
{
}
ArrayPointerClass::~ArrayPointerClass()
{
}
void ArrayPointerClass::pointingArray(float* arr[2]){
float newArray[2] = { 2.2f, 2.2f };
*arr = newArray;
}
I've got this error :
Error 3 error C2653: 'ArrayPointerClass' : is not a class or namespace name c:\users\alex\documents\visual studio 2013\projects\pointerarray\pointerarray\pointerarray.cpp 13 1 PointerArray
Error 3 error C3861: 'pointingArray': identifier not found c:\users\alex\documents\visual studio 2013\projects\pointerarray\pointerarray\pointerarray.cpp 13 1 PointerArray
I know in C++ arrays ,arrays without length defined are not allowed . is it the reason ?
Thanks for your support
There is no way to create a static class in c++. static keyword can be applied to objects and functions.
And each array name is a pointer. Therefore subscripts cannot be given in the parameter. It is sufficient to provide the pointer type.
The modified code which works:
#include <iostream>
#include <string>
using namespace std;
class ArrayPointerClass
{
public:
ArrayPointerClass();
~ArrayPointerClass();
static void pointingArray(float* arr);
};
ArrayPointerClass::ArrayPointerClass()
{
}
ArrayPointerClass::~ArrayPointerClass()
{
}
void ArrayPointerClass::pointingArray(float* arr){
float newArray[2] = { 2.2f, 2.2f };
arr = newArray;
}
int main()
{
float arr[2];
ArrayPointerClass obj;
obj.pointingArray(arr);
return 0;
}
*arr = newArray; will not work you can't copy a C array like that .
You could have done memcpy() or std::copy() like;
memcpy( newArray, arr, 2);
std::copy( newArray, newArray+2, arr);
Thanks everybody for your answers (even there were partially working)
I found my self a solution
// PointerArray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int* test() {
int size_needed = 2;
int* a = new int[size_needed];
a[0] = 0;
a[1] = 0;
return a;
}
int main()
{
//int arr[2] = { 1, 1 };
int* arr = test();
for (int i = 0; i < 2; i++){
cout << arr[i] << std::endl;
}
cin.get();
return 0;
}

C++ biginteger class operator=

I am trying to implement a biginteger class, and after I created a biginteger class, with a proper header file, and at first I am trying to define a operator=() operator, so when I make a new biginteger object, I will be able to make it equals with a integer.
This is the main.cpp:
#include <iostream>
#include "bigint.h"
using namespace std;
int main()
{
bigint bela = 15;
cout << "Hello world!" << bela.mennyi() <<endl;
return 0;
}
And this is the biginteger header:
#ifndef BIGINT_H
#define BIGINT_H
#include <vector>
#include <iostream>
class bigint
{
public:
bigint();
void operator=(const int &a);
int mennyi();
protected:
private:
std::vector<int> numarray;
};
#endif // BIGINT_H
And the biginteger.cpp file:
#include "bigint.h"
#include <iostream>
using namespace std;
bigint::bigint()
{
numarray.resize(0);
}
void bigint::operator=(const int &a)
{
int b = a;
if(b >= 0)
{
numarray.resize(0);
while(b!=0){
numarray.push_back(b%10);
b = b/10;
}
}
}
int bigint::mennyi()
{
int ki = 0;
for(int i = (numarray.size())-1; i>=0; i--)
{
ki = ki*10 + numarray[i];
}
return ki;
}
When I start the debugging I get an error saying: conversion from 'int' to non-scalar type 'bigint' requested.
You should implement this constructor:
bigint::bigint(int);