C++, classes, Segmentation fault, all over the programme - c++

I got an exercise form my teacher (the main() function) and was supposed to write functions and classes so that it would work. But I have seg fault and have no idea what to do about it. I would be grateful for any recommendations.
#include <iostream>
class TSeries{
public:
TSeries()
{
_size = 0;
_capacity = 0;
_tab = NULL;
}
TSeries(float *tab, const int size)
{
_tab = new float[size];
for(int i =0;i<size;i++) _tab[i] = tab[i];
_size = size;
}
~TSeries(){delete [] _tab;}
TSeries & operator+=(float value){return insert(value);}
TSeries & operator,(float value){return insert(value);}
TSeries & operator+(const TSeries & s)
{
// if(this->_size != s._size) std::cout<<"Size doesn't match!"<<std::endl;
/*else
{
std::cout<<"whee";
for(int i; i<this->_size;i++)
{
//this->_tab[i] += s._tab[i];
std::cout<<"nothing";
}
return *this;
}*/
std::cout<<"sth";
}
TSeries & operator()(int position1, int position2){}
TSeries & insert(float k)
{
if(_size >= _capacity) Enlarge();
_tab[_size++] = k;
return *this;
}
friend std::ostream & operator<<(std::ostream & out, const TSeries & s);
private:
int _size, _capacity;
float *_tab, *_itr;
static int _nr;
void Enlarge()
{
_capacity = 2 * _capacity + 1;
float *tmp = new float[_capacity];
for( int i=0;i<_size;++i)
{
tmp[i] = _tab[i];
}
delete [] _tab;
_tab = tmp;
}
};
std::ostream & operator<<(std::ostream & out, const TSeries & s)
{
int przedostatni = s._size - 1;
out<<"(";
for(int i =0;i<s._size;i++)
{
out<<(int)s._tab[i];
if(i != przedostatni)
out<<",";
}
out<<")"<<std::endl;
}
using namespace std;
int main(int argc, char **argv) {
TSeries series1;
series1 += 1.,2.,4.,2.;
cout<<"Series1: "<<series1<<endl;
const int size=7;
float tab[size] = {3.,3.,3.,4.,5.,1.,0.};
const TSeries series2(tab,size);
cout<<"Series2: "<<series2<<endl<<endl;
TSeries series3 = series1+series2;
cout<<"Series3: "<<series3<<endl<<endl;
series1+=1.,0.,3.;
series3=series1+series2;
cout<<" "<<series1<<endl;
cout<<" +"<<series2<<endl;
cout<<" ---------------------"<<endl;
cout<<"Series3: "<<series3<<endl<<endl;
//TSeries series4=series1(2,4);
cout<<"Series4: "<<series3<<endl;
return 0;
}

You fixed one of your problems (the assignment to the _tab pointer).
The other problem should have caused a warning from the compiler.
You need to return out from your operator<< method.
Note that you also should use the out parameter, rather than always using cout in the the method.

Related

How to implemet copy constructor in C++

