Andrew
Andrew

Reputation: 3589

C++ strlen error in Big C++

I've been working through some of the programs in Big C++ and after I copied append.cpp, Eclipse was telling me 'strlen' was not declared in this scope on line 8. I took a look online, and I thought it was because I had to include the <cstring> library, but that didn't solve it. What's wrong?

append.cpp:

#include <iostream>    

using namespace std;

void append(char s[], int s_maxlength, const char t[])
{
    int i = strlen(s); // error occurs here
    int j = 0;
    while(t[j] != '\0' && i < s_maxlength)
    {
         s[i] = t[j];
         i++;
         j++; 
    }
    //Add zero terminator
    s[i] = '\0';
 }

int main()
{
    const int GREETING_MAXLENGTH = 10;
    char greeting[GREETING_MAXLENGTH + 1] = "Hello";
    char t[] = ", World!";
    append(greeting, GREETING_MAXLENGTH, t);
    cout << greeting << "\n";
    return 0;
 }

Upvotes: 0

Views: 478

Answers (1)

user529758
user529758

Reputation:

Including the <cstring> header should solve (should have solved) the issue. My suspicion was correct: it was only Eclipse being dumb, it gave you a false positive.

In cases like this, don't believe the IDE! Always try to compile the source text - if the compiler accepts it, then the static analysis tool in the IDE was wrong.

Upvotes: 4

Related Questions