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;
}
Related
In many answers and questions such as this one, it is recommended to use cin.getline from <string> and not getline(cin, variable):
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cin.getline(name);
}
But in my case I have build issue:
g++ foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:8:21: error: no matching function for call to
‘std::basic_istream<char>::getline(std::string&)’
8 | cin.getline(name);
| ^
From the error told by the compiler, it is evident that there is no such function overload that accepts a type reference to std::string (for cin.getline()). Rather, it accepts parameters like:
const int MAX = 100;
char input[MAX];
cin.getline(input, MAX);
// Or better
// cin.getline(input, sizeof input);
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();
}
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, " ");
}
I am new to C++ and am writing a simple program to test out working with strings. I have defined a separate function that returns a string and want to call it within the main method. The same code works within the main method but I need to define it as a separate function.
My code is below:
7 #include <cstdlib>
8 #include <iostream>
9 #include <string>
10
11 using namespace std;
12
13 // function declaration
14 string hi();
15
16 int main(int argc, char** argv) {
17 // call method
18 string hi;
19 hi = hi();
20 cout << hi << endl;
21 }
22
23 string hi() {
24 //simple string function
25 return "Hello World";
26 }
And below is the error returned to the console when I try to compile with g++:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:19:13: error: no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_string<char>}) ()’
hi = hi();
^
You shadow your function declaration with your variable name. You can do 1 of 2 things:
Rename the function or variable. (example)
Qualify the name in the call, this would involve invoking the namespace of the function (ie. the global namespace) like this: ::hi(). (example)
Problem in the Code
You named the variable name and the function name, both hi, so the compiler gets confused about which one you mean.
Solution 1
As #Cheersandhth said, do this:
hi = ::hi();
to get the global hi in its global namespace, which is the function.
Live Example
Solution 2
Otherwise, if you want to change the variable name, you could either change the function name, or the variable, to a different value, and then it would work. Here is an example:
int main(int argc, char** argv)
{
string l_hi;
l_hi = hi();
cout << l_hi;
}
Or the alternative:
int main(int argc, char** argv)
{
string hi;
hi = hello();
cout << hi;
}
string hello()
{
return "Hello, World!";
}
Both of these solutions would work, the one you want to use would be a matter of preference. Also, use this answer as advice to not do name shadowing, which could either lead the compiler into picking one of the two names, (provided they fit the right context), or an error may be thrown.
Solution 3
Wait, there's a third solution?
Yes, but solution three is more of a workaround, although I prefer it. Just get rid of the variable itself, and cout like this:
cout << hi(); //So simple!
I believe the error message is rather clear: the hi in main hides the global hi-function. Use another name somewhere or scope the function: hi = hi() => hi = ::hi();
Try
string hi();
int main(int argc, char** argv) {
string _hi;
_hi = hi();
cout << hi << endl;
}
string hi() {
return "Hello World";
}
The reason the compiler is throwing an error is because it is parsing the parentheses after hi as a function call. But the string class in C++ doesn't have a function call operator member function. So, it gets confused when it sees hi() because it thinks you are talking about the string hi variable you just declared on the previous line. So, to distinguish the hi variable from the hi() function that returns a string, you can do one or both of the following:
You can rename your locally declared string variable to something that doesn't match the global function or vice versa.
You can prefix the function call with the global namespace qualifier to tell the compiler that you are using the global hi() function. (ie. ::hi()).
Example of first:
#include <iostream>
#include <string>
using namespace std;
string hi();
int main(int argc, char** argv) {
string my_hi;
my_hi = hi();
cout << my_hi << endl;
return 0;
}
string hi() {
return "Hello, world!";
}
Example of second:
#include <iostream>
#include <string>
using namespace std;
string hi();
int main(int argc, char** argv) {
string hi;
hi = ::hi();
cout << hi << endl;
return 0;
}
string hi() {
return "Hello, world!";
}
Because classes can overload the function call operator, it can sometimes look like you are calling a normal function, but the compiler may be calling the function call operator. Good naming practices can help minimize these issues, but sometimes you may need to be specific and include the global namespace qualifier to clarify to the compiler what you mean (ie. If the string class had a function call operator defined, there would be no compiler error, but you would not get the answer you wanted).
I was trying to play around with Strings in a Hangman program that I'm writing and couldn't get them to work so tried working with them on a simpler basis and I'm still having no luck.
As far as I've read online in the references and what other people have said this code should work:
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main (int argc, char** argv){
string word = {"Hello"};
int length = strlen(word);
}
But I get this compiler error:
'string' was not declared in this scope
and consequently, 'word' is also not declared in scope.
Can anyone see what I'm doing wrong? I'm using the g++ compiler on Ubuntu if that makes a difference, no idea which version though.
You are confusing C and C++.
You included only C libraries, whereas std::string comes from the C++ header string. You'd have to write:
#include <string>
to use it. However, you'd then have to make other changes, such as not using strlen.
You should learn from your C++ book, not random posts on the internet (#lolirony)
C version
#include <string.h>
int main(void)
{
const char* word = "Hello";
const size_t length = strlen(word); // `size_t` is more appropriate than `int`
return 0;
}
C-like C++ version
#include <cstring>
using namespace std;
int main()
{
const char* word = "Hello";
const size_t length = strlen(word);
}
Idiomatic C++ version (recommended)
#include <string>
int main()
{
const std::string word = "Hello";
const std::size_t length = word.size();
}
'string' was not declared in this scope
You need to include the header <string> and refer to it as std::string. Also, strlen does not understand std::string or any user defined types, but you can use the size() method instead:
#include <string>
int main()
{
std::string word = "Hello";
size_t length = word.size();
}
<cstring> is the header for C++ support of C-style null-terminated strings. You should include <string>.
You haven't included the C++ string header in your project.
#include <string>
The libraries that you've included are all plain-C headers.
Additionally, strlen() doesn't work with a c++ string; you should use word.size() instead.
string is a specialization of standard class std::basic_string . It is declared in header <string>
So if you want "to play around with standard class std::string:" you need to include directive
#include <string>
Header <cstring> is not the same as header <string> and contains declarations of standard C functions such as strlen.
However there is no any sense to apply function strlen to an object of type std::string The compiler in this case will issue an error.
I advice you to play with the following code that to see the difference
#include <iostream>
#include <string>
#include <cstring>
int main (int argc, char** argv)
{
std::string word = "Hello";
std::string::size_type length = word.length();
std::cout << "Object word of type std::string has value "
<< word << " with length of " << length
<< std::endl;
std::cout << "The size of the object itself is " << sizeof( word ) << std::endl;
char another_word[] = "Hello";
size_t another_length = std::strlen( another_word );
std::cout << "Object another_word of type char [6] has value "
<< another_word << " with length of " << another_length
<< std::endl;
std::cout << "The size of the object itself is " << sizeof( another_word ) << std::endl;
}