Error invalid user define conversion - c++

#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>
using namespace std;
int main(int argc, char* argv[])
{
if(argc != 3)
cout<< "Error! Not enough file!"<<endl;
char** words = new char* [10];
char** page = new char* [10];
string line;
char* key = "<-1>";
ifstream input (argv[1]);
while(strcmp(std::getline(input, line), key) != 0)
{
}
return 0;
}
So when I tried to run this(of course it is not finished). The compiler keeps giving me the error that says
/home/ds/DataStructuresRepo/Project2/untitled/main.cpp:17: error: invalid user-defined conversion from 'std::basic_istream<char>' to 'const char*' [-fpermissive]
while(strcmp(std::getline(input, line), key) != 0)
^
What am I doing wrong?

std::getline returns a std::basic_istream&. It writes what it reads into the line you're passing in. strcmp takes two const char*s. You cannot just pass the result of the first into the second - those types aren't convertible (hence the error about no conversion from std::basic_istream<char> to const char*).
Since you're getting a std::string anyway, you can just use it's operator== directly:
while (std::getline(input, line) && line != key)
{
}

What you're doing wrong is that you're passing an istream (the return value of getline) to strcmp which expects a char*.

Related

Why can't I provide a string argument to printw in ncurses? [duplicate]

This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 3 years ago.
For an application that I'm writing, I have a string type variable that I want to display within an ncurses window:
#include <iostream>
#include <ncurses.h>
#include <string>
int main(){
std::string mystring = "A sample string\n";
// Entering the ncurses window
initscr();
printw(mystring);
getch();
endwin();
}
which throws the following error at compilation:
test_app.cpp: In function ‘int main()’:
test_app.cpp:12:18: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘int printw(const char*, ...)’
printw(mystring);
Where am I going wrong? How can I rectify this?
Some key concepts in c++:
A string literal declaration (aka "this is a string literal") has a type const char[N], where N is the size of the string, including the null terminator.
std::string != const char[]
However, a std::string can be constructed with a const char[] using this constructor (found here):
basic_string( const CharT* s,
const Allocator& alloc = Allocator() );
Where CharT is your implementation specific char equivalent.
Now, notice how printw takes a const char*. You aren't passing a const char * to printw, you're passing a std::string, and they aren't implicitly convertible to a const char *.
We have two options to solve your problem...
1) Store the string as a char[] (aka char *):
#include <iostream>
#include <ncurses.h>
#include <string>
int main(){
char mystring[] = "A sample string\n"; // Can decay to a char * implicitly.
// Entering the ncurses window
initscr();
printw(mystring);
getch();
endwin();
}
2) Get a representation of the std::string as a char *:
#include <iostream>
#include <ncurses.h>
#include <string>
int main(){
std::string mystring = "A sample string\n";
// Entering the ncurses window
initscr();
// Since c++ 11, mystring.data() is required to return a null-terminated char *.
// If c++ version < c++11, use mystring.c_str().
printw(mystring.data());
getch();
endwin();
}

Im trying to recreate the Javascript str.split(delimiter) function into C++ and I dont know how to fix this

I worked a little bit with javascript and I decided to work on a function that will work similar to str.split(), but in C++. The next code I wrote shows me this error:
no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=char, _Alloc=std::allocator]" matches the argument list -- argument types are: (char *) -- object type is: std::vector>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
vector<char> split(char str[], char spliter[]){
unsigned i=0;
vector <char> output;
char *p = strtok(str, spliter);
do{
output.push_back(p);
p = strtok(NULL, spliter);
}while(p != NULL);
return output;
}
int main()
{
char s[51];
strcpy(s, "I have a cake");
split(s, " ");
}

Read non constant c string into string class

I need to read a non-constant C string into a C++ string. However, I only see methods in the string class that read constant C strings into the string class.
Is there anyway to do this in C++?
Update:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string a;
// other code here
a(argv[0]);
cout << a << endl;
return 0;
}
error:
test.cpp: In function 'int main(int, char**)':
test.cpp:11:14: error: no match for call to '(std::string {aka std::basic_string<char>}) (char*&)'
a(argv[0]);
I did some more investigation, and replaced argv[0] with a constant string, and found that I still got a similar error message. A better question would now be: How can I declare an object and call its constructor later?
You're misinterpreting what the function signatures mean. The conversion takes its argument as a const char *, but that doesn't mean that you cannot pass a char* to it. It's just telling you that the function will not modify its input. Why don't you just try it out?
#include <iostream>
#include <string>
int main()
{
char str[] = "hello";
std::string cppstr = str;
std::cout << cppstr << endl;
return 0;
}

c++ getline() function

I dont' quite understand how this function works.
I wrote a simple programming reading one line with getline().
for example:
ifstream in;
in.open("example.txt");
string line;
getline(in, line);
cout << line << endl;
When I tried to run this program I received an error message like this.
`assign1_2.cpp:33:20: error: cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)'
I simply don't understand what went wrong here. Please help!. I am a newbie to c++.
You didn't show the code with the error, but the error says you tried to call atoi with an argument of type std::string. atoi takes a C string (man atoi), so you need to call it like:
atoi( line.c_str() );
Which function are you trying to call? The gnu 'C' getline function or istream::getline?
istream::getline has the following signature
istream& istream::getline( char* str, streamsize count)
istream& istream::getline( char* str, streamsize count, char delim )
So you call should be something like:
char* buf[1000]
in.getline( buf, 1000 );
Change string line to char line[2000]
like so:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char line[2000];
fstream in;
in.open("example.txt",ios::in);
while(!in.eof())
{
in.getline(line,2000);
}
in.close();
cout <<line;
cout <<endl;
return 0;
}

string Comparison

I want to compare two user input strings, but not able to do so...
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv0[])
{
string my_string;
string my_string2;
cout<<"Enter string"<<endl;
cin>>my_string;
cout<<"Enter 2nd string"<<endl;
cin>>my_string2;
cout<<my_string<<" "<<my_string2;
strcmp(my_string,my_string2);
int result;
result= strcmp(my_string,my_string2);
cout<<result<<endl;
return 0;
}
This error is appearing.
Error 1 error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *' c:\users\asad\documents\visual studio 2008\projects\string\string\string.cpp 23 String
Since you're using std::string, strcmp is unnecessary -- you can just use <, ==, !=, etc.
Your includes:
Since you are including standard headers, they should be in <>
#include <string>
#include <iostream>
#include with "" is generally used for your own header files, not standard header files.
You are using C++, and therefore need not use strcmp. In C++, you can simply use == & != to compare two strings.
if (my_string == my_string2) result = 0;
else result = 1;
Also, if you want to convert a string to a const char*, you can use mystring.c_str()
If you want to use strcmp note that it takes different parameters than the ones you used.
http://www.cppreference.com/wiki/c/string/strcmp
Another way to do this is also
result= strcmp(my_string.c_str(),my_string2.c_str());