0
$\begingroup$

I write under code but I can't run it when I run by g++ . erro is about strlen.
errors:

a.cpp: In function ‘bool checker(char*, int)’:
a.cpp:185:26: error: ‘str’ is not a member of ‘std’
 185 |  for(int i = 0; i < std::str.length(word); i++)
  |                          ^~~
a.cpp: In function ‘int status(char*, int, int)’:
a.cpp:192:40: error: ‘str’ is not a member of ‘std’
   192 |  for(int i = 0; i < 4 && nw*4+i < std::str.length(pmt[pn]); i++)
  |                                        ^~~
a.cpp:199:10: error: ‘str’ is not a member of ‘std’
  199 |  if(std::str.length(pmt[pn]) > nw*4)
  |          ^~~
a.cpp: In function ‘int main()’:
a.cpp:225:22: error: ‘str’ is not a member of ‘std’
  225 |     len_words = std::str.length(pmt[i])/2;
    |                      ^~~
$\endgroup$
7
  • 1
    $\begingroup$ Isn't this a better question for stackoverflow? $\endgroup$
    – Devon Ryan
    Aug 23, 2019 at 13:07
  • $\begingroup$ how i can ask best question $\endgroup$ Aug 23, 2019 at 13:09
  • $\begingroup$ @fatemehkhoramdel please format your question properly, check your English to make it easier to understand (capitalize the beginning of sentences, ...). Explain what this code it is supposed to do and why it is relevant that you need bioinformaticians to help you (there are other sites to get help that might have people with the right skills to help you, but they might not be here). Good luck $\endgroup$
    – llrs
    Aug 23, 2019 at 15:04
  • $\begingroup$ I think this is what you're looking for: error strlen not a member of std only on linux not on solaris. $\endgroup$
    – terdon
    Aug 23, 2019 at 16:25
  • 5
    $\begingroup$ I'm voting to close this question as off-topic because it belongs on Stack Overflow. $\endgroup$ Aug 23, 2019 at 21:36

1 Answer 1

1
$\begingroup$

I think you're confusing C++ and C strings, which are very different. When you have a char *, that's a C-style "string". It's really just a bunch of bytes with a zero byte at the end, demarcating the end of the string. To find the length of a C string you include <cstring> (or <string.h> in C) and call strlen, like this:

/** Print every character in the string */
void print_chars_in_string(char* word) {
    for (size_t i = 0; i < strlen(word); ++i) {
        putchar(word[i]);
    }
}

strlen is actually pretty simple though, you can write your own version as an academic exercise like this:

size_t deluxe_bespoke_strlen(char* str) {
    size_t length = 0;

    while (*str++)
        ++length;

    return length;
}

Since the while-loop condition will be true until you hit the null terminator (the zero byte at the end), this is how you would implement the strlen function without a for-loop, otherwise you would need the strlen function for the strlen function.

In C++, strings are actual objects, with methods you can call. To get the length of a C++ string simply do this:

std::string my_name = "Jose";
std::cout << my_name.length() << std::endl;

If you have a C string, you can convert it to a C++ string with the built-in constructor:

const char* my_name = "Jose";
std::string my_name_cppstring = std::string(my_name);

You can also get the C version of a C++ string by calling the c_str() member function like this:

std::string sequence = "GATTACA";
size_t length = strlen(sequence.c_str());

My recommendation is to not mix C and C++. C is my favorite programming language, but that doesn't mean everyone should write everything in C. Since you're using C++ I'm guessing that's your preference, so I would suggest using C++ strings instead of C strings, just remember to include <string>.


As for your specific question, the compiler is giving you the correct message: str is not a member of std. You don't add the namespace to variables. This is how that works:

#include <cstdlib>
#include <iostream>

namespace CustomDeluxeBespokeStandardNamespace
{
    size_t custom_bespoke_strlen(char* str)
    {
        size_t length = 0;

        while (*str++)
            ++length;

        return length;
    }
}

int main(int argc, char *argv[])
{
    if (argc == 1) {
        std::cerr << "No input(s)." << std::endl;
        std::exit(EXIT_FAILURE);
    }

    while (*++argv) {
        std::cout << *argv << ": " << CustomDeluxeBespokeStandardNamespace::custom_bespoke_strlen(*argv) << std::endl;
    }
}

So if you run it like this:

testprogram horse house mouse cat

You should get this output:

horse: 5
house: 5
mouse: 5
cat: 3

So to recap, you were adding the namespace to your variables, and the compiler was giving you an error because it correctly could not find anything by that name in the std namespace. Even if it had though, C style strings do not have member functions. There are no such things as objects in C, at least not in the sense that you can define member functions for an object, so that was the second error.


As an aside, you will note all my examples using strlen both custom and actual return size_t. This is not an accident; in both C and C++ the return type of a call to strlen is size_t. You have to be careful because in your code you compare an int to a size_t. Comparing a signed value to an unsigned value is Not Good because it will work only if the signed value can be faithfully represented after a conversion to an unsigned value. If it can't though, you're in for a bad time.

$\endgroup$
8
  • $\begingroup$ ok, it did, but no results are shown when i create the file $\endgroup$ Aug 26, 2019 at 11:18
  • $\begingroup$ Can you post your code? $\endgroup$ Aug 26, 2019 at 11:23
  • $\begingroup$ my code have 2000 line...i think i can't post code here.. $\endgroup$ Aug 29, 2019 at 5:43
  • $\begingroup$ how i can post it? $\endgroup$ Aug 31, 2019 at 6:52
  • $\begingroup$ Can you link to a github? $\endgroup$ Aug 31, 2019 at 13:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.