overloading assignment operator and header file in c++ - 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();
};

Related

StringBuilder Class in C++ not working properly

I'm working on an assignment to create a class called StringBuilder that is used for fast string concatenation. I'm supposed to store strings in a dynamic array and have methods such as Append(string) which adds a new string to the dynamic array. The method I'm currently struggling with is GetString() that creates a single string on the heap that is the length of all the strings in the dynamic array that have been added thus far.
the code I have so far is:
okay my main problem is my GetString() function prints out hello over and over again until I force quit the program in Xcode. I don't understand what inside that method is making that happen.
My header file:
#pragma once
#include <string>
using namespace std;
class StringBuilder
{
public:
StringBuilder();
//~StringBuilder();
void GetString();
void AppendAll(string*, int);
void Length();
void Clear();
void Append(string userString);
void DoubleArray(string*& allWords, int newCapacity);
private:
string* p_array;
int capacity = 5;
};
my .cpp file :
#include "StringBuilder.hpp"
#include <iostream>
#include <string>
using namespace std;
----------
void StringBuilder::Append(string userString)
{
int nextWordPosition = 0;
for(int i=0; i < capacity ; i++)
{
p_array[i] = userString;
cout << p_array[i] << endl;
nextWordPosition +=1;
if(capacity == nextWordPosition)
{
capacity *=2;
DoubleArray(p_array, capacity * 2);
}
}
nextWordPosition++;
}
void StringBuilder::DoubleArray(string*& allWords, int newCapacity)
{
string* p_temp = new string[newCapacity];
for(int i =0; i < newCapacity / 2; i++)
{
p_temp[i] = allWords[i];
}
delete[] allWords;
allWords = p_temp;
}
void StringBuilder:: GetString()
{
for(int i=0; i < capacity ; i++)
{
cout << p_array[i]<< endl;
}
}
my main.cpp file :
#include <iostream>
#include <string>
#include "StringBuilder.hpp"
using namespace std;
int main()
{
string testString = "hello";
string test = "world!";
StringBuilder Builder1;
Builder1.Append(testString);
Builder1.Append(test);
Builder1.GetString();
return 0;
}

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

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";
}
}
};

problem passing array of struct to a function throwing undefined reference c++

I'm having issues with passing an array of structures to a function that searches them. I delcare an array of structs outside of main then copy it to a new array of structs inside of main (so I have access to them inside main and can pass them easier). Not sure why it is failing though. Can anyone help me?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 2000000;
const string DFile = "DFile.dms";
const string EFile = "EFile.dms";
const string VFile = "VFile.dms";
struct dogs
{
int did;
int age;
} DFBuffer[MAX];
struct examine
{
int vid;
int did;
int fee;
} EFBuffer[MAX];
struct vet
{
int vid;
int eLevel;
} VFBuffer[MAX];
void readDF(ifstream&);
void readEF(ifstream&);
void readVF(ifstream&);
int getLineCount(ifstream&);
bool dogCompare(dogs lhs, dogs rhs) {return lhs.did < rhs.did;}
bool vetCompare(vet lhs, vet rhs) {return lhs.vid < rhs.vid;}
bool examCompare(examine lhs, examine rhs) {return lhs.vid < rhs.vid;}
void vetExamSeach(struct vet newVetArray[], struct examine newExamArray[],
int, int);
int main()
{
dogs * newDogArray = new dogs[MAX];
examine * newExamArray = new examine[MAX];
vet * newVetArray = new vet[MAX];
ifstream DF, EF, VF;
int dogCount = 0, examCount = 0, vetCount = 0;
DF.open(DFile);
readDF(DF);
EF.open(EFile);
readEF(EF);
VF.open(VFile);
readVF(VF);
DF.open(DFile);
dogCount = getLineCount(DF);
EF.open(EFile);
examCount = getLineCount(EF);
VF.open(VFile);
vetCount = getLineCount(VF);
for(int i = 0; i < dogCount; i++)
newDogArray[i] = DFBuffer[i];
for(int i = 0; i < vetCount; i++)
newVetArray[i] = VFBuffer[i];
for(int i = 0; i < examCount; i++)
newExamArray[i] = EFBuffer[i];
cout << "Sorting...\n";
sort(newDogArray, newDogArray + dogCount, dogCompare);
sort(newExamArray, newExamArray + examCount, examCompare);
sort(newVetArray, newVetArray + vetCount, vetCompare);
cout << "Sorting complete!\n";
vetExamSeach(newVetArray, newExamArray, vetCount, examCount);
return 0;
}
here is the search function. for the sake of this question, im just trying to print what i pass it.
void search(vet newVetArray[], examine newExamArray[], int vCount, int eCount)
{
for(int i = 1; i < vCount; i++)
cout << "in search: " << newVetArray[i].vid << ' ' << newVetArray[i].eLevel << endl;
}
here is the error I'm getting
Here is my files. Not asking you to do my HW just help me solve my issue
When, I run your code, I get the same compilation error of undefined reference for readDf, readEF, readVF, getLineCount and vetExamSeach.
The error is because there is no definition of these functions. There are only just decalarations. When I define them (something random) the errors are gone.
So, define the function(s) and the error(s) would be gone.

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);

c++ 2d array maze navigation

I'm new to c++ and wondering if I'm doing this the best way. I need to read in a line from a text file and build an array from it and then navigate it. Let me know if I'm doing something wrong. Can I access matrix the way I am?
header:
#ifndef MAZE_HPP_
#define MAZE_HPP_
#include <fstream>
#include <vector>
#include <string>
class Maze
{
public:
Maze(int size);
~Maze() {}
enum Direction { DOWN, RIGHT, UP, LEFT };
// Implement the following functions:
// read maze from file, find starting location
void readFromFile(std::ifstream &f);
// make a single step advancing toward the exit
void step();
// return true if the maze exit has been reached, false otherwise
bool atExit();
// set row and col to current position of 'x'
void getCurrentPosition(int &row, int &col);
// You can add more functions if you like
private:
// Private data and methods
int size;
static string matrix[30][30];
};
#endif /* MAZE_HPP_ */
c++ file:
#include <iostream>
#include "maze.hpp"
#include "utils.hpp"
#include <vector>
#include <string>
Maze::Maze(int size) {
size = size;
}
Maze::void readFromeFile(std::ifstream &f) {
std::string line;
int i, j;
while(std::getline(f, line)) {
for(i = 0; i < line.length(); i++) {
for(j = 0; j < line.length(); j++) {
matrix[i][j] = line.at(j);
}
}
}
}
Maze::void step() {
}
Maze::bool atExit() {
}
Maze::void getCurrentPosition(int &row, int &col) {
}
Maze::void readFromeFile {}
Maze::void step() {}
Maze::bool atExit() {}
Maze::void getCurrentPosition(int &row, int &col) {}
these should be
void Maze::readFromeFile{}
void Maze::step() {}
bool Maze::atExit(){}
void Maze::getCurrentPosition(int &row, int &col){}