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;
}
Related
I am sorry if this is really basic but I just started and am extremely confused. I am trying to find the length of a string s using .length() I have included #include , #include "genlib.h"and #include "simpio.h", but am still getting an error.
#include <iostream>
#include "genlib.h"
#include "simpio.h"
#define MAX_HASH_CODE 10000
int Hash( int maxCode, string s);
#define Multiplier -1664117991L // Multiplier used in Hash function
int Hash(int maxCode, string s)
{
unsigned long hashcode = 0;
for (int i = 0; i < s.length(); i++)
hashcode = hashcode * Multiplier + s[i];
return (hashcode % maxCode);
}
int main ()
{
std::cout << "Please enter your name: ";
string name = GetLine();
int hashcode = Hash(MAX_HASH_CODE, name);
std::cout << " The hash code for your name is " << hashcode << "." <<std::endl;
return 0;
}
s.length() just gives an error and says:
request for member 'length' in 's', which is of non-class type 'string' {aka 'char*'}gcc
image of error
By looking at your error message, it looks like you're using the Visual Code IDE, not Visual Studio.
The reason why you are experiencing the error is because it does not recognize s as a string because you have not defined it as such. Even though you declared s to be of type string, it is not the std string. The data type string is only available if you import the <string> library with #include <string>; however, in C++ all standard libraries are including inside a namespace, std. To access the functions like cout and string inside of the std just include using namespace std; at the top like an include statement. However, note that by doing this any methods you declare with the same name as a function inside the std namespace will create a name conflict. You can instead just call the function by the namespace prefix std::, to which you are using by std::cout.
To fix your problem either import the <string> library, include the std namespace, or every time you want to use a string, use std::string s to clarify that you want the std string.
Method 1 (Only works in C, not C++):
#include <string>
string s;
int length = s.length()
Method 2:
using namespace std;
string s;
int length = s.length()
Method 3:
std::string s;
int length = s.length()
Also, a similar question like this has been posted here:
Why am I getting string does not name a type Error?
So I recently discovered the use of map and vectors, however, I'm having trouble of trying to figure a way to loop through a vector containing strings.
Here's what I've tried:
#include <string>
#include <vector>
#include <stdio>
using namespace std;
void main() {
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t) {
cout<<*t<<endl;
}
}
and when I try to compile it, I get this error:
cd C:\Users\Jason\Desktop\EXB\Win32
wmake -f C:\Users\Jason\Desktop\EXB\Win32\exbint.mk -h -e
wpp386 ..\Source\exbint.cpp -i="C:\WATCOM/h;C:\WATCOM/h/nt" -w4 -e25 -zq -od -d2 -6r -bt=nt -fo=.obj -mf -xs -xr
..\Source\exbint.cpp(59): Error! E157: col(21) left expression must be integral
..\Source\exbint.cpp(59): Note! N717: col(21) left operand type is 'std::ostream watcall (lvalue)'
..\Source\exbint.cpp(59): Note! N718: col(21) right operand type is 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> (lvalue)'
Error(E42): Last command making (C:\Users\Jason\Desktop\EXB\Win32\exbint.obj) returned a bad status
Error(E02): Make execution terminated
Execution complete
I tried the same method using map and it worked. The only difference was I changed the cout line to:
cout<<t->first<<" => "<<t->last<<endl;
Add iostream header file and change stdio to cstdio.
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
int main()
{
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t)
{
cout<<*t<<endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> data = {"Hello World!", "Goodbye World!"};
for (std::vector<std::string>::iterator t = data.begin(); t != data.end(); t++) {
std::cout << *t << std::endl;
}
return 0;
}
Or with C++11 (or higher):
#include <iostream>
#include <vector>
#include <string>
typedef std::vector<std::string> STRVEC;
int main()
{
STRVEC data = {"Hello World!", "Goodbye World!"};
for (auto &s: data) {
std::cout << s << std::endl;
}
return 0;
}
From the Open Watcom V2 Fork-Wiki on the C++ Library Status page:
<string>
Mostly complete. Although there are no I/O operators, all other member functions and string operations are available.
A workaround (besides implementing the << operator) would be asking the string instances for the C string:
for (vector<string>::iterator t = data.begin(); t != data.end(); ++t) {
cout << t->c_str() << endl;
}
This of course only works as long as the strings don't contain zero byte values.
When I compile your code, I get:
40234801.cpp:3:17: fatal error: stdio: No such file or directory
#include <stdio>
^
You clearly have a header called "stdio" in your include path that you haven't shown us.
If you change that line to the standard #include <iostream>, then the only reported error is that you wrote void main() instead of int main(). Fix that, and it will build and run.
In passing, note also that using namespace should be avoided.
I found a solution to my own issue. Instead of using a c_str, I used std::string and switched to using the G++ compiler instead of Open Watcom
Instead of having:
char *someString="Blah blah blah";
I instead replaced it with:
string someString="Blah blah blah";
This way is much more efficient and easier.
I am working with C++ in eclipse CDT and I am trying to convert string to uint64_t by using strtoull but everytime I get below error message -
..\src\HelloTest.cpp:39:42: error: strtoull was not declared in this scope
Below is my C++ example
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str = "1234567";
uint64_t hashing = strtoull(str, 0, 0);
cout << hashing << endl;
}
return 0;
}
Is there anything wrong I am doing?
Why your solution doesn't work has already been pointed out by others. But there hasn't been a good alternative suggested yet.
Try this for C++03 strtoull usage instead:
#include <string>
#include <cstdlib>
int main()
{
std::string str = "1234";
// Using NULL for second parameter makes the call easier,
// but reduces your chances to recover from error. Check
// the docs for details.
unsigned long long ul = std::strtoull( str.c_str(), NULL, 0 );
}
Or, since C++11, do it directly from std::string via stoull (which is just a wrapper for the above, but saves on one include and one function call in your code):
#include <string>
int main()
{
std::string str = "1234";
// See comment above.
unsigned long long ul = std::stoull( str, nullptr, 0 );
}
Never use char[] or pointers if you have a working alternative. The dark side of C++, they are. Quicker, easier, more seductive. If once you start down the dark path, forever will it dominate your destiny, consume you it will. ;-)
the structure for strtoull is: strtoull(const char *, char * *, int)
You have given it a std::string as pointed out by #juanchopanza
This is the solution I came up with is
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char str[] = "1234567";
unsigned long long ul;
char* new_pos;
charDoublePointer = 0;
ul = strtoull(str, &new_pos, 0);
cout << ul << endl;
return 0;
}
The output I got was: 1234567
Straight from the eclipse console.
Also at the end of your program you have return 0 out of scope with an extra curly brace.
I have a basic program that compares two strings :
#include <string>
#include <iostream>
using namespace std;
int main (int argc, char *argv[]) {
if(strcmp (argv[0],"./test") != 0) {
cout << "not equal" << endl;
} else {
cout << "equal" << endl;
}
return 0;
}
it compiles with gcc but not with clang :
> clang -o test test_clang.cpp
test_clang.cpp:7:6: error: use of undeclared identifier 'strcmp'
if(strcmp (argv[0],"./test") != 0) {
^
1 error generated.
Why doesn't it compile with clang ?
EDIT: People are getting harsh on stack overflow, up to the point that I am hesitating to post a question. The question above has a simple answer, fine, but is it normal to down-vote questions (twice in the first minute!) because they have a simple, yet non obvious, answer ?
Use
#include <string.h>
or
#include <cstring>
instead of
#include <string>
The string header is for the std::string from C++. string.h is for C zero terminated char* strings. cstring is like string.h but for C++.
The reason it worked with gcc is probably different warning/error level settings. It is possible to compile the code without #including the header and having the declaration of strcmp. The compiler will not be able to do type checking but the symbol still gets resolved by the linker.
You can also avoid using strcmp completely and write
#include <string>
#include <iostream>
int main (int argc, char *argv[]) {
std::string command = argv[0];
if( command != "./test" ) {
std::cout << "not equal" << endl;
} else {
std::cout << "equal" << endl;
}
return 0;
}
Using a std::string on one side of the comparison will cause the "./test" string to be converted into a std::string as well and the comparison will be done by the == operator of the std::string class.
You're not including the correct header file
#include <cstring>
You need to #include <cstring> (or possibly #include <string.h>.)
Many compilers include extra standard headers when you include another. The Standard allows this; it's your responsibility to use the headers that guarantee declarations for what you use, not just headers that happen to have the declarations for your compiler.
You have to include <cstring>. <string> is the header for C++ strings.
I'm having trouble understanding where I went wrong with my code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string str = "";
cin >> str;
remove(str.begin(), str.end(), ' ');
cout << str;
cin.ignore();
}
The error says "'remove': function does not take 3 arguments (C2660)"
Try adding
#include <algorithm>
"algorithm" is an STL header containing a lot of functions, including std::remove, which the OP is trying to call. The error he got was because there is another function that takes a single argument, called "remove", which deletes a file.