Wednesday, 11 February 2015

error: ‘f’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] note: ‘int f(int)’ declared here, later in the translation unit

Cause: 

This error is because of a bug fix in gcc 4.7 or later.



Explaination: 


Compilers earlier than gcc 4.7 accept a function usage in a template function even prior to its declaration. Actually it's a bug in these compilers. Now it got fixed in gcc 4.7.  


For example, code shown below


-------------------------------

template<typename T>
int t(T i)
{ return f(i); }

int f(int i)
{ return i; }

int main()
{
  return t(1);
}
------------------------------
compile with gcc < 4.7 but will result in the following error if gcc >= 4.7:

--------------

In instantiation of ‘int t(T) [with T = int]’ required from here
error: ‘f’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
note: ‘int f(int)’ declared here, later in the translation unit
--------------


Fix:

To fix, make sure the function f in the code above is declared before first use in function t. Like so:

--------------------

int f(int i)
{ return i; }

template<typename T>
int t(T i)
{ return f(i); }

int main()
{
  return t(1);
}
---------------------
Work around:
While compiling specify the flag -fpermissive.
Eg: gcc -fpermissive test.cpp


No comments:

Post a Comment