I have a program where just having cout statements works, however, adding in a class and calling its constructor completely breaks the program. I need help figuring out what the issue is.
This code produces no output:
#include <iostream>
#include <string>
using namespace std;
#define WORDCOUNTER_DATA_LENGTH 262144
struct WordCounterDataPair{
string Word;
unsigned int Count;
unsigned int Position;
};
class WordCounter{
public:
WordCounter(){
cout << "constructor\n";
for(unsigned int i = 0; i < WORDCOUNTER_DATA_LENGTH; i++){
Words[i] = "";
Counts[i] = 0;
}
};
WordCounter(string words[WORDCOUNTER_DATA_LENGTH], unsigned int counts[WORDCOUNTER_DATA_LENGTH]){
for(unsigned int i = 0; i < WORDCOUNTER_DATA_LENGTH; i++){
Words[i] = words[i];
Counts[i] = counts[i];
}
};
void IncrementWord(string word){
WordCounterDataPair search = ValueOf(word);
if(search.Count == 0){
Words[search.Position] = word;
Counts[search.Position] = 1;
Length++;
}else{
Counts[search.Position]++;
}
};
WordCounterDataPair at(unsigned int value){
WordCounterDataPair res;
res.Word = Words[value];
res.Count = Counts[value];
res.Position = value;
return res;
};
protected:
string Words[WORDCOUNTER_DATA_LENGTH];
unsigned int Counts[WORDCOUNTER_DATA_LENGTH];
unsigned int Length = 0;
WordCounterDataPair ValueOf(string word){
for(unsigned int i = 0; i < Length; i++){
if(Words[i] == word){
WordCounterDataPair res;
res.Word = word;
res.Count = Counts[i];
res.Position = i;
return res;
}
}
WordCounterDataPair res;
res.Word = "";
res.Count = 0;
res.Position = Length;
return res;
};
};
int main(){
cout << "before\n";
WordCounter WCTest;
cout << "after\n";
return 0;
}
And this code
#include <iostream>
#include <string>
using namespace std;
#define WORDCOUNTER_DATA_LENGTH 262144
struct WordCounterDataPair{
string Word;
unsigned int Count;
unsigned int Position;
};
class WordCounter{
public:
WordCounter(){
cout << "constructor\n";
for(unsigned int i = 0; i < WORDCOUNTER_DATA_LENGTH; i++){
Words[i] = "";
Counts[i] = 0;
}
};
WordCounter(string words[WORDCOUNTER_DATA_LENGTH], unsigned int counts[WORDCOUNTER_DATA_LENGTH]){
for(unsigned int i = 0; i < WORDCOUNTER_DATA_LENGTH; i++){
Words[i] = words[i];
Counts[i] = counts[i];
}
};
void IncrementWord(string word){
WordCounterDataPair search = ValueOf(word);
if(search.Count == 0){
Words[search.Position] = word;
Counts[search.Position] = 1;
Length++;
}else{
Counts[search.Position]++;
}
};
WordCounterDataPair at(unsigned int value){
WordCounterDataPair res;
res.Word = Words[value];
res.Count = Counts[value];
res.Position = value;
return res;
};
protected:
string Words[WORDCOUNTER_DATA_LENGTH];
unsigned int Counts[WORDCOUNTER_DATA_LENGTH];
unsigned int Length = 0;
WordCounterDataPair ValueOf(string word){
for(unsigned int i = 0; i < Length; i++){
if(Words[i] == word){
WordCounterDataPair res;
res.Word = word;
res.Count = Counts[i];
res.Position = i;
return res;
}
}
WordCounterDataPair res;
res.Word = "";
res.Count = 0;
res.Position = Length;
return res;
};
};
int main(){
cout << "before\n";
cout << "after\n";
return 0;
}
produces the output:
before
after
What is going on here? How can a class constructor even cause this? I am using g++ to compile, and I'm compiling with g++ main.cpp -o program.exe
. The compiler successfully creates a program for both files above and generates no errors or even warnings.
Thanks for your help in advance.