I'm trying to set up operator that puts two arrays together but takes out dublicates in the array.
My idea is that I copy the first array in to the new one and then copy the "products" that are not in the first array in to the new array.
I'm trying to call a function that checks if the "product" is in the first array but I keep getting the error:
error: member function 'er_varan_eins' not viable: 'this' argument has type 'const Inventory', but function is not marked const
Here is my code:
#include <iostream>
using namespace std;
struct Varan{
string vara;
int fjoldi;
double verd;
};
class Inventory {
public:
Inventory();
void stafrofsrod();
void verdmaetisrod();
void setsortorder(string rodun);
void setsorting();
bool er_varan_eins(const Varan vara1);
~Inventory();
friend istream& operator >> (istream& ins, Inventory &inv);
friend ostream& operator << (ostream& outs, const Inventory &inv);
friend Inventory operator + (const Inventory &inv1, const Inventory &inv2);
private:
Varan *vorulisti;
int n;
string order;
};
Inventory::Inventory() {
n = 0;
vorulisti = new Varan[n];
}
void Inventory::stafrofsrod() {
Varan tmp;
int i, j;
for (i = 1; i < n; i++) {
tmp.vara = vorulisti[i].vara;
tmp.fjoldi = vorulisti[i].fjoldi;
tmp.verd = vorulisti[i].verd;
j = i;
while (j > 0 && vorulisti[j - 1].vara > tmp.vara) {
vorulisti[j].vara = vorulisti[j - 1].vara;
vorulisti[j].fjoldi = vorulisti[j - 1].fjoldi;
vorulisti[j].verd = vorulisti[j - 1].verd;
j--;
}
vorulisti[j].vara = tmp.vara;
vorulisti[j].fjoldi = tmp.fjoldi;
vorulisti[j].verd = tmp.verd;
}
}
void Inventory::verdmaetisrod() {
Varan tmp;
int i, j;
for (i = 1; i < n; i++) {
tmp.vara = vorulisti[i].vara;
tmp.fjoldi = vorulisti[i].fjoldi;
tmp.verd = vorulisti[i].verd;
j = i;
while (j > 0 && (vorulisti[j - 1].fjoldi * vorulisti[j - 1].verd) < (tmp.fjoldi * tmp.verd)) {
vorulisti[j].vara = vorulisti[j - 1].vara;
vorulisti[j].fjoldi = vorulisti[j - 1].fjoldi;
vorulisti[j].verd = vorulisti[j - 1].verd;
j--;
}
vorulisti[j].vara = tmp.vara;
vorulisti[j].fjoldi = tmp.fjoldi;
vorulisti[j].verd = tmp.verd;
}
}
void Inventory::setsortorder(string rodun) {
order = rodun;
setsorting();
}
void Inventory::setsorting() {
if (order == "Value") {
verdmaetisrod();
}
else {
stafrofsrod();
}
}
Inventory::~Inventory() {
delete [] vorulisti;
}
istream& operator >> (istream& ins, Inventory &inv) {
ins >> inv.n;
inv.vorulisti = new Varan[inv.n];
for (int i = 0; i < inv.n; i++) {
ins >> inv.vorulisti[i].vara;
ins >> inv.vorulisti[i].fjoldi;
ins >> inv.vorulisti[i].verd;
}
inv.stafrofsrod();
return ins;
}
ostream& operator << (ostream& outs, const Inventory &inv) {
for (int i = 0; i < inv.n; i++) {
outs << inv.vorulisti[i].vara << " " << inv.vorulisti[i].fjoldi << " " << inv.vorulisti[i].verd << endl;
}
return outs;
}
Inventory operator + (const Inventory &inv1, const Inventory &inv2) {
int counter = 0;
for (int i = 0; i < inv1.n; i++) {
for (int j = 0; j < inv2.n; j++) {
if (inv1.vorulisti[i].vara == inv2.vorulisti[i].vara) {
counter++;
}
}
}
int new_size = inv1.n + inv2.n - counter;
Inventory new_array;
new_array.n = new_size;
new_array.vorulisti = new Varan[new_size];
for (int i = 0; i < inv1.n; i++) {
new_array.vorulisti[i].vara = inv1.vorulisti[i].vara;
new_array.vorulisti[i].fjoldi = inv1.vorulisti[i].fjoldi;
new_array.vorulisti[i].verd = inv1.vorulisti[i].verd;
}
int teljari = 0;
for (int j = 0; j < inv2.n; j++) {
if( inv1.er_varan_eins(inv2.vorulisti[j].vara)) {
teljari++;
}
else {
new_array.vorulisti[j + inv1.n - teljari].vara = inv2.vorulisti[j].vara;
new_array.vorulisti[j + inv1.n - teljari].fjoldi = inv2.vorulisti[j].fjoldi;
new_array.vorulisti[j + inv1.n - teljari].verd = inv2.vorulisti[j].verd;
}
}
new_array.stafrofsrod();
return new_array;
}
bool Inventory::er_varan_eins(const Varan vara1) {
for (int i = 0; i < n; i++) {
if (vorulisti[i].vara == vara1.vara) {
return true;
}
}
return false;
}
int main()
{
Inventory inv1, inv2;
cin >> inv1 >> inv2;
cout << inv1 + inv2;
return 0;
}
Since inv1 is a const Inventory &, you can only call const member functions on it. Your er_varan_eins function can be made const since it does not modify any members of the Inventory class.
Related
#include<iostream>
using namespace std;
class Text{
public:
~Text(){
delete data;
}
char* data{};
int mSize{};
void fill(char* stringInput) {
mSize = strlen(stringInput);
data = new char [mSize];
for (int i = 0; i < mSize; i++){
data[i] = stringInput[i];
}
}
};
class myString{
public:
explicit myString(int size){ // constructor
strAmount = size;
strings = new Text [size];
}
~myString(){ // destructor
delete[] strings;
}
void addString(char* input){
strings[filledAmount].fill(input);
filledAmount++;
}
void delString(int pos){
for ( int i = pos; i < filledAmount; i++){
swap(strings[i], strings[i+1]);
}
strings[filledAmount].data = nullptr;
strings[filledAmount].mSize = 0;
filledAmount--;
}
void eraseEverything(){
for ( int i = 0; i < filledAmount; i++){
strings[i].data = {};
strings[i].mSize = 0;
}
filledAmount = 0;
}
int maxString() const {
int index{};
for ( int i = 0 ; i < filledAmount; i++){
if (strings[i].mSize > strings[index].mSize){
index = i;
}
}
return index;
}
int charAmount(){
int counter{};
for(int i = 0 ; i < filledAmount; i++){
counter+=strings[i].mSize;
}
return counter;
}
double digitPercentage(){
int digitsAmount{};
for(int i = 0; i < filledAmount; i++){
for ( int j = 0; j < strings[i].mSize; j++){
if (isdigit(strings[i].data[j])){
digitsAmount++;
}
}
}
double digitPercent = (digitsAmount/(double)charAmount())*100;
return digitPercent;
}
int filledAmount{};
int strAmount{};
Text* strings;
};
void render_text(myString& obj) {
for (int k = 0; k < obj.filledAmount; k++) {
for (int i = 0; i < obj.strings[k].mSize; i++)
cout << obj.strings[k].data[i];
cout << endl;
}
cout << endl;
}
int main(){
myString a(5);
a.addString((char *) "zxc 1v1 forever shadow fiend");
a.addString((char *) "This is a string");
a.addString((char *) "12345");
a.addString((char *) "Hello");
a.addString((char *) "A1oha Dance");
render_text(a);
a.delString(1);
render_text(a);
int maxInd = a.maxString();
cout << "Max string :\n";
for (int i = 0; i < a.strings[maxInd].mSize; i++) {
cout << a.strings[maxInd].data[i];
}
cout << "\n\n";
}
Please help me find the crash point. I suppose it crashes in the destructor pole, but I still can't figure it out.
This is something like a self-written string class, the problem is that I can't find the place where the problems start.
I also have a thought that the destructor tries to delete too much memory from the heap so the compiler prevents it from doing that. Can I somehow change the size of the strings array?
I have fixed all your memory errors, with the help of address sanitizers
The main change here is:
Add copy constructors and copy assignment operators
Manually delete the last element in the function delString
Though that the code now works, it's not a true modern c++ style code. I highly recommend that your using std::string to replace your Text class. Use std::vector to replace the dynamic array. Then you will stay away from the pain of memory errors. The delString should be replaced with vector::erase,which is much neat than your hand write algorithm.
https://en.cppreference.com/w/cpp/string/basic_string
https://en.cppreference.com/w/cpp/container/vector
I strongly recommend rewriting the code with std::string and std::vector
#include <cstring>
#include <iostream>
using namespace std;
class Text {
public:
~Text() { delete[] data; }
char* data{};
int mSize{};
Text() = default;
Text(const Text& oth) {
mSize = oth.mSize;
data = new char[mSize];
std::copy(oth.data, oth.data + oth.mSize, data);
}
Text& operator=(const Text& oth) {
delete[] data;
mSize = oth.mSize;
data = new char[mSize];
std::copy(oth.data, oth.data + oth.mSize, data);
return *this;
}
void fill(char* stringInput) {
mSize = strlen(stringInput) + 1;
data = new char[mSize];
for (int i = 0; i < mSize; i++) {
data[i] = stringInput[i];
}
}
};
class myString {
public:
explicit myString(int size) { // constructor
strAmount = size;
strings = new Text[size];
}
myString(const myString& oth) {
strAmount = oth.strAmount;
strings = new Text[oth.strAmount];
for (size_t i = 0; i < strAmount; ++i) {
strings[i] = oth.strings[i];
}
}
myString& operator=(const myString& oth) {
delete[] strings;
strAmount = oth.strAmount;
strings = new Text[oth.strAmount];
for (size_t i = 0; i < strAmount; ++i) {
strings[i] = oth.strings[i];
}
return *this;
}
~myString() { // destructor
delete[] strings;
}
void addString(char* input) {
strings[filledAmount].fill(input);
filledAmount++;
}
void delString(int pos) {
for (int i = pos; i < filledAmount; i++) {
swap(strings[i], strings[i + 1]);
}
delete[] strings[filledAmount].data;
strings[filledAmount].data = nullptr;
strings[filledAmount].mSize = 0;
filledAmount--;
}
void eraseEverything() {
for (int i = 0; i < filledAmount; i++) {
strings[i].data = {};
strings[i].mSize = 0;
}
filledAmount = 0;
}
int maxString() const {
int index{};
for (int i = 0; i < filledAmount; i++) {
if (strings[i].mSize > strings[index].mSize) {
index = i;
}
}
return index;
}
int charAmount() {
int counter{};
for (int i = 0; i < filledAmount; i++) {
counter += strings[i].mSize;
}
return counter;
}
double digitPercentage() {
int digitsAmount{};
for (int i = 0; i < filledAmount; i++) {
for (int j = 0; j < strings[i].mSize; j++) {
if (isdigit(strings[i].data[j])) {
digitsAmount++;
}
}
}
double digitPercent = (digitsAmount / (double)charAmount()) * 100;
return digitPercent;
}
int filledAmount{};
int strAmount{};
Text* strings = nullptr;
};
void render_text(myString& obj) {
for (int k = 0; k < obj.filledAmount; k++) {
for (int i = 0; i < obj.strings[k].mSize; i++)
cout << obj.strings[k].data[i];
cout << endl;
}
cout << endl;
}
int main() {
myString a(6);
a.addString((char*)"zxc 1v1 forever shadow fiend");
a.addString((char*)"This is a string");
a.addString((char*)"12345");
a.addString((char*)"Hello");
a.addString((char*)"A1oha Dance");
render_text(a);
a.delString(1);
render_text(a);
int maxInd = a.maxString();
cout << "Max string :\n";
for (int i = 0; i < a.strings[maxInd].mSize; i++) {
cout << a.strings[maxInd].data[i];
}
cout << "\n\n";
return 0;
}
This is a project for my object oriented programming class. I need to introduce the followings: overloading + to sum two polynomials, overloading * to multiply a polynomial with a scalar or to multiply it with another polynomial, overloading [] to return coeficient on a specific position, method for adding , deleting a coeficient and evaluating polynom in a certain point (f(x)).
1.Everything is working besides the destructor. The destructor that should be used (delete [] coef) of Polynomial will break (Heap Coruption) if i use cout << A + B and i do not know why.
What i use right now is a workaround but it will not delete coef[0]. That's the point where it breaks. If you can help me somehow i will be gratefull. Can you understand what caused it?
2.I need to have one more overloaded operator, " / ". It should make division between two polynomials and I do not know how to implement it. I tried to search for it but i couldn't understand any algorithm. Any advice or algorithm explanation would be great, because i really do not know from where to start and i need to finish it until tommorrow morning.
3.If you have any advice on coding or efficienty will also be great ( project requirement: do not use STL ).
Thank you!
Polynomial.h:
#pragma once
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::istream;
class Polynomial
{
unsigned int deg;
double *coef;
public:
Polynomial() : deg(1), coef(new double[1]) {}
Polynomial(double [], int);
Polynomial(Polynomial&);
Polynomial& operator = (const Polynomial&);
~Polynomial();
friend ostream &operator <<(ostream&, const Polynomial&);
friend istream &operator >>(istream&, Polynomial&);
Polynomial operator + (const Polynomial) ;
double operator[] (unsigned int) const;
Polynomial operator * (int) const;
Polynomial operator * (const Polynomial obj) const;
unsigned int getDeg() { return this->deg; };
double eval(int);
void addCoef(double, int);
void delCoef(int);
};
inline Polynomial::Polynomial(double coefficients[], int number) : deg(number), coef(new double[number])
{
for (int i = 0; i < deg; i++) {
coef[i] = coefficients[i];
}
}
inline Polynomial::Polynomial(Polynomial &ob) {
this->deg = ob.deg;
this->coef = new double[deg];
for (int i = 0; i <= deg; i++)
{
this->coef[i] = ob.coef[i];
}
}
inline Polynomial::~Polynomial() {
for (int i = this->deg; i > 0; i--)
this->delCoef(i);
this->coef[0] = 0; //If i write it like this delete [] coef; it breaks.
this->deg = 0; //with HeapCoruption detected
}
Polynomial.cpp:
Polynomial& Polynomial::operator = (const Polynomial& obj)
{
if (this != &obj) //Testing to avoid situations like A = A;
{
this->deg = obj.deg;
for (int i = 0; i <= obj.deg; i++)
this->coef[i] = obj.coef[i];
}
return *this;
}
istream& operator >> (istream& in, Polynomial& obj)
{
in >> obj.deg;
cout << endl;
obj.coef = new double[obj.deg];
for (int i = 0; i <= obj.deg; i++)
{
in >> obj.coef[i];
}
return in;
}
ostream& operator << (ostream& out, const Polynomial& obj)
{
out << obj.deg << endl;
for (int i = 0; i <= obj.deg; i++)
{
if (obj.coef[i] != 0)
{
if (obj.coef[i] < 0)
out << '(' << obj.coef[i] << ')';
else
out << obj.coef[i];
if (i > 1)
out << "*x^" << i;
if (i == 1)
out << "*x";
if (i != obj.deg)
out << " + ";
}
}
out << endl <<endl;
return out;
}
Polynomial Polynomial::operator+ (const Polynomial obj)
{
Polynomial aux;
if (obj.deg >= deg)
{
aux.deg = obj.deg;
aux.coef = new double[obj.deg];
for (int i = 0; i <= deg; i++)
aux.coef[i] = obj.coef[i] + coef[i];
for (int i = deg + 1; i <= obj.deg; i++)
aux.coef[i] = obj.coef[i];
}
else // obj.deg < this->deg
{
aux.deg = deg;
aux.coef = new double[deg];
for (int i = 0; i <= obj.deg; i++)
{
aux.coef[i] = obj.coef[i] + coef[i];
}
for (int i = obj.deg + 1; i <= deg; i++)
{
aux.coef[i] = coef[i];
}
}
return aux;
}
double Polynomial::operator[] (unsigned int pos) const
{
if (pos > this->deg) {
throw std::out_of_range("Index bigger than polynomial length");
}
return this->coef[pos];
}
Polynomial Polynomial::operator * (const int scalar) const
{
Polynomial aux;
aux.deg = this->deg;
aux.coef = new double[aux.deg];
for (int i = 0; i <= aux.deg; i++)
aux.coef[i] = this->coef[i] * scalar;
return aux;
}
Polynomial Polynomial::operator * (const Polynomial obj) const
{
Polynomial aux;
aux.deg = obj.deg + this->deg;
aux.coef = new double[aux.getDeg()];
for (int i = 0; i <= aux.getDeg(); i++)
aux.addCoef(0, i);
for (int i = 0; i <= this->deg; i++)
for (int j = 0; j <= obj.deg; j++)
aux.coef[i+j] += (this->coef[i]) * obj.coef[j];
return aux;
}
double Polynomial::eval(int x) {
double sum = 0;
for (int i = 0; i <= this->deg; i++)
{
if (i == 0)
sum += this->coef[0];
else
{
int aux = i;
int xaux = x;
aux--;
while (aux != 0)
{
xaux *= x;
aux--;
}
sum += this->coef[i] * xaux;
}
}
return sum;
}
void Polynomial::addCoef(double NewCoef, int pos)
{
if (pos < 0)
return;
if (pos > this->deg)
{
double *newCoef = new double[pos];
for (int i = 0; i <= this->deg; i++)
newCoef[i] = this->coef[i];
for (int i = this->deg + 1; i < pos; i++)
newCoef[i] = 0;
newCoef[pos] = NewCoef;
this->coef = newCoef;
this->deg = pos;
return;
}
else
{
this->coef[pos] = NewCoef;
}
}
void Polynomial::delCoef(int pos)
{
if (pos > this->deg || pos < 0 )
return;
if (pos == this->deg)
{
this->coef[pos] = 0;
int degNoua = pos - 1;
while (this->coef[pos] == 0 && pos != 0)
pos--;
if (pos == 0 && this->coef[pos] == 0)
{
delete this->coef;
this->deg = 0;
}
else
{
this->deg = pos;
double *aux = new double[pos];
for (int i = 0; i <= pos; i++)
aux[i] = this->coef[i];
this->coef = aux;
}
}
else
{
this->coef[pos] = 0;
}
}
This question already has answers here:
calling destructor explicitly
(2 answers)
Closed 3 years ago.
So I have a class Vector and when I'm calling in main for example the destructor of an object I get an exception. The line of code from the exception file is "free_dbg(block, _UNKNOWN_BLOCK)"; Note that if I do only the reading of the vectors or only the destructor call, I won't get the exception. When I do both together, this is when the problem appears.
Here is the code:
#include "pch.h"
#include <iostream>
#include <fstream>
using namespace std;
class Vector
{
public:
Vector();
~Vector();
Vector(const Vector& vec);
void insert_element(int value, int position);
void delete_element(int position);
void set_nr_elem(int nr) { nr_elem = nr; }
int get_nr_elem() const { return nr_elem; }
int get_element(int position) const { return arr[position]; }
friend istream& operator>>(istream& input, Vector &vec);
friend ostream& operator<<(ostream& output, const Vector &vec);
Vector operator=(const Vector& vec);
void free_memory() { delete[] arr; }
private:
int *arr;
int nr_elem;
};
Vector::Vector()
{
arr = NULL;
nr_elem = 0;
}
Vector::~Vector()
{
delete[]arr;
nr_elem = 0;
}
Vector::Vector(const Vector& vec)
{
arr = new int[vec.nr_elem];
nr_elem = vec.nr_elem;
for (int i = 0; i < nr_elem; i++)
arr[i] = vec.arr[i];
}
void Vector::insert_element(int value, int position)
{
if (position < 0 || position > nr_elem)
{
cout << "Invalid position";
return;
}
arr = (int*)realloc(arr, (nr_elem + 1) * sizeof(int));
for (int i = nr_elem; i >= position + 1; i--)
arr[i] = arr[i - 1];
arr[position] = value;
nr_elem++;
}
void Vector::delete_element(int position)
{
if (position < 0 || position >= nr_elem)
{
cout << "Invalid position";
return;
}
for (int i = position; i < nr_elem; i++)
arr[i] = arr[i + 1];
delete &arr[nr_elem];
nr_elem--;
}
istream& operator>>(istream& input, Vector &vec)
{
int nr;
input >> nr;
for (int i = 0; i < nr; i++)
{
int x;
input >> x;
vec.insert_element(x, vec.get_nr_elem());
}
return input;
}
ostream& operator<<(ostream& output, const Vector &vec)
{
if (vec.get_nr_elem())
{
output << "Number of elements: " << vec.get_nr_elem() << "\n";
output << "The elements: ";
for (int i = 0; i < vec.get_nr_elem(); i++)
output << vec.get_element(i) << " ";
output << "\n";
}
else
{
output << "The vector has no elements";
}
return output;
}
Vector Vector::operator=(const Vector& vec)
{
if (this == &vec)
return *this;
delete[] arr;
arr = new int[vec.nr_elem];
for (int i = 0; i < vec.nr_elem; i++)
arr[i] = vec.arr[i];
nr_elem = vec.nr_elem;
return *this;
}
class Natural_Big_Number
{
public:
void set_sign(char c) { sign = c; }
char get_sign() const { return sign; }
friend istream& operator>>(istream& input, Natural_Big_Number &number);
friend ostream& operator<<(ostream& output, const Natural_Big_Number &number);
friend Natural_Big_Number operator+(const Natural_Big_Number& number1, const Natural_Big_Number& number2);
friend Natural_Big_Number operator-(const Natural_Big_Number& number1, const Natural_Big_Number& number2);
friend Natural_Big_Number& operator*(const Natural_Big_Number& number1, const Natural_Big_Number& number2);
friend Natural_Big_Number& operator/(const Natural_Big_Number& number1, const Natural_Big_Number& number2);
Natural_Big_Number operator=(const Natural_Big_Number& number);
private:
Vector vec;
char sign;
};
istream& operator>>(istream& input, Natural_Big_Number &number)
{
int x;
input >> x;
char c;
input.get();
c = input.get();
if (c == '-')
number.set_sign('-');
else
{
number.set_sign('+');
int digit = (int)c - 48;
number.vec.insert_element(digit, number.vec.get_nr_elem());
}
while ((c = input.get()) >= '0' && c <= '9')
{
int digit = (int)c - 48;
number.vec.insert_element(digit, number.vec.get_nr_elem());
}
return input;
}
ostream& operator<<(ostream& output, const Natural_Big_Number &number)
{
cout << "Number of digits: " << number.vec.get_nr_elem() << '\n';
cout << "The number: ";
if (number.get_sign() == '-')
cout << number.get_sign();
for (int i = 0; i < number.vec.get_nr_elem(); i++)
cout << number.vec.get_element(i);
cout << endl;
return output;
}
Natural_Big_Number operator+(const Natural_Big_Number& number1, const Natural_Big_Number& number2)
{
Natural_Big_Number result;
int carry = 0;
int it_digits1 = number1.vec.get_nr_elem() - 1;
int it_digits2 = number2.vec.get_nr_elem() - 1;
if (number1.get_sign() == number2.get_sign())
{
result.set_sign(number1.get_sign());
while (it_digits1 >= 0 && it_digits2 >= 0)
{
int aux = number1.vec.get_element(it_digits1) + number2.vec.get_element(it_digits2) + carry;
int value = aux % 10;
result.vec.insert_element(value, 0);
carry = aux / 10;
it_digits1--;
it_digits2--;
}
for (int i = 0; i <= it_digits1; i++)
{
int aux = number1.vec.get_element(it_digits1) + carry;
int value = aux % 10;
carry = aux / 10;
result.vec.insert_element(value, 0);
}
for (int i = 0; i <= it_digits2; i++)
{
int aux = number2.vec.get_element(it_digits2) + carry;
int value = aux % 10;
carry = aux / 10;
result.vec.insert_element(value, 0);
}
}
/*else if (number1.get_sign() == '-' && number2.get_sign() == '+')
{
result = number1;
result.set_sign('+');
result = number2 + result;
}*/
cout << result;
return result;
}
Natural_Big_Number operator-(const Natural_Big_Number& number1, const Natural_Big_Number& number2)
{
Natural_Big_Number result;
int carry = 0;
int it_digits1 = number1.vec.get_nr_elem() - 1;
int it_digits2 = number2.vec.get_nr_elem() - 1;
while (it_digits1 >= 0 && it_digits2 >= 0)
{
int aux = number1.vec.get_element(it_digits1) - number2.vec.get_element(it_digits2) + carry;
int value = aux % 10;
result.vec.insert_element(value, 0);
carry = aux / 10;
it_digits1--;
it_digits2--;
}
for (int i = 0; i <= it_digits1; i++)
{
int aux = number1.vec.get_element(it_digits1) + carry;
int value = aux % 10;
carry = aux / 10;
result.vec.insert_element(value, 0);
}
for (int i = 0; i <= it_digits2; i++)
{
int aux = number2.vec.get_element(it_digits2) + carry;
int value = aux % 10;
carry = aux / 10;
result.vec.insert_element(value, 0);
}
cout << result;
return result;
}
Natural_Big_Number Natural_Big_Number::operator=(const Natural_Big_Number& number)
{
if (this == &number)
return *this;
vec.free_memory();
set_sign(number.get_sign());
vec = number.vec;
return *this;
}
int main()
{
ifstream f("date.in");
Natural_Big_Number number1, number2;
f >> number1 >> number2;
number1 = number2;
cout << number1;
return 0;
}
That's because you shouldn't do that.
The destructor is called for you when vec1 goes out of scope.
Your program attempts to destroy vec1 twice, which causes mayhem.
class Vector {
protected:
unsigned int dim;
float *a;
public:
Vector() {
a = NULL;
dim = 0;
}
Vector(const unsigned int& x) {
dim = x;
a = new float[dim];
for (unsigned int index = 0; index < x; index++) {
cin >> a[index];
}
}
Vector(const float *init, const unsigned int& size) {
if (init) {
dim = size;
a = new float[size];
for (unsigned int index = 0; index < size; index++) {
a[index] = init[index];
}
}
}
void initVector(const float *init, const unsigned int& size) {
if (init) {
dim = size;
a = new float[size];
for (unsigned int index = 0; index < size; index++) {
a[index] = init[index];
}
}
}
void initVector(const unsigned int& size) {
dim = size;
a = new float[dim];
for (unsigned int index = 0; index < size; index++) {
cin >> a[index];
}
}
unsigned int getDim() {
return dim;
}
float operator[](unsigned int pos) {
return a[pos];
}
~Vector() {
delete[] a;
}
friend class Matrice;
friend istream& operator>>(istream&, Vector&);
};
class Matrice {
protected:
Vector *v;
public:
Matrice() {
v = NULL;
}
Vector operator[](int pos) {
return v[pos];
}
friend istream& operator>>(istream&, Matrice&);
};
class Matrice_oarecare : public Matrice
{
protected:
unsigned int linii;
public:
Matrice_oarecare() {
linii = 0;
}
Matrice_oarecare(int coloane, int linii) {
this->linii = linii;
v = new Vector[linii];
for ( int index = 0; index < linii; index++) {
cin >> v[index]; //Asta apeleaza >> pentru Vector, care citeste toate numerele din sir
}
}
friend istream& operator>>(istream& in, Matrice_oarecare&a);
friend ostream& operator<<(ostream& out, Matrice_oarecare&a);
};
class Matrice_patratica : public Matrice {
unsigned int dim;
public:
Matrice_patratica() {
v = NULL;
dim = 0;
}
Matrice_patratica(unsigned int dim) {
this->dim = dim;
v = new Vector[dim];
for (unsigned int index = 0; index < dim; index++) {
float *init = new float[dim];
for (unsigned index2 = 0; index2 < dim; index2++)
cin >> init[index];
v[index].initVector(init, dim);
}
}
friend ostream& operator<<(ostream&, Matrice_patratica&);
};
istream& operator>>(istream& in, Vector& v) {
in >> v.dim;
v.a = new float[v.dim];
for (unsigned int i = 0; i < v.dim; i++)
in >> v.a[i];
return in;
}
ostream& operator<<(ostream& os, Matrice_oarecare& a) {
for (unsigned indexI = 0; indexI < a.v->getDim(); indexI++) {
for (unsigned indexJ = 0; indexJ < a.linii; indexJ++) {
os << a[indexI][indexJ];
}
}
return os;
}
ostream& operator<<(ostream& os, Matrice_patratica& a) {
for (unsigned indexI = 0; indexI < a.dim; indexI++) {
for (unsigned indexJ = 0; indexJ < a.dim; indexJ++) {
os << a[indexI][indexJ];
}
}
return os;
}
int main() {
Matrice_oarecare c(2,2);
cout << c << endl;
return 0;
}
I have some problems while trying to dynamic allocate a matrix. Whatever I write, it return something like '21.3244141e-48198' and idk why. I think that the problem might be either the overloaded << operator, or one inheritance.
The 'Matrice_oarecare' and 'Matrice_patratica' have to inherit the Matrice class, and I have to create them using the Vector class.
I'm getting an error regarding "disgarded qualifiers" when I use this. The Entier class is posted below.
cout << d // where d is of type dynamic_array.
The global overloaded function:
template <class T> std::ostream& operator<<(std::ostream& stream, dynamic_array<T> const& data)
{
data.print_array(stream);
return stream;
}
A public member of dynamic_array
void print_array(std::ostream &os = cout)
{
for (int i = 0; i < size; i++) os << array[i] << endl;
}
Entire Class dynamic array:
/*
Needs a reszie function added
Merge sort is better for sequential, stable(equal elements not re-arranged, or
*/
#include "c_arclib.cpp"
using namespace std;
template <class T> class dynamic_array
{
private:
T* array;
T* scratch;
void merge_recurse(int left, int right)
{
if(right == left + 1)
{
return;
}
else
{
int i = 0;
int length = right - left;
int midpoint_distance = length/2;
int l = left, r = left + midpoint_distance;
merge_recurse(left, left + midpoint_distance);
merge_recurse(left + midpoint_distance, right);
for(i = 0; i < length; i++)
{
if((l < (left + midpoint_distance)) && (r == right || array[l] > array[r]))
{
scratch[i] = array[l];
l++;
}
else
{
scratch[i] = array[r];
r++;
}
}
for(i = left; i < right; i++)
{
array[i] = scratch[i - left];
}
}
}
void quick_recurse(int left, int right)
{
int l = left, r = right, tmp;
int pivot = array[(left + right) / 2];
while (l <= r)
{
while (array[l] < pivot)l++;
while (array[r] > pivot)r--;
if (l <= r)
{
tmp = array[l];
array[l] = array[r];
array[r] = tmp;
l++;
r--;
}
}
if (left < r)quick_recurse(left, r);
if (l < right)quick_recurse(l, right);
}
public:
int size;
dynamic_array(int sizein)
{
size=sizein;
array = new T[size]();
}
void print_array(std::ostream &os = cout)
{
for (int i = 0; i < size; i++) os << array[i] << endl;
}
void print_array()
{
for (int i = 0; i < size; i++) cout << array[i] << endl;
}
int merge_sort()
{
scratch = new T[size]();
if(scratch != NULL)
{
merge_recurse(0, size);
return 1;
}
else
{
return 0;
}
}
void quick_sort()
{
quick_recurse(0,size);
}
void rand_to_array()
{
srand(time(NULL));
int* k;
for (k = array; k != array + size; ++k)
{
*k=rand();
}
}
void order_to_array()
{
int* k;
int i = 0;
for (k = array; k != array + size; ++k)
{
*k=i;
++i;
}
}
void rorder_to_array()
{
int* k;
int i = size;
for (k = array; k != array + size; ++k)
{
*k=i;
--i;
}
}
};
template <class T> std::ostream& operator<<(std::ostream& stream, dynamic_array<T> const& data)
{
data.print_array(stream);
return stream;
}
int main()
{
dynamic_array<int> d1(1000000);
d1.order_to_array();
clock_t time_start=clock();
d1.merge_sort();
clock_t time_end=clock();
double result = (double)(time_end - time_start) / CLOCKS_PER_SEC;
cout << result;
cout << d1;
}
The problem is in following lines:
template <class T>
std::ostream& operator<<(std::ostream& stream, dynamic_array<T> const& data)
^^^^^^
and
void print_array(std::ostream &os = cout) /* const */
^^^^^ missing
Since you are passing data as const& to operator <<, you have to maintain its const qualification. i.e. data cannot call any non-const member of class dynamic_array. You can solve this problem in 2 ways:
pass data as simple dynamic_array<T>& to operator <<
make print_array a const method (uncomment above)
"Discards const qualifiers", presumably...
Replace:
void print_array(std::ostream &os = cout)
{
for (int i = 0; i < size; i++) os << array[i] << endl;
}
void print_array()
{
for (int i = 0; i < size; i++) cout << array[i] << endl;
}
...with...
void print_array(std::ostream &os = cout) const
{
for (int i = 0; i < size; i++) os << array[i] << endl;
}
...or, better still ...
void print_array(std::ostream &os = cout) const
{
std::copy(array, array + size, std::ostream_iterator<T>(os, "\n"));
}
This member function can be declared const since it doesn't modify any attributes of the class. See the FAQ on Const-Correctness.
Your second print_array() method is redundant since the first takes a default argument of std::cout.