this is the header of a class that I have been designing for an assignment. I have included constructors, a destructors, as well as overloaded operators. Could you give me a hint how to properly define the constructors in a class using c++ 20 most recent features in an efficient way.
#ifndef VECTOR_DOUBLE_H
#define VECTOR_DOUBLE_H
#include <memory>
#include <vector>
class vector_double {
public:
vector_double(int size);
vector_double(std::initializer_list<double> lst);
vector_double(const double* array, int size);
vector_double(const vector_doubler& other);
vector_doubleoperator=(const vector_double& other);
// because I use a managed pointer I don't need a destructor
~vector_double() noexcept = default;
void set(int index, double val);
double& get(int index);
const double& get(int index) const;
int size() const;
void reset(double val);
void fill_from(std::initializer_list<double> lst);
void fill_from(const double* array, int size);
int copy_to(std::vector<double>& vec) const;
double& operator[](int index);
const double& operator[](int index) const;
operator double() const;
vector_double add(const vector_double& other) const;
vector_doubleadd(double number) const;
vector_doublemul_by(double number) const;
void resize(int size);
friend std::ostream& operator<<(std::ostream& out, const vector_double& vec);
private:
std::unique_ptr<double[]> m_array;
int m_size;
};
inline std::ostream& operator<<(std::ostream& out, const vector_double& vec){
if (vec.m_size == 0){
out << "{ }";
}
else{
auto first = true;
out << '{';
for (int i=0; i < vec.m_size; ++i){
if (!first)
out << ", ";
else
first = !first;
out << vec.m_array[i];
}
out << '}';
}
return out;
}
#endif //VECTOR_DOUBLE_H
This example definition may help, I tried sticking to C++20 features:
#include <cmath>
#include "vector_double.h"
vector_double::vector_double(int size):
m_array{ new double[size] },
m_size{size}
{}
vector_double::vector_double(std::initializer_list<double> lst): //Constructor that takes an init list
vector_double(lst.size())
{
std::copy(lst.begin(), lst.end(), m_array.get());
}
vector_double::vector_double(const double* array, int size): //Constructor that takes array and size
vector_double(size)
{
// std::copy(array, array + size, m_array.get());
std::copy(&array[0], &array[size], m_array.get());
}
vector_double::vector_double(const vector_double& other): //Copy Constructor
vector_double(other.m_size)
{
std::copy(&other.m_array[0], &other.m_array[m_size], &m_array[0]);
}
vector_double& vector_double::operator=(const vector_double& other) {
if (this != &other) {
if (m_size != other.m_size) {
auto* array = new double[other.m_size];
m_array.reset(array);
m_size = other.m_size;
}
std::copy(&other.m_array[0], &other.m_array[m_size], &m_array[0]);
}
return *this;
}
void vector_double::set(int index, double val) {
if (index < 0 || index > m_size)
throw std::out_of_range("oooh my!");
m_array[index] = val;
}
double& vector_double::get(int index) {
if (index < 0 || index > m_size)
throw std::out_of_range("oooh my!");
return m_array[index];
}
const double& vector_double::get(int index) const {
if (index < 0 || index > m_size)
throw std::out_of_range("oooh my!");
return m_array[index];
}
int vector_double::size() const {
return m_size;
}
void vector_double::reset(double val) {
for (int i=0; i<m_size; ++i){
m_array[i] = val;
}
}
void vector_double::fill_from(std::initializer_list<double> lst) {
int size = std::min((int)lst.size(), m_size);
std::copy(lst.begin(), lst.begin() + size, &m_array[0]);
}
void vector_double::fill_from(const double* array, int size) {
size = std::min(size, m_size);
for (int i = 0; i < size; ++i) {
m_array[i] = array[i];
}
}
int vector_double::copy_to(std::vector<double>& vec) const {
for (int i = 0; i < m_size; ++i) {
vec.push_back(m_array[i]);
}
return m_size;
}
double& vector_double::operator[](int index) {
return m_array[index];
}
const double& vector_double::operator[](int index) const { //Overloading "[]" operator
return m_array[index];
}
vector_double::operator double() const {
double sum = 0.0;
for (int i = 0; i < m_size; ++i) {
sum += m_array[i] * m_array[i];
}
return std::sqrt(sum);
}
vector_double vector_double::add(const vector_double& other) const {
if (m_size != other.m_size)
throw std::logic_error("size mismatch");
auto copy = *this;
for (int i = 0; i < m_size; ++i) {
copy[i] += other[i];
}
return copy;
}
vector_double vector_double::add(double number) const {
auto copy = *this;
for (int i = 0; i < m_size; ++i) {
copy[i] += number;
}
return copy;
}
vector_double vector_double::mul_by(double number) const {
auto copy = *this;
for (int i = 0; i < m_size; ++i) {
copy[i] *= number;
}
return copy;
}
void vector_double::resize(int size) {
if (size != m_size){
auto array = new double[size] {0,};
auto common = std::min(size,m_size);
for (int i = 0; i < common; ++i) {
array[i] = m_array[i];
}
m_array.reset(array);
m_size = size;
}
}

