Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: Segmentation Fault in c++ code.  (gelezen 1777 keer)

Offline bart85

  • Lid
Segmentation Fault in c++ code.
« Gepost op: 2018/02/28, 14:30:09 »
In onderstaande code heb ik een segmentatiefout. De fout ontstaat in de functie AddWordpair. Het lijkt erop dat de gegevens verloren gaan bij het toevoegen van een object van het wordpair class aan de array. Dit is een array gedeclareerd met een pointer. Is het nodig om een copyconstructor te gebruiken. Ik weet niet goed hoe de copy constructor werkt.

#include <iostream>

class wordpair{
public:
wordpair(){}
wordpair(std::string word1, std::string word2):ItsWord1(word1),ItsWord2(word2){}
wordpair(const wordpair &);

std::string GetItsWord1() const {return ItsWord1;}
std::string GetItsWord2() const {return ItsWord2;}
void SetBothWords(std::string word1,std::string word2){ItsWord1=word1;ItsWord2=word2;}
private:
std::string ItsWord1,ItsWord2;
//niets
};

wordpair::wordpair(const wordpair & rhs){}

class dictionary{
public:
dictionary();
dictionary(const dictionary &);
~dictionary();
void AddWordpair(wordpair wordpair);
void AddWordpair(std::string word1, std::string word2);
void SetItsLang(std::string lang1,std::string lang2){ItsLang1=lang1;ItsLang2=lang2;}
std::string GetItsLang1() const {return ItsLang1;}
std::string GetItsLang2() const {return ItsLang2;}
wordpair GetItsWordPairs(int n){return pItsWordPairs[n];}
private:
int Itsn;
wordpair* pItsWordPairs =  new wordpair[100];
std::string ItsLang1,ItsLang2;
};

dictionary::dictionary(){}
dictionary::~dictionary(){
delete [] pItsWordPairs;
}
dictionary::dictionary(const dictionary &){}

void dictionary::AddWordpair(wordpair wordpair){
pItsWordPairs[Itsn++] = wordpair;
}

void dictionary::AddWordpair(std::string word1, std::string word2){
wordpair wordpair(word1,word2);
pItsWordPairs[Itsn++] = wordpair;
}
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.

Offline bart85

  • Lid
Re: Segmentation Fault in c++ code.
« Reactie #1 Gepost op: 2018/02/28, 15:17:14 »
Het probleem is al opgelost. Een klein foutje van mij. Itsn moet op 0 gezet worden bij de constructor van de class.
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.