[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Unresolved external

Clint

11/30/2008 3:00:00 PM

I am trying to build a program written by a colleague and get an
'unresolved external' error when I run the Borland make utility
(offending function below). Any ideas what's wrong? I am not a c++
programmer so a simple solution would be very welcome.

/*
* strtoko.cpp
*
* Does exactly the same as the standard function "strtok", but
* also rebuilds the original string as it goes, but storing the
* separator which was overwritten with an end-of-string and
restoring
* it on the subsequent entry.
*
* Returns a pointer to the next symbol found, or NULL if end of
string.
*
* s i-o : string to be scanned.
* ct in : string containing all valid separators.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *strtoko( char *s, const char *ct )

{ static char *localp, hold;
char *token;

if ( s != NULL )
localp = s;
else
{ if ( localp == NULL )
{ fprintf( stderr, "\nError in 'strtoko' : Called with NULL
prior " );
fprintf( stderr, "to a call with string for scanning.\n"
);
exit(1);
}
if ( hold == '\0' ) return( NULL );
*localp = hold;
localp++;
}

localp += strspn( localp, ct ); /* skip over any separator
characters */
if ( localp == '\0' ) return( NULL );
token = localp;
localp += strcspn( localp, ct ); /* skip to next separator (or
end-string)*/
hold = *localp;
*localp = '\0';
return( token );
}
3 Answers

Obnoxious User

11/30/2008 3:50:00 PM

0

On Sun, 30 Nov 2008 15:00:07 +0000, Kenny M wrote:

> I am trying to build a program written by a colleague and get an
> 'unresolved external' error when I run the Borland make utility
> (offending function below). Any ideas what's wrong? I am not a c++
> programmer so a simple solution would be very welcome.
>
> /*
> * strtoko.cpp
> *
> * Does exactly the same as the standard function "strtok", but *
> also rebuilds the original string as it goes, but storing the *
> separator which was overwritten with an end-of-string and restoring
> * it on the subsequent entry.
> *
> * Returns a pointer to the next symbol found, or NULL if end of
> string.
> *
> * s i-o : string to be scanned. * ct in : string containing
> all valid separators. */
>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
>
> char *strtoko( char *s, const char *ct )
>
> { static char *localp, hold;
> char *token;
>
> if ( s != NULL )
> localp = s;
> else
> { if ( localp == NULL )
> { fprintf( stderr, "\nError in 'strtoko' : Called with NULL
> prior " );
> fprintf( stderr, "to a call with string for scanning.\n"
> );
> exit(1);
> }
> if ( hold == '\0' ) return( NULL );
> *localp = hold;
> localp++;
> }
>
> localp += strspn( localp, ct ); /* skip over any separator
> characters */
> if ( localp == '\0' ) return( NULL ); token = localp;
> localp += strcspn( localp, ct ); /* skip to next separator (or
> end-string)*/
> hold = *localp;
> *localp = '\0';
> return( token );
> }

Do you link strtoko.o?

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

Thomas J. Gritzan

11/30/2008 6:31:00 PM

0

Kenny M schrieb:
> I am trying to build a program written by a colleague and get an
> 'unresolved external' error when I run the Borland make utility
> (offending function below). Any ideas what's wrong? I am not a c++
> programmer so a simple solution would be very welcome.

First, this is not a program, it is a function. You would have to
provide a main function that calls the function.
The simplest solution would be to ask your colleague for a main function.

Second, the code compiles with a C compiler, so it actually is C code.
It is C++ code too, but in modern C++ you normally want to use
std::string and friends.

> /*
> * strtoko.cpp
> *
> * Does exactly the same as the standard function "strtok", but
> * also rebuilds the original string as it goes, but storing the
> * separator which was overwritten with an end-of-string and
> restoring
> * it on the subsequent entry.
[...]

> char *strtoko( char *s, const char *ct )
>
> { static char *localp, hold;
> char *token;

The function is not reentrant. Since this function is supposed to be a
better strtok, I would at least fix that. Of course the interface
(function signature) would need to be changed for this.

> if ( s != NULL )
> localp = s;
> else
> { if ( localp == NULL )
> { fprintf( stderr, "\nError in 'strtoko' : Called with NULL
> prior " );
> fprintf( stderr, "to a call with string for scanning.\n"
> );
> exit(1);

I would ASSERT here (in debug mode), and return NULL otherwise. It is a
programming/logic error, and a library function shouldn't exit() a program.

> if ( hold == '\0' ) return( NULL );
> *localp = hold;
> localp++;
> }
>
> localp += strspn( localp, ct ); /* skip over any separator
> characters */
> if ( localp == '\0' ) return( NULL );

This condition is never true. It should be:

if ( *localp == '\0' ) return( NULL );

Otherwise, it won't find the end of the string sometimes.

> token = localp;
> localp += strcspn( localp, ct ); /* skip to next separator (or
> end-string)*/
> hold = *localp;
> *localp = '\0';
> return( token );
> }

--
Thomas

Clint

12/1/2008 11:00:00 AM

0

Thanks for pointing this out, Thomas. I have replaced his own strtoko
function with the strtok function in string.h and it seems to compile.

On Sun, 30 Nov 2008 19:30:49 +0100, "Thomas J. Gritzan"
<phygon_antispam@gmx.de> wrote:

>Kenny M schrieb:
>> I am trying to build a program written by a colleague and get an
>> 'unresolved external' error when I run the Borland make utility
>> (offending function below). Any ideas what's wrong? I am not a c++
>> programmer so a simple solution would be very welcome.
>
>First, this is not a program, it is a function. You would have to
>provide a main function that calls the function.
>The simplest solution would be to ask your colleague for a main function.
>
>Second, the code compiles with a C compiler, so it actually is C code.
>It is C++ code too, but in modern C++ you normally want to use
>std::string and friends.
>
>> /*
>> * strtoko.cpp
>> *
>> * Does exactly the same as the standard function "strtok", but
>> * also rebuilds the original string as it goes, but storing the
>> * separator which was overwritten with an end-of-string and
>> restoring
>> * it on the subsequent entry.
>[...]
>
>> char *strtoko( char *s, const char *ct )
>>
>> { static char *localp, hold;
>> char *token;
>
>The function is not reentrant. Since this function is supposed to be a
>better strtok, I would at least fix that. Of course the interface
>(function signature) would need to be changed for this.
>
>> if ( s != NULL )
>> localp = s;
>> else
>> { if ( localp == NULL )
>> { fprintf( stderr, "\nError in 'strtoko' : Called with NULL
>> prior " );
>> fprintf( stderr, "to a call with string for scanning.\n"
>> );
>> exit(1);
>
>I would ASSERT here (in debug mode), and return NULL otherwise. It is a
>programming/logic error, and a library function shouldn't exit() a program.
>
>> if ( hold == '\0' ) return( NULL );
>> *localp = hold;
>> localp++;
>> }
>>
>> localp += strspn( localp, ct ); /* skip over any separator
>> characters */
>> if ( localp == '\0' ) return( NULL );
>
>This condition is never true. It should be:
>
>if ( *localp == '\0' ) return( NULL );
>
>Otherwise, it won't find the end of the string sometimes.
>
>> token = localp;
>> localp += strcspn( localp, ct ); /* skip to next separator (or
>> end-string)*/
>> hold = *localp;
>> *localp = '\0';
>> return( token );
>> }