I cannot assign values to dynamic cstring and modfiy them

For a homework problem, I need to implement a cstring class and overload operators. However, I am having trouble initializing the char array. In the following code,
StringClass::StringClass()
{
c = new char[10];
c = "Default";
stringlength = strlen(c);
}
#pragma once
#include <fstream>
class StringClass
{
private:
char* c;
int stringlength;
public:
StringClass();
~StringClass();
void print()const;
StringClass(char*, int);
StringClass(const StringClass*);
StringClass& operator=(const StringClass*);
friend std::istream& operator>>(std::istream&, StringClass*);
friend std::ostream& operator<<(std::ostream&, const StringClass*);
StringClass& operator+(const StringClass*);
char operator[](int);
};
For the line c = "Default";
I get an error const char* cannot be assigned to char*,but I did not set c to a const. If I change charc to const char c, I can set it equal to default in the constructor but then I cannot modify it further. Why is this?
EDIT: I can change the declaration to this and it works properly. Is this the correct way to do this?
c = new char[10]{ "Default" };
Full implementation file,
#include "StringClass.h"
#include <fstream>
#include <iostream>
StringClass::StringClass()
{
c = new char[10];
c = "Default";
stringlength = strlen(c);
}
StringClass::~StringClass()
{
c = NULL;
delete c;
}
void StringClass::print()const
{
for (int i = 0; i < 10; ++i)
std::cout << c[i];
std::cout<< std::endl;
std::cout << stringlength;
}
StringClass::StringClass(const StringClass* p)
{
for (int i = 0; i < 10; ++i)
{
c[i] = p->c[i];
}
stringlength = p->stringlength;
}
StringClass& StringClass::operator=(const StringClass* a)
{
if (this == a)
return *this;
else
{
for(int i = 0; i < 10; ++i)
c = &a->c[i];
stringlength = a->stringlength;
}
return *this;
}
//std::istream& operator>>(std::istream& in, StringClass* a)
//{
//in >> a->c >> a->stringlength;
//return in;
//}
std::ostream& operator<<(std::ostream& out, const StringClass* a)
{
out << a->c << " " << a->stringlength << std::endl;
return out;
}
StringClass& StringClass::operator+(const StringClass* a)
{
StringClass temp;
temp.c = c + *a->c;
temp.stringlength = stringlength + a->stringlength;
return temp;
}
char StringClass::operator[](int a)
{
return c[a];
}
c = new char[10]{ "Hello" };

Repeating words in string class

