I am new to C++ and I am trying to create an object but somehow the compiler does not compile and throws me an exception "Exception thrown at 0x76EBE3DC (ntdll.dll) in projectLinkedList.exe: 0xC0000005: Access violation reading location 0x00829CE8." while popping up the "CreateProcessA.cpp". I have no idea on how to solve this issue. I think there are some errors in my code.
This is my code in .h
#ifndef WORDDATA_h
#define WORDDATA_h
#include "NumList.h"
class WordData
{
private:
char* wordData;
int wordLength;
int frequency;
//declare a numlist object
NumList numList;
//print function
friend std::ostream& operator<< (std::ostream&, const WordData&);
public:
//default constructor
WordData();
//parameterized constructor
WordData(std::string word, int linenumber);
//destructor
~WordData();
//copy constructor
WordData(const WordData&);
//copy assignment operator
WordData& operator= (const WordData&);
//setter
void addFrequency();
//getter
int getNumListFElement() const;
bool searchLineNum(int) const;
void appendLineNum(int);
int frequencyCount() const;
//read-only pointer to the stored word
const char * getPointerWordData() const;
//read-only reference to the numlist object
const NumList getReferenceNumList();
//compare 2 words
int compare(const WordData&) const;
};
#endif
This is my code in .cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "WordData.h"
using namespace std;
//Default Constructor
WordData::WordData()
{
wordData = nullptr;
wordLength = 0;
frequency = 0;
NumList numList;
}
WordData::WordData(std::string word, int lineNumber)
{
//Covert the string to lowercase
for (size_t i = 0; i < word.length(); ++i) {
if ('A' <= word[i] && word[i] <= 'Z') {
word[i] = char(((int)word[i]) + 32);
}
}
wordData = new char[word.length()+1];
//Set a dumpy max char of 50
strcpy_s(wordData, 50, word.c_str());
wordLength = sizeof(wordData) / sizeof(*wordData);
frequency = 1;
NumList numList;
this->numList.insertEnd(lineNumber);
}
//Destructor
WordData::~WordData()
{
}
//Copy Constructor
WordData::WordData(const WordData& anotherWord)
{
wordLength = strlen(anotherWord.wordData);
frequency = anotherWord.frequency;
numList = anotherWord.numList;
wordData = new char[wordLength+1]; //+1 for \0 at the end
//for (int i = 0; i < wordLength; ++i)
//{
// wordData[i] = anotherWord.wordData[i];
//}
strcpy_s(wordData, 50, anotherWord.wordData);
}
//Copy Assignment Operator
WordData& WordData::operator= (const WordData& anotherWord)
{
//Calling copy constructor
WordData temp(anotherWord);
//Exchanging the content of WordData
std::swap(wordData, temp.wordData);
std::swap(wordLength, temp.wordLength);
std::swap(frequency, temp.frequency);
std::swap(numList, temp.numList);
return *this;
}
//Setter
void WordData::addFrequency()
{
frequency += 1;
}
//Getter
int WordData::getNumListFElement() const
{
return numList.getElement(0);
}
bool WordData::searchLineNum(int lineNum) const
{
if(this->numList.search(lineNum))
return true;
else
return false;
}
void WordData::appendLineNum(int lineNum)
{
if (!(this->numList.search(lineNum)))
this->numList.insertEnd(lineNum);
else
cout << "The line number already exists in the list." << "\n";
}
int WordData::frequencyCount() const
{
return frequency;
}
//Read-only pointer to the wordData
const char* WordData::getPointerWordData() const
{
return wordData;
}
//Read-only reference to the numList
const NumList WordData::getReferenceNumList()
{
NumList numList;
return numList;
}
//Compare 2 words
int WordData::compare(const WordData& anotherWord) const
{
return (strcmp(anotherWord.wordData, wordData));
}
std::ostream& operator<< (std::ostream& out, WordData& word)
{
out << "The word of " << word.getPointerWordData() << " has a frequency of " << word.frequencyCount() << "\n" << "It appears on lines ";
word.getReferenceNumList().print();
return out;
}
This is my code in main
#include <iostream>
#include <string>
#include "WordData.h"
int main()
{
cout << "Hello" << endl;
WordData data1("hello" , 10);
data1.getPointerWordData();
system("pause");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire