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:
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);
}
While compiling specify the flag---------------------
Work around:
-fpermissive
.Eg: gcc -fpermissive test.cpp
No comments:
Post a Comment