0

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
Ryoooon
  • 3
  • 4
  • 3
    maybe add a } at the end of the file to close namespace statement – Nagasaki Apr 17 '13 at 15:29
  • 4
    Just so you know, names with two underscores or beginning with an underscore followed by an uppercase letter are reserved for the implementation (compiler) and should not be used. – Captain Obvlious Apr 17 '13 at 15:29
  • @Nagasaki The file is actually much larger so the namespace is closed off. – Ryoooon Apr 17 '13 at 15:33
  • It will be easier to help if you post a short, self contained example that reproduces the problem. – juanchopanza Apr 17 '13 at 15:34
  • 1
    @user2291405 The error you mention should have told you *what line* that error occurs on. Can you add that to your question? – Drew Dormann Apr 17 '13 at 15:34
  • Is your code complete? Where does the error occurs? – didierc Apr 17 '13 at 15:35
  • 1
    Is there a `#define abs` somewhere in your header files? – Adam Rosenfield Apr 17 '13 at 15:39
  • @Adam Rosenfield yes there is a #define abs macro in one of the header files, I tried commenting it out and hoped it would use the one in no such luck. I get "implicit declaration of function Abs" error from another file – Ryoooon Apr 17 '13 at 15:49
  • 1
    Then that's the problem. Macros respect nothing. Wherever you write `abs`, the preprocessor immediately replaces it with the macro text without even giving the compiler a chance to look at it. – Gorpik Apr 17 '13 at 16:03

1 Answers1

0

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.

Andy Brown
  • 11,766
  • 2
  • 42
  • 61