Dynamic matrix in class - c++

I have problem with dynamic allocation in c++.
This is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
class Wektor {
int rozmiar;
float *TabWe;
friend std::istream& operator >> (std::istream &Strm, Wektor &Wek);
public:
Wektor(int rozmiar) : rozmiar(rozmiar) {
TabWe = new float[rozmiar];
}
Wektor() {
for (int i = 0; i < rozmiar; i++)
{
TabWe[i] = 0;
}
}
~Wektor()
{
for (int i = 0; i <rozmiar; i++)
{
delete[] TabWe;
}
}
};
istream& operator >>(istream &Strm, Wektor &Wek)
{
cout << "Size: ";
Strm >> Wek.rozmiar;
for (int i = 0; i < Wek.rozmiar; i++)
{
Strm >> Wek.TabWe[i];
}
return Strm;
}
int main()
{
Wektor wek;
cin >> wek;
}
After I enter first value to the matrix I get this error:
I think there is problem with default constructor, because you can see on the screenshot that this matrix has no value when program starts. What is wrong with it?

Related

I have a problem with reading & printing my class

I have a problem with printing my class. I want this class to read a binary number and then print it. I am a Beginner, so here can be a really dumb mistake.
This code has a wrong output, but a correct input.
I tried to fix this, but I couldn't. I hope you will find the mistake.
Please help. Thanks!
Here is the code:
#include <iostream>
using namespace std;
class Binary
{
int len;
bool* arr;
public:
Binary();
Binary(const Binary&);
friend istream& operator >> (istream&, Binary&);
friend ostream& operator << (ostream&, const Binary&);
};
Binary::Binary()
{
len = 0;
arr = new bool[0];
}
Binary::Binary(const Binary& b)
{
len = b.len;
arr = new bool[len];
for (int i = 0; i < len; i++) {
arr[i] = b.arr[i];
}
}
istream& operator>>(istream& in, Binary& b)
{
char line[101];
in.getline(line, 100);
b.len = strlen(line);
b.arr = new bool[b.len];
for (int i = 0; i < b.len; i++) {
b.arr[i] = (line[i] == '0' ? 0 : 1);
}
return in;
}
ostream& operator<<(ostream& out, const Binary& b)
{
for (int i = 0; i < b.len; i++) {
out << b.arr;
}
return out;
}
int main() {
Binary a;
cin >> a;
cout << a;
return 0;
}
The problem is with this line of code:
out << b.arr;
You are printing the array pointer, b.arr, instead of a value in the array.
This will work:
out << b.arr[i] ? '1' : '0';
You should also consider writing a destructor to free your previously allocated memory, and also free the previous array before overwriting it's pointer on this line:
b.arr = new bool[b.len];

How to use std::sort to sort particular objects from an array?

