I'm new to programming in c++ and I've encountered a problem while trying to use the strcmp function. I use Dev c++ version 4.9.9.2. The program is a very simple one, trying to copy lines from one file to another till the occurence of a specific sentence. When trying to compile, it doesnt seem to like the strcmp function even though I've included
include <string>. The program and compile log are shown below
C:\Dev-Cpp\trials\Untitled3.cpp:26: error: no matching function for call to `strcmp(std::string&, std::string&)'
C:/Dev-Cpp/include/string.h:43: note: candidates are: int strcmp(const char, const char)
two lines above are what you need to look at.
They point out that first "no matching function for call to `strcmp(std::string&, std::string&)"
and that the valid "candidates are: int strcmp(const char, const char)"
Use the function "c_str()" to convert your strings to character arrays.
strcmp(t.c_str(),s.c_str())
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I'm new to programming in c++ and I've encountered a problem while trying to use the strcmp function. I use Dev c++ version 4.9.9.2. The program is a very simple one, trying to copy lines from one file to another till the occurence of a specific sentence. When trying to compile, it doesnt seem to like the strcmp function even though I've included
include <string>. The program and compile log are shown below
include <iostream>
include <string>
include <fstream>
include <stdlib.h>
using namespace std;
int main()
{
fin.close();
fout.close();
system("pause");
}
The compile log is as follows:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Dev-Cpp\trials\Untitled3.cpp" -o "C:\Dev-Cpp\trials\Untitled3.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Dev-Cpp\trials\Untitled3.cpp: In function
int main()': C:\Dev-Cpp\trials\Untitled3.cpp:26: error: no matching function for call to
strcmp(std::string&, std::string&)'C:/Dev-Cpp/include/string.h:43: note: candidates are: int strcmp(const char, const char)
Execution terminated
Any idea what I'm doing wrong ? This could be a total amateur mistake. Thanks a ton in advance.
Cheers
Pramod
strcmp() is for comparing char arrays. For cstring's you could just use the == operator like on integers, floats, etc.
C:\Dev-Cpp\trials\Untitled3.cpp:26: error: no matching function for call to `strcmp(std::string&, std::string&)'
C:/Dev-Cpp/include/string.h:43: note: candidates are: int strcmp(const char, const char)
two lines above are what you need to look at.
They point out that first "no matching function for call to `strcmp(std::string&, std::string&)"
and that the valid "candidates are: int strcmp(const char, const char)"
Use the function "c_str()" to convert your strings to character arrays.
strcmp(t.c_str(),s.c_str())