So I have to write a string class, and I need help with reading from a file into a vector of string classes I've created. It somewhat works, as it reads from the file but it repeats the the word read in a few times depending on which word it's on.
// .h
/*Class description:
A string class. Various functions for the class.
String is passed into objects of the class. Reads
and writes to files.*/
#ifndef MYString12_H
#define MYString12_H
#include <fstream>
using namespace std;
class MYString12
{
public:
MYString12();
MYString12(const MYString12 & mstr);
MYString12(const char* ptr);
~MYString12();
MYString12& operator = (const MYString12& argStr);
friend MYString12 operator + (const MYString12& str1, const MYString12& str2);
char operator [] (int index);
bool operator > (const MYString12& argStr2);
bool operator < (const MYString12& argStr2);
bool operator == (const MYString12& argStr);
friend istream& operator >> (istream& istr, MYString12& argStr);
friend ostream& operator << (ostream& istr, MYString12& argStr);
int length() const;
int capacity()const;
char at(int index);
const char* c_str()const;
static int getCurrentCount();
static int getCreatedCount();
private:
char* str;
int cap = 20;
int end;
const int compareTo(const MYString12& argStr);
static int currentCount;
static int createdCount;
};
#endif
Here is class cpp file
// MYString12.cpp
#include "stdafx.h"
#include "MYString12.h"
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cstdlib>
using namespace std;
int MYString12::createdCount = 0;
int MYString12::currentCount = 0;
// default constructor
MYString12::MYString12()
{
cap = 20;
end = 0;
str = new char[cap];
str[end] = '\0';
createdCount++;
currentCount++;
}
// copy constructor
MYString12::MYString12(const MYString12& mstr)
{
this->end = mstr.end;
this->cap = mstr.cap;
this->str = new char[mstr.cap];
while (end >= cap) {
cap += 20;
}
for (int i = 0; i < end; i++) {
str[i] = mstr.str[i];
}
//mstr.str[end] = '\0';
createdCount++;
currentCount++;
}
// constructor with string passed in
MYString12::MYString12(const char* ptr)
{
int i = 0;
while (ptr[i] != '\0') {
end++;
i++;
}
while (end >= cap) {
cap += 20;
}
str = new char[cap];
for (int j = 0; j < end; j++) {
str[j] = ptr[j];
}
createdCount++;
currentCount++;
}
// destructor
MYString12::~MYString12()
{
delete[] str;
currentCount--;
}
// overloaded assignment operator
GAString12& GAString12::operator = (const GAString12& mstr)
{
if (this == &mstr) {
return *this;
}
this->end = mstr.end;
this->cap = mstr.cap;
while (end >= cap) {
cap += 20;
}
for (int i = 0; i < end; i++) {
str[i] = mstr.str[i];
}
//mstr.str[end] = '\0';
return *this;
}
// overloaded concatanation operator
MYString12 operator + (const MYString12& str1, const MYString12& str2)
{
int temp = str1.end + str2.end + 1;
char tempArray[200];
int i = 0;
int j = 0;
while (i < temp)
{
if (i < str1.end)
{
tempArray[i] = str1.str[i];
i++;
} else {
tempArray[i] = str2.str[j];
i++;
j++;
}
}
tempArray[i] = '\0';
MYString12 concatenatedObj(tempArray);
return concatenatedObj;
}
// overloaded index operator
char MYString12::operator [] (int index)
{
return str[index];
}
// overloaded greater than operator
bool MYString12::operator > (const MYString12& argStr)
{
if ((*this).compareTo(argStr) > 0)
{
return true;
}
else {
return false;
}
}
// overloaded less than operator
bool MYString12::operator < (const MYString12& argStr)
{
if ((*this).compareTo(argStr) < 0)
{
return true;
}
else {
return false;
}
}
// overloaded equals equals operator
bool MYString12::operator == (const MYString12& argStr)
{
if ((*this).compareTo(argStr) == 0)
{
return true;
}
else {
return false;
}
}
// compares ascii values of objStr and argStr
const int MYString12::compareTo(const MYString12& argStr)
{
int asciiSubtraction = 0;
int limit = 0;
if (end <= argStr.end)
{
limit = end;
}
else {
limit = argStr.end;
}
int i = 0;
while (i <= limit && (str[i] == argStr.str[i])) {
i++;
}
asciiSubtraction = str[i] - argStr.str[i];
return asciiSubtraction;
}
// overloaded extraction operator
istream& operator >> (istream& istr, MYString12& argStr)
{
char temp[100];
istr >> temp;
argStr = GAString12(temp);
return istr;
}
// overloaded insertion operator
ostream& operator << (ostream& ostr, MYString12& argStr)
{
int i = 0;
while (argStr.str[i] != '\0')
{
ostr << argStr.str;
i++;
}
return ostr;
}
// returns size of passed in string
int MYString12::length() const
{
return end;
}
// returns size of memory allocated
int MYString12::capacity() const
{
return cap;
}
// returns a char of string at passed index
char MYString12::at(int index)
{
if (index < 0 || index > end) {
return '\0';
}
else {
return str[index];
}
}
// returns passed in string as c string
const char* MYString12::c_str() const
{
createdCount++;
currentCount++;
return str;
}
// returns the amount of alive instances of class
int MYString12::getCurrentCount()
{
return currentCount;
}
// returns the amount of overall created instances of class
int MYString12::getCreatedCount()
{
return createdCount;
}
And here is main
// main
int main()
{
vector<MYString12> word(100);
ifstream fin;
fin.open("infile3.txt");
if (fin.fail()) {
cout << "Error." << endl;
exit(1);
}
int wordCount = 0;
while (fin >> word[wordCount]) {
cout << word[wordCount];
system("pause");
wordCount++;
}
word.resize(wordCount);
fin.close();endl;
return 0;
}
It doesn't print out to the console any of the words. Nothing is printed. Why doesn't it print?

