last day (15 days later) » 

02:54
I have to warn you: I'm about to eat supper in a minute, so I might disappear :)
Any idea why G++ would fail to find member functions provided by a library when implicitly declared at the top of a source file? allow me to gist this...
perfectly acceptable sir
Can I see a simple example?
gist: 1396858, 2011-11-27 02:55:36Z
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int hash_string(char str_name[]);

int main()
{
	int pass, slen, spos, hash_value;
	char hash_str[9];
	char hash_table[64][9];
	int collisions = 0;

	// init hash table
	for ( pass = 0; pass < 64; pass++)
	{
		hash_table [ pass ][0] = '\0';
	}

	// porudce 64 strings to be hashed
	for ( pass = 0; pass < 64; pass ++)
	{
		// generate random string of 5 to 8 chars
		slen = 5 + rand() % 4;
		spos = 0;
		do
		{
			hash_str[ spos ] = 'A' + rand() % 25;
			spos++;
			slen--;
		} while ( slen != 0 );
		hash_str[ spos ] = '\0';
		
    //display the string and its hash value
    if ( 0 == pass % 4 )
    {
      cout << endl;
    }
    cout << hash_str << ", " ;
		hash_value = hash_string ( hash_str );
    cout << hash_value << "\t";

    // check for collision
    if ( '\0' == hash_table[ hash_value ] [0] )
    {
      strcpy( hash_table[ hash_value ], hash_str );
    } else {
      collisions++;
    }
	
  }
  cout << endl;
  cout << "There were " << collisions < " collisions. " << endl;

  return EXIT_SUCCESS;
}

int hash_string(char str_name[])
{
  int hashval;

  hashval = str_name[ 0 ];
  hashval ^= str_name[ strlen( str_name ) / 2 ];
  hashval += str_name[ strlen( str_name ) - 1 ];
  return ( hashval & 0x3f );
}
its complaining about the strcpy on line 48
and the #include <string>; using namespace std; should have taken care of that i thought....
charles:(Desktop)~/Dropbox/School/IT327/Day8/Lab1 $
↦ g++ hashtable.cpp
hashtable.cpp: In function ‘int main()’:
hashtable.cpp:48:50: error: ‘strcpy’ was not declared in this scope
@GeorgeEdison i changed it from #include <string> to #include <string.h> and life was good again....
O.o i dunno WHY that is, but ooookay
if you have any insight it would be wonderful :)
03:19
Back now.
Sure, I can explain that.
#include <string> includes the C++ string class.
#include <string.h> includes old C string utilities.
<string.h> is the C class correct?
hmm.. but the C++ class should have worked just as well
i dont see this issue in VisualStudio when compiling the same source code, just on g++
@lazyPower Yeah, but #include <string> doesn't pull in strcpy().
That might be an implementation difference.
oyea?
03:20
Technically g++ is correct in this case.
the #include <string> coupled with the using namespace std; should have covered it.
unless i'm way off here
and the C++ lib doesnt include strcpy or strlen
Well, #include <string> (according to the spec., if I'm not mistaken) is not supposed to define strcpy even if it uses it internally.
where would it be provided if not in the string header?
or would i have to use the C libs to get those member functions?
It's part of the C standard library, so yes - you would need #include <string.h>.
oh
well, hmm.. odd.. some compiler mojo going on in the MS Sphere on that one.
03:23
I have VS 2010 Express.
I can check the headers...
(You have me curious now!)
haha
its really deranged man. Posix vs NonPosix systems - so much is lost in translation
03:39
Router reset in progress...
03:49
Search "<string.h>" (7 hits in 6 files)
  D:\Program Files\Microsoft Visual Studio 10.0\VC\include\cstring (2 hits)
	Line 9:   #include <string.h>
	Line 13:  #include <string.h>
  D:\Program Files\Microsoft Visual Studio 10.0\VC\include\exception (1 hits)
	Line 40:  #include <string.h>
  D:\Program Files\Microsoft Visual Studio 10.0\VC\include\msclr\marshal.h (1 hits)
	Line 24: #include <string.h>
  D:\Program Files\Microsoft Visual Studio 10.0\VC\include\regex (1 hits)
	Line 25: #include <string.h>
@lazyPower: That's all of the files that include string.h (in MS VC++).
I'll try Mingw32 now and compare them.
Search "<string.h>" (3 hits in 2 files)
  D:\Program Files\CodeBlocks\MinGW\include\c++\3.4.5\cstring (2 hits)
	Line 51: #include <string.h>
	Line 53: // Get rid of those macros defined in <string.h> in lieu of real functions.
  D:\Program Files\CodeBlocks\MinGW\include\c++\3.4.5\mingw32\bits\c++config.h (1 hits)
	Line 587: /* Define to 1 if you have the <string.h> header file. */
So there you go... MS VC++ includes string.h in exception and regex while Mingw32 does not.

last day (15 days later) »