Link to home
Start Free TrialLog in
Avatar of kiphughes
kiphughes

asked on

VC++ 6.0 size_t and cstdlib

i'm trying to use size_t in VC++ 6.0. the reference i have says to #include <cstdlib> and use the full name std::size_t.

this is a no go. i get error C2653: 'std' : is not a class or namespace name.

any ideas what's wrong?
ASKER CERTIFIED SOLUTION
Avatar of VCStud
VCStud

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kiphughes
kiphughes

ASKER

i've tried that. i get error C2871: 'std' : does not exist or is not a namespace.
did u tried

STDDEF.H

as header without namespace??
i've tried that: #include "stddef.h"

causes error C2039: 'size_t' : is not a member of 'std'

at the line: typedef std::size_t size_type;
> i've tried that: #include "stddef.h"
you should use <stddef.h>
also try including <stdlib.h>
>at the line: typedef std::size_t size_type;

remove std::
i.e. typedef size_t size_type;

and try to compile
Avatar of jkr
In VC++, 'cstdlib' is just a tiny wrapper around 'stdlib.h':

// cstdlib standard header

#if     _MSC_VER > 1000
#pragma once
#endif

#ifndef _CSTDLIB_
#define _CSTDLIB_
#ifdef _STD_USING
 #undef _STD_USING
 #include <stdlib.h>
 #define _STD_USING
#else
 #include <stdlib.h>
#endif /* _STD_USING */
#endif /* _CSTDLIB */

/*
 * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 */

So, there's no namespace std, and also no need to use it - simply refer to 'size_t' as 'size_t' or '::size_t', as it's a member of the global namespace...
>> You have to include the file in the following way: -
>> 
>> #include <cstdlib>
>> using namespace std;
Why?  If you are to bring the entire std namespace into the global namespace, you might as well just include <stdlib.h> instead of <cstdlib>.

>> In VC++, 'cstdlib' is just a tiny wrapper
>> around 'stdlib.h':
Apparently tjat is true, however it is a mistake.

The <cstdlib> file should have allt he declarations that appear in the <stdlib.h> file HOWEVER those declarations are supposed to be in the std namespace.  In other words, if you use <cstdlib> then size_t is supposed to be in the std namespace.  