Printing unexpected blank lines and crash

For a school project I need to make a constructor with a recursive sort function that is given from a teacher in it, with a few function that I had to make myself. When I start the program the result shows a few blank lines and then the expected output. But when I increase int_buffer a(100) or higher and trys again the program just crashes. I think it might be memory leak, but not quite sure and don't know how to fix the problem either. I think that the problem is in int_sorted.cpp
Code:
main.cpp:
#include "int_buf.h"
#include "int_sorted.h"
#include <iostream>
#include <string>
void f(int_buffer& buf){
for (int* i = buf.begin();i != buf.end();i++) {
}
for (const int* i = buf.begin(); i != buf.end(); i++) {
}
}
int main(){
int_buffer a(100);
int_buffer b(50);
int r[] = {1,4,10,2,54,3,6,20};
for (size_t i = 0; i < a.size(); i++){
a[i] = std:: rand();
b[i] = std::rand();
}
int_sorted x(a.begin(), a.size());
}
std::cout << "\n";*/
for (int i = 0; i < x.size();i++) {
std::cout<<*(x.begin() + i)<<"\n";
return 0;
}
int_buf.h:
#ifndef INT_BUF_H
#define INT_BUF_H
#include <cstdlib>
class int_buffer {
public:
bool check_sorted(int_buffer int_check);
explicit int_buffer(size_t size);
int_buffer(const int * source, size_t size);
int_buffer(const int_buffer & rhs);
int_buffer(int_buffer && rhs);
int_buffer & operator =(const int_buffer & rhs);
int_buffer & operator =(int_buffer && rhs);
int& int_buffer::operator[](size_t index);
size_t size() const;
int * begin();
int * end();
const int* begin() const;
const int* end() const;
~int_buffer();
private:
size_t sz;
int* buf;
};
#endif
int_buf.cpp:
#include "int_buf.h"
#include <algorithm>
#include <iostream>
#include <cstddef>
bool int_buffer::check_sorted(int_buffer int_check){
int* a = int_check.begin();
int* b = nullptr;
while (a != int_check.end() -1){
b= a + 1;
if (*a > *b){
return false;
}
else{
a = b;
}
}
return true;
}
int_buffer::int_buffer(size_t size) :sz(size), buf(new int[size]){}
int_buffer::int_buffer(const int* source, size_t size) : sz(size), buf(new int[size]){
for (size_t i = 0; i < sz;i++){
buf[i] = *(source+i);
}
}
int_buffer::int_buffer(const int_buffer& rhs):sz(rhs.sz),buf(new int[rhs.sz]){
std::copy(rhs.begin(), rhs.end(), begin());
}
int_buffer::~int_buffer(){
delete[] buf;
}
int_buffer::int_buffer(int_buffer && rhs) :buf(nullptr), sz(0){
std::swap(buf, rhs.buf);
std::swap(sz, rhs.sz);
}
int_buffer& int_buffer::operator =(const int_buffer & rhs){
int_buffer temp(rhs);
std::swap(temp.buf,buf);
std::swap(temp.sz,sz);
return *this;
}
int_buffer & int_buffer::operator =(int_buffer&& rhs){
std::swap(buf, rhs.buf);
std::swap(sz, rhs.sz);
return *this;
}
int& int_buffer::operator[](size_t index){
return buf[index];
}
size_t int_buffer::size() const{
return sz;
}
int* int_buffer::begin(){
return buf;
}
int* int_buffer::end(){
return buf + sz;
}
const int* int_buffer::begin() const{
return buf;
}
const int* int_buffer::end() const{
return buf + sz;
}
int_sorted.h:
#ifndef INT_SORTED_H
#define INT_SORTED_H
#include "int_buf.h"
#include <iostream>
class int_sorted{
public:
int_sorted(const int* source, std::size_t size);
std::size_t size() const;
int* insert(int value);
const int* begin() const;
const int* end() const;
int_sorted merge(const int_sorted& merge_with) const;
int_sorted sort(const int*begin, const int* end);
void int_sorted::selSort(int_sorted& arr, int size);
private:
int_buffer buffer;
};
#endif
int_sorted.cpp:
#include "int_sorted.h"
#include "int_buf.h"
#include <iostream>
#include <algorithm>
#include <cstddef>
int_sorted::int_sorted(const int* source, size_t size) :buffer(source, size){
if (buffer.size() < 1) {}
else {
if (buffer.check_sorted(buffer)) {}
else {
int_buffer tmp(buffer.begin(), 0);
tmp = sort(buffer.begin(), buffer.end()).buffer;
buffer = tmp;
}
}
}
size_t int_sorted::size() const{
return buffer.size();
}
int* int_sorted::insert(int value) {
int_buffer tmp(buffer.size()+1);
for (int i =0; i < buffer.size();i++) {
tmp[i] = buffer[i];
}
tmp[buffer.size()] = value;
this->buffer = tmp;
return (buffer.end()-1);
}
const int* int_sorted::begin() const{
return buffer.begin();
}
const int* int_sorted::end() const{
return buffer.end();
}
int_sorted int_sorted::merge(const int_sorted& merge_with) const{
int_sorted c(this->begin(), 0);
const int* travA = this->begin();
const int* travB = merge_with.begin();
int tCountA = 0;
int tCountB = 0;
while ((travA + tCountA)!=this->end()&&(travB + tCountB )!=merge_with.end()){
if (travA[tCountA] < travB[tCountB]){
c.insert(travA[tCountA]);
tCountA++;
}
else{
c.insert(travB[tCountB]);
tCountB++;
}
}
while ((travA + tCountA)!=this->end()){
c.insert(travA[tCountA]);
tCountA++;
}
while ((travB + tCountB) != merge_with.end()){
c.insert(travB[tCountB]);
tCountB++;
}
std::cout << "\n";
return c;
}
int_sorted int_sorted::sort(const int* begin, const int* end) {
if (begin == end)return int_sorted(begin, 1);
if (begin == end - 1)return int_sorted(begin, 1);
ptrdiff_t half = (end - begin) / 2;
const int* mid = begin + half;
return sort(begin, mid).merge(sort(mid, end));
}
This is what it looks like when I have int_buffer a(20);
unexpected blank lines

