Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

Misleading error message (from declaring a friend function)

Koehler__Thomas
Beginner
934 Views

Hello, the icpc returns on the following code an error: 'error: "a" has already been declared in the current scope ', which is misleading as the error can be solved by declaring the function (like in the two commented lines). The gcc warns me with: 'warning: friend declaration ‘T N::a(C, int)’ declares a non-template function [-Wnon-template-friend] friend T N::a(C, int);' and notifies me with 'note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)'.

#include <iostream>

template <typename T>
class C;

namespace N {

template <typename T>
T a(C<T> b);

//template <typename T>
//T a(C<T>, int);
    
}   // END namespace N

template <typename T>
class C 
{
    T i;
 public:
    C(T i = 0) : i(i) { }
    
    template <typename T2>
    friend T N::a(C<T2>);
    
    friend T N::a(C, int);
};

int main()
{
    C<int> mc(7);
}


namespace N {

template <typename T>
T a(C<T> b)
{
    return b.i;  
}

template <typename T>
T a(C<T> b, int c)
{
    return b.i+c;
}
    
}   // END namespace N
I am aware, that the first friend declaration is not really usable, but this is the shortest way to show the error message that I could think of. Best regards, Thomas Köhler

 

 

0 Kudos
0 Replies
Reply