the libraries that ship with VC seem to have this wrong and this is what is causing the problem.
nietod, what do u recommend i do?
Live with it?   :-)  :-(

Its a mistake in the library.  You can try to fix it by adding a

namespace std
{
   // old contents of <cstdlib> here
};

to the cstdlib file.  That is pretty much what was "supposed" to be there".   However, its very likely that while that fixes your current problem, it might cause all sorts of other problems  (which again should not occur.)  My guess is that either the standard library that ships with VC or VC itself is not capable of handling this for some reason (some incompatibility with standard C++.) and an attempt to place things in std that aren't there currently will probably cause problems.  I've seen that VC/its libraries fails at other times when mixing namespace and global code.

Or You could live with it in the global namespace, which for better or worse is certainly a common practice.  Or you could keep it in the flobal namespace, but prepare to use it in the std namespace by using some #defines (yuck), like

#ifdef NamespacesWork
#define InStd std::
#else
#define InStd
#endif

    *   *   *

InStd size_t ArraySize = 12;

this allows you to change from the global namespace to the std namespace by just changing a single #define.  it also helps you to uniquely mark the std::'s that are not currently working, so they can be tracked down and altered at some later date in order to "clean up" from this patch.
Live with it?   :-)  :-(

Its a mistake in the library.  You can try to fix it by adding a

namespace std
{
   // old contents of <cstdlib> here
};

to the cstdlib file.  That is pretty much what was "supposed" to be there".   However, its very likely that while that fixes your current problem, it might cause all sorts of other problems  (which again should not occur.)  My guess is that either the standard library that ships with VC or VC itself is not capable of handling this for some reason (some incompatibility with standard C++.) and an attempt to place things in std that aren't there currently will probably cause problems.  I've seen that VC/its libraries fails at other times when mixing namespace and global code.

Or You could live with it in the global namespace, which for better or worse is certainly a common practice.  Or you could keep it in the flobal namespace, but prepare to use it in the std namespace by using some #defines (yuck), like

#ifdef NamespacesWork
#define InStd std::
#else
#define InStd
#endif

    *   *   *

InStd size_t ArraySize = 12;

this allows you to change from the global namespace to the std namespace by just changing a single #define.  it also helps you to uniquely mark the std::'s that are not currently working, so they can be tracked down and altered at some later date in order to "clean up" from this patch.
Hmm, I thought "So, there's no namespace std, and also no need to use it - simply refer to 'size_t' as 'size_t' or '::size_t', as it's a member of the global namespace... " covered all that...
All the C++ <cXXXX> files are the same as the C <XXXX.h> files, except they define things in the std namespace.  Technically, the C++ files, not C ones, should be used.  The C++ standard does not define effects of using the C versions, only the C++ ones  But in practice, its often the C ones that are predictable and not the C++ ones.
ok, here's what i'm, trying to do. i'm trying to create a bag class. here's what my reference gives me as an example:

class bag
{
public:
     // TYPEDEFS and MEMBER CONSTANTS
     typedef int value_type;
     typedef std::size_t size_type;
     static const size_type CAPACITY = 30;
     // the rest of the public members will be listed later
private:
     value_type data[CAPACITY];
     size_type used;
};

what code do i need to include to make size_t in this header file work?

is there a compiler that is 100% compliant to the C++ standard? it's hard to learn when ur reference talks about the standard and ur compiler has other ideas.
ok, here's what i'm, trying to do. i'm trying to create a bag class. here's what my reference gives me as an example:

class bag
{
public:
     // TYPEDEFS and MEMBER CONSTANTS
     typedef int value_type;
     typedef std::size_t size_type;
     static const size_type CAPACITY = 30;
     // the rest of the public members will be listed later
private:
     value_type data[CAPACITY];
     size_type used;
};

what code do i need to include to make size_t in this header file work?

is there a compiler that is 100% compliant to the C++ standard? it's hard to learn when ur reference talks about the standard and ur compiler has other ideas.
Well, you should just use it as

class bag
{
public:
    // TYPEDEFS and MEMBER CONSTANTS
    typedef int value_type;
    typedef size_t size_type;
    static const size_type CAPACITY = 30;
    // the rest of the public members will be listed later
private:
    value_type data[CAPACITY];
    size_type used;
};

'size_t' isn't a member of 'namespace std' and I could not think of a reason why it should...
jkr, the above code will not compile. i mentioned some of the errors i'd get in earlier posts.
kiphughes, what I would do is to create a replacement <cstdlib> file.  Call it something like "MyStdLib" or something like that.  Its contents would be

namespace std
{
#include <cstdlib>
};

then use this include instead of <cstdlib>.  Then when the problem is fixed in VC's libraries, perhaps on the next release, go back and change your includes to use cstdlib as you recompile things

Next VC does not allow you to define a constant in a class.  You must define it in the enclosing namespace, like

class bag
{
public:
   // TYPEDEFS and MEMBER CONSTANTS
   typedef int value_type;
   typedef std::size_t size_type;
   static const size_type CAPACITY;
   // the rest of the public members will be listed later
private:
   value_type data[CAPACITY];
   size_type used;
};

const bag::size_type bag::CAPACITY = 30;

however, that doesn't work now because you can't use capacity to define the array size....  So instead of a constant, use an enum, like

class bag
{
public:
   // TYPEDEFS and MEMBER CONSTANTS
   typedef int value_type;
   typedef std::size_t size_type;
   enum { CAPACITY = 30 };
   // the rest of the public members will be listed later
private:
   value_type data[CAPACITY];
   size_type used;
};

This is a fairly common practice.
everyone, thank you for your assistance. nietod, your solution worked for me. however, if i try to #include <algorithm>, it causes some new problems. following your suggestion, i did this:

#include <MyStdLib.h>

class bag
{
public:
  // TYPEDEFS and MEMBER CONSTANTS
  typedef int value_type;
  typedef std::size_t size_type;
  enum { CAPACITY = 30 };
  // the rest of the public members will be listed later
private:
  value_type data[CAPACITY];
  size_type used;
};

this works fine. the problem is that in the implementation file of this class, i need to use #include <algorithm>. if i put that before the #include <bag.h>, i get this:

error C2039: 'size_t' : is not a member of 'std'

however, if i put that after the #include <bag.h>, i get these:

error C2146: syntax error : missing ';' before identifier 'wint_t'
fatal error C1004: unexpected end of file found

any ideas?
same thing happens if i try #include <iostream>
Try something like

#ifndef _MYCSTDLIB_
#define _MYCSTDLIB_
namespace std
{
#include <cstdlib>
#undef _CSTDLIB_
};
#endif

The problem is that the ordinary #ifdefs in <cstdlib> are preventing it from being included a 2nd time, outside of std, which is what the other STL files (mistakenely) want.
nietod, i sent u an email (tniec@wareunl.com), did u get it?
I've been away from my computer for 4 days.  I have 100s of e-mails to go through today.  I may or may not have gotten it.  I'll let you know.  But what problem, are you having?  I would have thought the last suggestion would have fixed this.
it didn't fix it. i think i may have come up w a solution as well. i'll give it a try later today or tomorrow.
I still haven't gotten to you letter, but I am leaving town again later today (about 7 hours from now)  What is the current problem?
So VCStud had the answer after all.  Sorry to have wasted your time.
my apologies. nietod, ur help was extremely helpful. however, i noticed that i made a mistake when trying to follow VCStud's suggestion. had i followed his suggestion in the first place, my problem would have been resolved a long time ago.