segmentation fault scrabble game

im working on a little scrabblegame which i read a txtfile and create a hashtable for all words inside. Word is a specific class which contains a vector of .
i want to create a hashmap by my own and define the length of my "Dictionary" is 50000. im using a nullpointer to reserve all index of my array. If i want to print to my hashtable, compiler tells me a segmentation fault. does any one seems the error?
the headerfile:
class Dictionary {
public:
Dictionary();
Dictionary(string filepath);
friend std::ostream& operator<<(std::ostream& os, const Dictionary& obj);
bool find(const Word& word);
vector<Word> allPossibleWords(const vector<Character>& tiles);
// struct compare {
//
// bool operator()(const Word& a, const Word& b) {
// return a.operator<(b);
// }
// } myCompare;
vector<Word> m_allWords;
vector<Word>::iterator itVecWords;
static const int table_size = 500000;
// std::array <Word*, table_size> arrWords = {nullptr};
Word* arrWords[table_size] = {nullptr};
int hash(Word new_word);
void addItem(Word word);
void printHashTable();
the cpp:
Dictionary::Dictionary(string filepath) {
ifstream datei(filepath.c_str());
while (datei.good() && !datei.eof()) {
string temp;
string temp1;
string::size_type pos;
getline(datei, temp);
pos = temp.find(" ");
temp1 = temp.substr(0, pos);
Word new_word(temp1);
addItem(new_word);
}
datei.close();
}
std::ostream& operator<<(std::ostream& os, const Dictionary& obj) {
for (int i = 0; i < obj.m_allWords.size(); i++) {
os << obj.m_allWords[i] << endl;
}
return os;
}
bool Dictionary::find(const Word& word) const {
if (std::binary_search(m_allWords.begin(), m_allWords.end(), word)) {
return true;
}
return false;
}
vector<Word> Dictionary::allPossibleWords(const vector<Character>& tiles) const {
vector<Word> ergebnis;
string tmp;
int cnt = 0;
for (int i = 0; i < tiles.size(); i++) {
tmp += tiles[i].GetC();
}
sort(tmp.begin(), tmp.end());
for (int i = 1; i <= tiles.size(); i++) {
do {
string piece = tmp.substr(0, i);
do {
Word search = Word(piece);
//Überschreibt immer der in Ergebnis existierte Wert
if (find(search) && std::find(ergebnis.begin(), ergebnis.end(), search) == ergebnis.end()) {
ergebnis.push_back(search);
}
} while (next_permutation(piece.begin(), piece.end()));
} while (next_permutation(tmp.begin(), tmp.end()));
}
return ergebnis;
}
int Dictionary::hash(Word new_word) {
int index = 0;
for (auto u : new_word.new_Character) {
index += (int) u.GetC();
}
index = index * (int) new_word.new_Character.at(0).GetC();
index = index * (int) new_word.new_Character.at(new_word.new_Character.size() - 1).GetC();
return index % table_size;
}
void Dictionary::addItem(Word word) {
int index = hash(word);
if (arrWords[index] == nullptr) {
arrWords[index] = new Word(word);
} else {
Word* ptr = arrWords[index];
Word* neu = new Word(word);
while (ptr->getNextWord() != nullptr) {
ptr = ptr->getNextWord();
}
ptr->setNextWord(neu);
}
}
void Dictionary::printHashTable() {
Word* tmp;
for (int i = 0; i < table_size; i++) {
tmp = arrWords[i];
if (tmp != nullptr) {
tmp->printWord();
cout << "Index : " << i;
}
tmp = tmp->getNextWord();
}
}
class Word {
public:
Word();
Word(string m_wort);
int length() const;
int points() const;
friend std::ostream& operator<<(std::ostream& os, const Word& obj);
bool operator==(const Word& right) const; /
bool operator!=(const Word& right) const;
bool contains(const Character& c) const;
Word substr(int start, int end) const;
bool operator<(const Word& right) const;
vector <Character> new_Character;
Word* Next = nullptr;
void setNextWord(Word*);
Word* getNextWord();
void printWord();
string getWordAsString();
CPP File:
void Word::setNextWord(Word* w) {
Next = w;
}
Word* Word::getNextWord() {
return Next;
}
void Word::printWord() {
string s = "";
for (int i = 0; i < new_Character.size(); i++) {
s += new_Character.at(i).GetC();
}
cout << s << endl;
}
string Word::getWordAsString() {
string s;
for (int i = 0; i < new_Character.size(); i++) {
s += new_Character.at(i).GetC();
}
return s;
}