Question

I am currently trying to integrate a library (IsoAgLib) into my CPP project. I am not deeply experienced with CPP. The error I am getting is: "expected unqualified-id before '{' token". I believe it has something to do with templates as I have come across other similar issues. It might also have something to do with the abs function call. Any help would be much appreciated!

EDIT: I am leaving out code after the template, the file is quite large

Error 45 expected unqualified-id before '{' token
Error 47 expected unqualified-id before ')' token
Error 46 expected ')' before '{' token

all of these errors occur on line 31 which is "template inline T abs(const T& val)" (I left commenting out at the beginning)

#ifndef UTIL_FUNCS_H
#define UTIL_FUNCS_H

#include <IsoAgLib/isoaglib_config.h>
#ifdef USE_DATASTREAMS_IO
class StreamInput_c;
#endif
#include <cstdlib>  // Include before vector or else CNAMESPACE stuff is screwed up for Tasking
#include <cstring>
#include <vector>
#ifdef USE_VT_UNICODE_SUPPORT
#include <string>
#endif

// Begin Namespace __IsoAgLib
namespace __IsoAgLib
{
  template <class T> inline T abs(const T& val)
  {
    return (val < 0) ? (-val) : val;
  }

} // end of namespace __IsoAgLib
#endif
Était-ce utile?

La solution

On some systems abs() and others such as min() and max() are implemented as preprocessor macros. To see if that's the case for you, add the line

#undef abs

after this line:

#include <cstdlib>

The preprocessor has no respect for namespaces and will blindly substitute its definition of abs into your source code before it's fed to the compiler even if the result is syntactic chaos.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top