0

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;
 }
Andrew
  • 3,501
  • 8
  • 35
  • 54
  • [Compiles for me.](http://ideone.com/XsY2sF) –  Aug 15 '13 at 18:27
  • Should compile fine with included: http://ideone.com/dkAVsG – Oliver Charlesworth Aug 15 '13 at 18:27
  • Compiles for me on VS2010 – Itsik Aug 15 '13 at 18:27
  • Including `` should fix it (and does for me). But why are you messing around with dangerous C-style string manipulation in C++? – Mike Seymour Aug 15 '13 at 18:27
  • @H2C03, I'm running this in Eclipse using g++. I tried including the library, but I still received the same error. – Andrew Aug 15 '13 at 18:29
  • 1
    Also, try to actually compile it. It's maybe just that the static analyzer in Eclipse is being dumb, but the compiler will run just fine (as it should). –  Aug 15 '13 at 18:29
  • 1
    "Big C++"? Never heard of that language. – Daniel Kamil Kozar Aug 15 '13 at 18:32
  • @H2C03, I ran it in a different text editor and compiled it manually. It works now. Seems Eclipse just has a couple of issues... – Andrew Aug 15 '13 at 18:32
  • Show us the actual source file that exhibits the problem. The code in your question doesn't have `#include `. Add `#include `, verify that the compiler complains, and copy-and-paste your exact source file, plus the exact error message, into your question. – Keith Thompson Aug 15 '13 at 18:33
  • @DanielKamilKozar, my apologies, I was referencing this book: http://www.amazon.com/Big-C-Cay-S-Horstmann/dp/0471470635 – Andrew Aug 15 '13 at 18:33
  • @DanielKamilKozar I believe its a book, though never heard of it either – SwiftMango Aug 15 '13 at 18:33
  • Just a note - void append(char s[], int s_maxlength, const char t[]) is bad practice - passing arrays using [] syntax is syntactically strange, and invokes some strange corners of the C++ language that many are not familiar with. If you must pass raw arrays around, the correct syntax is char*. If your book uses char name[] as function arguments, I strongly recommend getting a better book. – Stewart Aug 15 '13 at 18:37
  • @Stewart, gotcha. I think I'll toss this book aside and go with a different one – Andrew Aug 15 '13 at 18:42
  • Have a look at this list for recommendations: http://stackoverflow.com/q/388242/1025391 – moooeeeep Aug 15 '13 at 18:43
  • That it compiles for one compiler does not actually prove anything regarding other compilers in the case of missing headers. – user2672165 Aug 15 '13 at 19:26

1 Answers1

4

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.