Question

In VS2010 calloc is highlighted with the error "expression must have (pointer-to) function type" in the following code block only when it is inside of a member-function of a class. When it is placed in the main(), there is no problem. There are no variables named calloc.

RoadSegment ***map;

map = (RoadSegment ***) calloc(nRows, sizeof(RoadSegment **));
for( int rCur=0; rCur < nRows; ++rCur )
    map[rCur] = (RoadSegment **) calloc(nCols, sizeof(RoadSegment *));

I don't understand what the difference could be, nonetheless why the error is occurring in the first place in the member-function.

Was it helpful?

Solution

As predicted by my comment, it turned out that you had not included your headers in the right order. Your implementation was in a header, and so <cstdlib> needed to be included before that header:

#include <cstdlib>      // Required by myclass.h
#include "myclass.h" 

This makes things difficult when you have lots of source files requiring your header - each source file must remember to include other headers, even though they don't need those headers directly.

Here are some different strategies for avoiding this kind of problem:

  • Make a habit of always including standard headers and other library headers (anything in angle-brackets) before any local headers (anything in double-quotes) - note that each source file still must remember to include the prerequisite headers; or

  • If your header uses something from another header, make sure you include the required headers inside your own header (sometimes you have mutually-dependent headers and can't do this - instead you can forward-declare stuff for one of those headers); or

  • Use a pre-compiled header that is required for all source files and put all the common headers in there (this is the lazy approach, yet common for Microsoft compilers: ie "stdafx.h" - it has practical uses when you have very large headers that are used everywhere and you want to reduce compile time).

If you are implementing code in a header, make sure it's for the right reasons. If it's just that you're lazy and don't feel like splitting it into a source file, that's a bad reason. If it's template code that must be defined in a header, that's a good reason.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top