error: no match for 'operator='

Hello I recently came across this error in a function for one of my classes and I can't figure out how to fix it. This is the code:

Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef WORDCOUNT_H
#define WORDCOUNT_H

# include <iostream>
#include <string>
#include <fstream>
#include <new>
#include <cstring>

using namespace std;

class WordCount
{
	public:
		WordCount();
		void fileOpen(string fileName);
		void fileClose();
		int countWords(string wrd);
		int countWordsCase(string wrd);
	private:
		ifstream* file;
};

#endif 


Cpp File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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
Last edited on
closed account (Dy7SLyTq)
i think you remove * at the beginning
When I do that I get:

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.
Last edited on
*file is of type std::ifstream

new ifstream(fileName.c_str()) returns a std::ifstream*

See the mismatch?
Last edited on
closed account (Dy7SLyTq)
you need to compile with -std=c++11

edit or open with the .c_str(); method
Last edited on
after making the below changes i'm able to compile your code


file = new ifstream(fileName.c_str())

edit: Remove the open call, as the constructor handles it
Last edited on
closed account (Dy7SLyTq)
except you want to remove the second line. its pointless
@DTSCode,
I didn't understand you.. whats is that pointless?
closed account (Dy7SLyTq)
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
1
2
3
4
std::ifstream //don't `using' in a header
   file; //no need for a pointer

file.open(fileName.c_str());
Oh, ok.. Thanks

I have edited it..
Topic archived. No new replies allowed.