#include "WordCount.h"
void WordCount::fileOpen(string fileName)
{
*file = new ifstream(fileName.c_str());// this is giving the error
file->open(fileName);
}
void WordCount::fileClose()
{
file->close();
delete *file;
}
int WordCount::countWords(string wrd)
{
string fileInput;
int count = 0;
int posi = 0;
for(int i=0; i < wrd.length(); i++)
{
wrd[i] = toupper(wrd[i]);
}
wrd = wrd + " ";
if (file->is_open())
{
while(!file->eof())
{
getline(*file, fileInput);
for(int i=0; i<fileInput.length(); i++)
{
fileInput[i] = toupper(fileInput[i]);
}
while (fileInput.find(wrd) != string::npos)
{
count++;
posi += fileInput.find(wrd) + 1;
}
posi = 0;
}
}
}
Any help is appreciated.
Edit: here is the error im getting when trying to compile:
g++ -c WordCount.cpp
WordCount.cpp: In member function ‘void WordCount::fileOpen(std::string)’:
WordCount.cpp:5:40: error: no match for ‘operator=’ in ‘*((WordCount*)this)->WordCount::file = (fileName.std::basic_string<_CharT, _Traits, _Alloc>::c_str<char, std::char_traits<char>, std::allocator<char> >(), (operator new(520ul), (<statement>, ((std::ifstream*)<anonymous>))))’
WordCount.cpp:5:40: note: candidate is:
In file included from WordCount.h:6:0,
from WordCount.cpp:1:
/usr/include/c++/4.7/fstream:420:11: note: std::basic_ifstream<char>& std::basic_ifstream<char>::operator=(const std::basic_ifstream<char>&)
/usr/include/c++/4.7/fstream:420:11: note: no known conversion for argument 1 from ‘std::ifstream* {aka std::basic_ifstream<char>*}’ to ‘const std::basic_ifstream<char>&’
WordCount.cpp:6:22: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
WordCount.cpp:6:22: note: candidate is:
In file included from WordCount.h:6:0,
from WordCount.cpp:1:
/usr/include/c++/4.7/fstream:531:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
/usr/include/c++/4.7/fstream:531:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
WordCount.cpp: In member function ‘void WordCount::fileClose()’:
WordCount.cpp:12:11: warning: deleting ‘void*’ is undefined [enabled by default]
make: *** [WordCount.o] Error 1
g++ -c WordCount.cpp
WordCount.cpp: In member function ‘void WordCount::fileOpen(std::string)’:
WordCount.cpp:6:22: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
WordCount.cpp:6:22: note: candidate is:
In file included from WordCount.h:6:0,
from WordCount.cpp:1:
/usr/include/c++/4.7/fstream:531:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
/usr/include/c++/4.7/fstream:531:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
WordCount.cpp: In member function ‘void WordCount::fileClose()’:
WordCount.cpp:12:11: warning: deleting ‘void*’ is undefined [enabled by default]
make: *** [WordCount.o] Error 1
And I assume it needs the * because file is a pointer.
Edit: Nevermind that did fix it because it see the error is now on line 6 not 5.
because when you call new ifstream(fileName.c_str()); it opens the file there (you are calling the ifstream constructor, which if passed a string, treats it as a file path and opens it). -> opens it a second time