I have those classes and I want to sort an array of objects, considering x coordinate, and sort just those with a particular value to an attribute.
Class.h
#include <iostream>
#include <algorithm>
class Punct2D
{
protected:
int x, y;
public:
Punct2D() {};
~Punct2D() {};
int get_x() const;
int get_y() const;
void set_x(const int x);
void set_y(const int y);
friend std::ostream &operator << (std::ostream &flux, Punct2D dot);
friend std::istream &operator >> (std::istream &flux, Punct2D &dot);
};
class Punct2DColorat :public Punct2D
{
private:
char *color;
public:
Punct2DColorat() { this->color = NULL; };
~Punct2DColorat() {};
char *get_color();
void set_color(char *color);
bool operator<(Punct2DColorat dot);
};
Here I have the implementation.
#include "Class.h"
int Punct2D::get_x() const
{
return this->x;
}
int Punct2D::get_y() const
{
return this->y;
}
void Punct2D::set_x(const int x)
{
this->x = x;
}
void Punct2D::set_y(const int y)
{
this->y = y;
}
char *Punct2DColorat::get_color()
{
return this->color;
}
void Punct2DColorat::set_color(char *color)
{
this->color = new char[strlen(color) + 1];
for (int i = 0; i < strlen(color) + 1; i++) this->color[i] = color[i];
}
bool Punct2DColorat::operator<(Punct2DColorat dot)
{
return this->x < dot.get_x();
}
std::ostream &operator << (std::ostream &flux, Punct2D dot)
{
flux << "Punct(" << dot.get_x() << "," << dot.get_y() << ")\n";
return flux;
}
std::istream &operator >> (std::istream &flux, Punct2D &dot)
{
std::cout << "Introduceti x :";
flux >> dot.x;
std::cout << "Introduceti y :";
flux >> dot.y;
return flux;
}
And here is the Main.
#include "Class.h"
void main()
{
int n, it = 0; char *aux = new char[15]; bool value;
Punct2DColorat *dots;
std::cout << "Cate puncte introduceti :"; std::cin >> n;
dots = new Punct2DColorat[n];
for (int i = 0; i < n; i++)
{
std::cout << "Introduceti 0 pentru Punct2D, respectiv 1 pentru Punct2D colorat :";
std::cin >> value;
if (value)
{
std::cin >> dots[i];
std::cout << "Introduceti culoarea punctului :";
std::cin >> aux;
dots[i].set_color(aux);
}
else
{
std::cin >> dots[i];
}
}
std::sort(dots, dots + n, [](Punct2DColorat dot) { return dot.get_color() != NULL; });
for (int i = 0; i < n; i++)
{
std::cout << dots[i];
if (dots[i].get_color() != NULL)
{
std::cout << "Culoare :" << dots[i].get_color() << "\n";
}
std::cout << "\n";
}
}
I want to sort the dots with color !=NULL, I tried this, it works but I have a runtime error.
bool Punct2DColorat::operator<(Punct2DColorat dot)
{
if ((this->color != NULL) && (dot.get_color() != NULL))return this->x < dot.get_x();
return true;
}
How can I sort just the objects with color !=NULL and the other objects with color==NULL remain in the same position?
Here is an example:
//If have 3 objects in the following order stored in the dots array.
dots[0].get_x()=3;
dots[0].get_y()=3;
dots[0].get_color()="Red";
dots[1].get_x()=0;
dots[1].get_y()=0;
dots[1].get_color()=NULL;
dots[2].get_x()=1;
dots[2].get_y()=1;
dots[2].get_color()="Blue";
//After sort i want to have them like this:
dots[0].get_x()=1;
dots[0].get_y()=1;
dots[0].get_color()="Blue";
dots[1].get_x()=0;
dots[1].get_y()=0;
dots[1].get_color()=NULL;
dots[2].get_x()=3;
dots[2].get_y()=3;
dots[2].get_color()="Red";
Thanks.
The problem is, your comparison operator evaluates to true for any couple of non-colored points.
A possible solution is to construct a second vector, sort it and re-insert
std::vector<Punct2DColorat> tmp;
for (int i = 0; i < n; i++)
{
if (dots[i].get_color() != NULL)
{
tmp.push_back(dots[i]);
}
}
std::sort(tmp.begin(), tmp.end());
int j = 0;
for (int i = 0; i < n; i++)
{
if (dots[i].get_color() != NULL)
{
dots[i] = tmp[j];
++j;
}
}

Errors in Linking Files in Eclipse

