1

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

MMM DDD
  • 13
  • 2
  • "Visual Studio 2020" isn't a thing. Please clarify the exact version and edition of Visual Studio you're using. – Dai Jun 23 '20 at 03:16
  • 2
    Do you mean to use `std::string`? You need to `#include` for that, and qualify `string` with `std::`. – cigien Jun 23 '20 at 03:16
  • Are you using GCC with Visual Studio? Why are you using `#define` instead of a `constexpr`? You don't need to forward-declare your `int Hash` function in this case. What is `genlib.h` and `simpio.h`? Those are not standard headers. – Dai Jun 23 '20 at 03:17
  • Sorry you are right it not 2020 its called Visual Studio Code. I got this code from a stanford class that uses genlib.h and simpio.h. This is their starter code that is supposed to have a few errors but I don't know how to fix this one. I added std::string to everywhere where I defined a string but now it says it has zero tasks but won't build the exe. it says this: – MMM DDD Jun 23 '20 at 03:30
  • I added std::string to everywhere where I defined a string but now it says it has zero tasks but won't build the exe. it says this: C:\Users\Me\AppData\Local\Temp\ccIK2bW6.o: In function `main': c:/Users/Me/cProjects/Main/warmup.cpp:45: undefined reference to `GetLine()' collect2.exe: error: ld returned 1 exit status The terminal process terminated with exit code: 1 Terminal will be reused by tasks, press any key to close it. – MMM DDD Jun 23 '20 at 03:36
  • Did you try compiling? Sometimes Intellisense if off. –  Jun 23 '20 at 04:23
  • Long ints are only 32 bits on some compilers. Consider using a known type like `int64_t` for portability – doug Jun 23 '20 at 04:24

1 Answers1

0

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?

notMyName
  • 690
  • 2
  • 6
  • 17
  • Yo thank you so much this worked perfectly and you explained it very easily. – MMM DDD Jun 23 '20 at 03:40
  • @MMMDDD Read it again, as I changed it up – notMyName Jun 23 '20 at 03:43
  • Also accepting the answer would be much appreciated – notMyName Jun 23 '20 at 03:45
  • No problem. But if you have the time also I use std::string name = GetLine(); and it doesn't seem to like this as it says c:/Users/Me/cProjects/Main/warmup.cpp:45: undefined reference to `GetLine()' – MMM DDD Jun 23 '20 at 03:46
  • I think I just going to use cin >> – MMM DDD Jun 23 '20 at 04:21
  • 2
    Are you sure that's the [right string header](https://stackoverflow.com/q/9257665/10957435)? –  Jun 23 '20 at 04:26
  • 1
    I agree it should be #include not – Steve Kidd Jun 23 '20 at 05:53
  • And you need the #include regardless of whether you use 'using' or the qualified name std::string. – Steve Kidd Jun 23 '20 at 05:55
  • @SteveKidd You do not, it runs without importing '', try it out with this online C++ compiler: https://www.onlinegdb.com/online_c++_compiler – notMyName Jun 23 '20 at 15:00
  • @notMyName In this specific example you don't need to add #include BUT only because #include internally includes it for you. Remove iostream and you will see it fail to compile unless you include . #include is needed somewhere in the translation unit so there is a risk that methods 2 and 3 mislead even though they work with this particular code snippet. Note, however that your answer is plain WRONG re Method 1. std::string must be referred to either by using its qualified name or via a using statement. – Steve Kidd Jun 24 '20 at 04:55
  • @notMyName My more general point is that if you use some library class, I would automatically think 1) I should #include it's declaration file. 2) I must either use the class's qualified name or use a using namespace. Personally, I would #include in this scenario even though it happens to be included somewhere within . [Aside: there can be exceptions to 1 where you are using a project-wide includes file (and possibly pre-compiled headers) - I am not advocating this but it does happen - even so I know I need the include somewhere]. – Steve Kidd Jun 24 '20 at 05:10