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

  • The open source cloud computing project OpenStack has come a long way since NASA and Rackspace launched it in 2010. Backed by leading technology infrastructure providers including Cisco, Dell, EMC, HP, IBM, Intel, and VMware, OpenStack underpins significant workloads at an increasingly diverse set of organizations, including BWM, CERN, Comcast, eBay, and Wal-Mart. For CIOs engaged in broader programs to win, serve, and retain customers -- and refocus business technology (BT) spend -- a planned and pragmatic …

  • Protect Your Business against Today's Rapidly Growing Threats Your business, employees, partners, and customers are depending more and more on network connectivity, and protecting data and infrastructure has moved to the top of the IT agenda. You can't arm yourself against today's rapidly increasing threats unless you understand how they work. Read this ebook to learn more about the top 10 DNS attacks that can target your external and internal DNS infrastructure, the impact they can have on the DNS server …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date