Solved

VC++ 6.0 size_t and cstdlib

Posted on 2001-08-23
28
877 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:kiphughes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
  • 3
  • +3
28 Comments
 
LVL 2

Accepted Solution

by:
VCStud earned 50 total points
ID: 6420901
Hi,
You have to include the file in the following way: -

#include <cstdlib>
using namespace std;

This tells the compiler to use the std namespace & you would not get the error.

Cheers,
VCStud
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6420911
i've tried that. i get error C2871: 'std' : does not exist or is not a namespace.
0
 
LVL 1

Expert Comment

by:kuchnaheen
ID: 6421065
did u tried

STDDEF.H

as header without namespace??
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:kiphughes
ID: 6421080
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;
0
 
LVL 2

Expert Comment

by:VCStud
ID: 6421156
> i've tried that: #include "stddef.h"
you should use <stddef.h>
also try including <stdlib.h>
0
 
LVL 1

Expert Comment

by:kuchnaheen
ID: 6421227
>at the line: typedef std::size_t size_type;

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

and try to compile
0
 
LVL 86

Expert Comment

by:jkr
ID: 6421896
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...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6427283
>> 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.
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6427298
nietod, what do u recommend i do?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6427312
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6427313
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6428373
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...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6428536
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.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6431710
It is just a VC++ bug, here is the Bug Info:

http://support.microsoft.com/support/kb/articles/Q243/4/44.ASP

0
 
LVL 1

Author Comment

by:kiphughes
ID: 6448189
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.
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6448238
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6448383
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...
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6448482
jkr, the above code will not compile. i mentioned some of the errors i'd get in earlier posts.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6448633
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.
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6449275
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?
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6449312
same thing happens if i try #include <iostream>
0
 
LVL 22

Expert Comment

by:nietod
ID: 6450399
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.
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6455320
nietod, i sent u an email (tniec@wareunl.com), did u get it?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6456553
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.
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6457783
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6460466
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6563355
So VCStud had the answer after all.  Sorry to have wasted your time.
0
 
LVL 1

Author Comment

by:kiphughes
ID: 6566683
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question