Hottest Forum Q&A on CodeGuru - December 15th

Introduction:

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)

Thread:

_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\
   memory(484):
warning C4150: deletion of pointer to incomplete type
        'std::basic_ifstream<_Elem,_Traits>'; no destructor called
with
[
_Elem=char,
_Traits=std::char_traits
]
C:\Program\Microsoft Visual Studio .NET 2003\Vc7\include
   \memory(484):
see reference to class template instantiation
'std::basic_ifstream<_Elem,_Traits>' being compiled with
[
_Elem=char,
_Traits=std::char_traits
]
C:\Program\Microsoft Visual Studio .NET 2003\Vc7\include
   \memory(483):
while compiling class-template member function
'std::auto_ptr<_Ty>::~auto_ptr(void)' with
[
_Ty=std::ifstream
]
c:\MINT\mint1\XLoadAtoms.h(13) : 
see reference to class template instantiation 'std::auto_ptr<_Ty>'
being compiled with
[
_Ty=std::ifstream
]

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);
pMyClass->SomeFunction();

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)

Thread:

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)

Thread:

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?

Hi,
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)

Thread:

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
by /OPT:REF
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)"
(??1CComBSTR@ATL@@QAE@XZ)
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)

Thread:

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
this?
// 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;
theList.push_back(item);
item = new MyClass;
theList.push_back(item);
// 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
either.
int compare(MyClass*n1, MyClass*n2)
{
  return n1.x < n2.x;
}

// the next line produces a compile error
theList.sort(compare);

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;
  }
};
// ...
theList.Sort(MyClassCompare())

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
{
  public:
    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;
  srand(time(0));
  MyClass items[5];
  for(int i = 0; i < 5; i++)
        items[i].x = rand();
 
  theList.push_back(&items[0]);
  theList.push_back(&items[1]);
  theList.push_back(&items[2]);
  theList.push_back(&items[3]);
  theList.push_back(&items[4]);

  theList.sort(greater<MyClass*>());
  for(it = theList.begin(); it != theList.end(); it++)
  {
      MyClass* n = *it;
      cout << n->x << endl;
  }
  cin.ignore();
  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 http://dotnetslackers.com. DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at: http://dotnetslackers.com/community/blogs/sonukapoor/

Comments

  • 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

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds