fletch254
fletch254

Reputation: 85

C++ String - Out of scope error

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.

Upvotes: 0

Views: 1532

Answers (5)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385104

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();
}

Upvotes: 7

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

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; 
}

Upvotes: 0

Karl Nicoll
Karl Nicoll

Reputation: 16419

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.

Upvotes: 1

juanchopanza
juanchopanza

Reputation: 227390

'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();
}

Upvotes: 4

lrineau
lrineau

Reputation: 6274

<cstring> is the header for C++ support of C-style null-terminated strings. You should include <string>.

Upvotes: 1

Related Questions