I am having so much trouble with linking files in eclipse. I keep getting undefined reference errors, even though I added each file path to the Paths and Symbols setting in Project. I also included the header files. I have 1 Application file along with 3 implementation files and 2 header files.
Here is 1 implementation file:
#include <iostream>
#include <fstream>
#include "complex2.h"
#include "complexType.h"
using namespace std;
complexDB::complexDB(int lineCount)
{
length = lineCount;
c = new complexType[lineCount];
num_complex = 0;
}
void complexDB::set_Index(int i, const complexType& cT)
{
c[i] = cT;
}
void complexDB::set_numComplex(int n)
{
num_complex = n;
}
void complexDB::insert(const complexType& insertItem)
{
if (num_complex < length)
{
int oldi = num_complex;
c[oldi] = insertItem; // inserts item at the last index of array
num_complex++;
}
}
void complexDB::deletee(const complexType& deleteItem)
{
}
void complexDB::list()
{
cout << "in list:" << endl;
cout << length << "\t" << num_complex << endl;
for (int i = 0; i < num_complex; i++)
{
cout << c[i];
}
}
void complexDB::save()
{
ofstream fout;
fout.open("126complex.txt");
if (fout.is_open())
{
cout << "is open" << endl;
}
for (int i = 0; i < num_complex; i++)
{
fout << c[i];
}
}
complexDB::~complexDB()
{
delete[] c;
}
Here is another implementation file:
#include <fstream>
#include <sstream>
#include "complexType.h"
//#include "complex2.cpp"
using namespace std;
#define CA_MAX_SIZE 10
#define ComplexFileName "126.txt"
void CreateComplexFile()
{
complexType ca[CA_MAX_SIZE];
ofstream fout;
fout.open(ComplexFileName);
for (int i = 0; i < CA_MAX_SIZE; i++)
{
ca[i].setComplex(i, i + 1);
fout << ca[i];
}
fout.close();
}
void ReadComplexFile()
{
complexType ca[CA_MAX_SIZE];
ifstream fin;
fin.open(ComplexFileName);
for (int i = CA_MAX_SIZE - 1; i >= 0; i--)
{
fin >> ca[i];
}
fin.close();
}
void ReadComplexFileEOF()
{
complexType ca[CA_MAX_SIZE];
ifstream fin;
fin.open(ComplexFileName);
int i = 0;
while (!fin.eof())
{
fin >> ca[i++];
}
fin.close();
}
void ReadComplexFileTwice()
{
complexType ca[CA_MAX_SIZE];
ifstream fin;
fin.open(ComplexFileName);
for (int i = CA_MAX_SIZE - 1; i >= 0; i--)
{
fin >> ca[i];
}
fin.clear();
fin.seekg(0, ios::beg);
int i = 0;
while (!fin.eof())
{
fin >> ca[i++];
}
fin.close();
}
void ImportComplexFile(string fname)
{
ifstream fin;
double real, im;
char plusorminus, ichar;
string oneline;
fin.open(fname.c_str());
while (!fin.eof())
{
getline(fin, oneline);
stringstream(oneline) >> real >> plusorminus >> im >> ichar;
}
fin.close();
}
void ImportComplexFile2(string fname)
{
ifstream fin;
fin.open(fname.c_str());
double real, im;
char plusorminus, ichar;
complexType c;
string oneline;
while (!fin.eof())
{
getline(fin, oneline);
real = 0;
im = 0;
plusorminus = '\0';
ichar = '\0';
stringstream(oneline) >> real >> plusorminus >> im >> ichar;
switch (plusorminus)
{
case '-':
im = -im;
break;
case 'i':
im = real;
real = 0;
break;
case '\0':
im = 0;
break;
}
c.setComplex(real, im);
cout << c << endl;
}
fin.close();
}
For both files, anything with complexType gives the error:
undefined reference to `complexType::complexType(double, double)'
Also for the main file I get this error:
cannot find -lC:\Users\Altemush\Documents\SJSU_Dev\projects_mingw\complex2\src
I suppose I'm not linking the files together correctly, een though I thought I did. Please, can anyone help me?
Here is complexType.h:
#ifndef H_complexNumber
#define H_complexNumber
#include <iostream>
using namespace std;
class complexType
{
friend ostream& operator<< (ostream&, const complexType&);
friend istream& operator>> (istream&, complexType&);
friend complexType operator+(const complexType& one,
const complexType& two);
public:
void setComplex(const double& real, const double& imag);
complexType(double real = 0, double imag = 0);
complexType& operator=(const complexType &rhs);
complexType operator-(const complexType& two);
complexType operator-(int x);
int realPart;
int imaginaryPart;
};
#endif
Here is complexType.cpp:
#include <iostream>
#include "complexType.h"
using namespace std;
ostream& operator<< (ostream& os, const complexType& complex)
{
os << "(" << complex.realPart << ", "
<< complex.imaginaryPart << ")" << endl;
return os;
}
istream& operator>> (istream& is, complexType& complex)
{
char ch;
// is >> ch;
is >> complex.realPart;
is >> ch;
is >> complex.imaginaryPart;
is >> ch;
return is;
}
complexType::complexType(double real, double imag)
{
setComplex(real, imag);
}
void complexType::setComplex(const double& real, const double& imag)
{
realPart = real;
imaginaryPart = imag;
}
complexType operator+(const complexType& one, const complexType& two)
{
complexType temp;
temp.realPart = one.realPart + two.realPart;
temp.imaginaryPart = one.imaginaryPart + two.imaginaryPart;
return temp;
}
complexType complexType::operator-( const complexType &operand2 )
{
return complexType( realPart - operand2.realPart,
imaginaryPart - operand2.imaginaryPart );
}
complexType complexType::operator-( int operand2 )
{
return complexType( realPart - operand2,
imaginaryPart - operand2 );
}
complexType& complexType::operator=(const complexType &rhs){
realPart = rhs.realPart;
imaginaryPart = rhs.imaginaryPart;
return *this;
}

Questions about C++ operators overloading

I am a beginner of C++ learning, and I have some questions about the '<<' and '>>'.
Why the results of cout are not correct? At the same time, after I input the coordinate of c, the program froze.
Code:
class Vector
{friend istream &operator >> (istream &is,Vector &vec );
friend ostream &operator << (ostream &os,Vector &vec );
private:
int num;
double *cor;
public:
Vector(int n=0,double *c=NULL);//
};
int main()
{ double b[5]={1,2,3,4,5};
Vector a(5,b);
cout<<a;
Vector c(2);
cin>>c;
}
Vector::Vector(int n,double *c)
{
num=n;
double *cor=new double[num];
if (c) {
for (int i=0;i<n;i++) {cor[i]=c[i];cout<<cor[i]<<endl;}
}
}
istream &operator >> (istream &is,Vector &vec )
{ cout<<"Input the coordinate:";
for (int i=0;i<vec.num;i++)
is>>vec.cor[i];
return is;
}
ostream &operator << (ostream &os,Vector &vec )
{
for (int i=0;i<vec.num;i++){
os<<vec.cor[i];}
return os;
}
double *cor=new double[num];
You're declaring a local variable named cor, not initializing the member cor. It should be:
cor = new double[num];
However in real code, you would use an unique_ptr to a double array, which deletes the array automatically (with no additional overhead):
#include <iostream>
#include <memory> // for unique_ptr
using namespace std;
class Vector {
public:
Vector(int = 0, double* = nullptr);
private:
friend istream& operator>>(istream&, Vector& vec);
friend ostream& operator<<(ostream&, Vector& vec);
int num;
unique_ptr<double[]> array;
};
int main() {
double b[] = {1, 2, 3, 4, 5};
Vector a(5, b);
cout << a;
Vector c(2);
cin >> c;
}
Vector::Vector(int n, double* c) {
num = n;
array = make_unique<double[]>(n); // in C++14
//array = unique_ptr<double[]>(new double[n]); // in C++11
if (!c) return;
for (int i = 0; i < n; i++) {
array[i] = c[i];
cout << array[i] << endl;
}
}
istream& operator>>(istream& is, Vector& vec) {
cout << "Input the coordinates: ";
for (int i = 0; i < vec.num; i++)
is >> vec.array[i];
return is;
}
ostream& operator<<(ostream& os, Vector& vec) {
for (int i = 0; i < vec.num; i++)
os << vec.array[i];
return os;
}
Small but important tip:
using 'const':
> ostream &operator << (ostream &os,const Vector &vec)
since vec is received by reference and you are not going to change it.

c++ getting error trying to call a function that reads a file

I have a function Readf I'm trying to call to fill in an array inside each constructor, I tried only with the default constructor with a code statement ReadF(cap,*point,counter);.
The second argument is what's giving me a problem in figuring out, I would get an error with '&' beside it or ' '. And I get an external error with the '*', I'm still new to c++ so I'm not experienced in dealing with classes.
I have to use the ReadF member function, are there other ways instead of calling it to get the results I need.
Also some of the code for the functions I have might not be perfect, but I would like to deal with this problem first before I move on to another.
array.h:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class AR {
public:
AR();
AR(int);
AR(const AR&);
//~AR();
void ReadF(const int, string&, int);
AR& operator=(const AR&);
friend ostream& operator<<(ostream&, AR&);
friend ifstream & operator>>(ifstream&, AR&);
private:
string* point;
int counter;
int cap;
};
array.cpp:
#include "array.h"
AR::AR() {
counter = 0;
cap = 2;
point = new string[cap];
}
AR::AR(int no_of_cells) {
counter = 0;
cap = no_of_cells;
point = new string[cap];
}
AR::AR(const AR& Original) {
counter = Original.counter;
cap = Original.cap;
point = new string[cap];
for(int i=0; i<counter; i++) {
point[i] =Original.point[i];
}
}
// AR::~AR() {
// delete [ ]point;
//}
ostream& operator<<(ostream& out, AR& Original) {
cout << "operator<< has been invoked\n";
for (int i=0; i< Original.counter; i++) {
out << "point[" << i <<"] = " << Original.point[i] << endl;
}
return out;
}
AR& AR::operator=(const AR& rhs) {
cout << "operator= invoked\n";
if (this == &rhs)
return *this;
point = rhs.point;
return *this;
}
void ReadF(const int neocap, string& neopoint, int neocounter) {
ifstream in;
in.open("sample_strings.txt"); //ifstream in; in.open("sample_data.txt");
if (in.fail())
cout<<"sample_data not opened correctly"<<endl;
while(!in.eof() && neocounter < neocap) {
in >> neopoint[neocounter];
neocounter++;
}
in.close();
}
ifstream& operator>>(ifstream& in, AR& Original) {
Original.counter = 0;
while(!in.eof() && Original.counter < Original.cap) {
in >> Original.point[Original.counter];
(Original.counter)++;
}
return in;
}
It appears you are missing AR:: in the ReadF definition.
On second thought you are reading into an index of neopoint that likely isn't allocated yet. You could change ReadF to read:
void ReadF(const int neocap, string& neopoint,int neocounter)
{
ifstream in;
in.open("sample_strings.txt"); //ifstream in; in.open("sample_data.txt");
if (in.fail())
{
cout<<"sample_data not opened correctly"<<endl;
return;
}
neopoint.resize(neocap);
while(neocounter < neocap && in >> neopoint[neocounter])
{
neocounter++;
}
in.close();
}
Although you probably want to look into stringstream
std::ifstream in("sample_strings.txt");
if (in)
{
std::stringstream buffer;
buffer << in.rdbuf();
in.close();
inneopoint= buffer.str();
}