Hottest Forum Q&A on CodeGuru - December 15th


Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:

What is the usage of auto_ptr? (top)


_uj is using a auto_ptr in his header file, but unfortunaly he gets a warning. Can you tell us how to get rid of that warning?

When I try to declare this in a header file I get a warning.
auto_ptr ifs;
The program works, but how can I get rid of this warning?
C:\Program\Microsoft Visual Studio .NET 2003\Vc7\include\
warning C4150: deletion of pointer to incomplete type
        'std::basic_ifstream<_Elem,_Traits>'; no destructor called
C:\Program\Microsoft Visual Studio .NET 2003\Vc7\include
see reference to class template instantiation
'std::basic_ifstream<_Elem,_Traits>' being compiled with
C:\Program\Microsoft Visual Studio .NET 2003\Vc7\include
while compiling class-template member function
'std::auto_ptr<_Ty>::~auto_ptr(void)' with
c:\MINT\mint1\XLoadAtoms.h(13) : 
see reference to class template instantiation 'std::auto_ptr<_Ty>'
being compiled with

You get the warning because you are using auto_ptr the wrong way. To use it correctly, you need to declare it as in the following:
Info: The purpose of auto_ptr is to delete memory which you have newed; for example,

auto_ptr<CMyClass> pMyClass(new CMyClass);

The stored pointer must either be null or designate an object allocated by a new expression. The destructor for auto_ptr<Type> deletes the allocated object if it owns it.

How does a template cause code bloat? (top)


Kheun is curious about the accusation that a "template causes code bloat" and wants to know what is behind that!

Hi All,
I have been hearing a lot of "template causes code bloat"
accusations and I would like to find out how does that really
happen and what are the pitfalls that I should watch out for.

As of now, it is required that all template classes must have both
their declaration and definition to be in the same header file.
In addition, the definition for this template class member function
must be declared inline. I am wondering if the code bloat is caused
by an inefficient compiler that inlines all the member functions.

If not, it is caused by instantiating of many similar classes.
In other words, it is due to bad design.

The problem of declaring and defining a template in the same header is only persistant in VC++. Dev-C++ does not have such problems. Besides that, this is a topic which cannot be explained is some sentences here. But, take a look at this article. It desribes the problem briefly—solutions are also presented in this documentation.

How do I install a shared DLL when using Installshield? (top)


diegojancic is using Installshield for Visual Studio 6.0 and is not able to install a DLL, such as mfc40.dll. Does he need to re-distribute the DLL with his application?

I'm using the install shield for VC++ 6.0, and I can't install a
shared DLL, such as the mfc40.dll.... Does anybody know how I can
do this?

Well, actually you don't need to re-distribute or install the DLL because it comes with the newer OS. But, if you still want to re-distribute it, you can just copy it in the applications folder and the application will use this one instead of the file in the Windows directory.
See SafeDLLSearchMode for more infomation.

How can I get rid of a liner warning (LNK4089: OLEAUT32.DLL)? (top)


OReubens created a new MFC project and got a warning. Now he wants to get rid of that.

If you create a new MFC based project, using MFC as DLL, and
compile the release build, you end up getting following error:
LINK: warning LNK4089: all references to 'OLEAUT32.dll' discarded
Is there a way to get rid of the warning? I've tried adding
OLEAUT32.lib to the 'Ignore specific library' linker settings, but
then you end up getting another error (giving the impression that
OLEAUT32.DLL is needed, although it isn't).
mfcs71.lib(stdafx.obj) : error LNK2019: unresolved external
symbol __imp__SysFreeString@4 referenced in function
"public: __thiscall ATL::CComBSTR::~CComBSTR(void)"
I realise that "it's just a warning," but I really want to get rid
of it.

One possibility is to tell the linker to ignore it by adding the /IGNORE:4089 switch to the linker command line from the Project->Settings dialog. But, the problem is that this does not solve the problem. Adding the suggested line will ignore all LNK4089 errors.

So, what can be done here? Unfortunaly, I have not yet gotten the solution. Do you have any solution? If yes, e-mail it to me and I will publish it in the next column.

How do I sort a list of pointers? (top)


stober needs to sort a list of pointers. He has already written some code but unfortunaly he didn't get it to work. Maybe you can help! Here is his problem.

My list contains pointers, not the actual objects. How do I sort
// compiler error generated on this class because it does not
// have a class-specific parameter.
int operator<(MyClass*n1, MyClass*n2)
  return n1->x < n2->x;

// If I define it like this, no compiler errors but it is never
// called.
int operator<(MyClass& n1, MyClass& n2)
  return n1.x < n2.x;

list<MyClass*> theList;

MyClass *item = new MyClass;
item = new MyClass;
// etc. etc for the remaining items

// now sort the list
theList.Sort();    // error
After the above unsuccessful attempts to sort the list, I tried to
use the second form of list::sort, which has a parameter. I THINK
the parameter is to a comparison function. but that doesn't work
int compare(MyClass*n1, MyClass*n2)
  return n1.x < n2.x;

// the next line produces a compile error

The answer from YvesM is correct, but it only works with VC7. Here is his answer:

The comparison function should return bool and be const. Try the following:

struct MyClassCompare
  const bool operator()(const MyClass *n1, const MyClass *n2) const
     return n1->x < n2->x;
// ...

There is a bug in the std::list::sort function in that you need a greater<> template to satisfy the compiler. This has been (or should be) corrected in VC 7.0. Here is the workaround for VC6 provided by Paul McKenzie:

#include <iostream>
#include <vector>
#include <list>
#include <time.h>

class MyClass
    int x;

template <>
struct std::greater<MyClass*>
  bool operator()(const MyClass *n1, const MyClass *n2) const
     return n1->x < n2->x;

typedef int listtype;

using namespace std;

int main(int argc, char* argv[])
  list<MyClass*> theList;
  list<MyClass*>::iterator it;
  MyClass items[5];
  for(int i = 0; i < 5; i++)
        items[i].x = rand();

  for(it = theList.begin(); it != theList.end(); it++)
      MyClass* n = *it;
      cout << n->x << endl;
  return 0;

You specialize on the greater<> template, and the compiler will now call the greater<MyClass*> instead of the default greater<> (which will just compare pointer values).

Again, the code that Yves posted works correctly and must work correctly on every STL implementation. The only implementation I know where Yves' code does not work is on the VC 6.0 STL implementation provided by DinkumWare. I believe DinkumWare has corrected this issue, but you may have to pay for the upgrade.

About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at:


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds