error: declaration of `operator==' as non-function

A

Alex Vinokur

Hi,

GNU g++ 3.4 detects an error in code below.
What is wrong here?

--------- [C++] foo.cpp : BEGIN ---------

template <typename T>
struct Foo
{
friend bool operator== <T> (const Foo<T>&, const Foo<T>&); // Line#4
};

template <typename T>
bool operator== (const Foo<T>&, const Foo<T>&)
{
return false;
}

int main()
{
Foo<int> foo;
return 0;
}

--------- [C++] foo.cpp : END -----------



------ Compilation with GNU gcc 3.3 (Cygwin) : BEGIN ------

$ g++ --version
g++ (GCC) 3.3.3 (cygwin special)
[---omitted---]

$ g++ foo.cpp
// No errors/warnings

------ Compilation with GNU gcc 3.3 (Cygwin) : END --------



------ Compilation with GNU gcc 3.4 (DJGPP) : BEGIN ------

$ gpp --version
gpp.exe (GCC) 3.4.1
[---omitted---]


$ gpp foo.cpp

foo.cpp:4: error: declaration of `operator==' as non-function
foo.cpp:4: error: expected `;' before '<' token


------ Compilation with GNU gcc 3.4 (DJGPP) : END --------
 
S

Sharad Kala

Alex Vinokur said:
Hi,

GNU g++ 3.4 detects an error in code below.
What is wrong here?

--------- [C++] foo.cpp : BEGIN ---------

template <typename T>
struct Foo
{
friend bool operator== <T> (const Foo<T>&, const Foo<T>&); // Line#4

Shouldn't it be just - friend bool operator== (const Foo<T>&, const
Foo<T>&); // Line#4

Sharad
 
A

Alex Vinokur

Sharad Kala said:
Alex Vinokur said:
Hi,

GNU g++ 3.4 detects an error in code below.
What is wrong here?

--------- [C++] foo.cpp : BEGIN ---------

template <typename T>
struct Foo
{
friend bool operator== <T> (const Foo<T>&, const Foo<T>&); // Line#4

Shouldn't it be just - friend bool operator== (const Foo<T>&, const
Foo<T>&); // Line#4

Sharad

If I put line
friend bool operator== (const Foo<T>&, const Foo<T>&); // // Line#4
instead
friend bool operator== <T> (const Foo<T>&, const Foo<T>&); // Line#4
GNU gpp 3.4.1 produces the following warnings:

foo.cpp:4: warning: friend declaration `bool operator==(const Foo<T>&, const
oo<T>&)' declares a non-template function
foo.cpp:4: warning: (if this is not what you intended, make sure the function
template has already been declared and add <> after the function name here) -W
-non-template-friend disables this warning

Because I need template operator== the program should be rewritten as following:

--------- [C++] foo.cpp : BEGIN ---------
template <typename T>
struct Foo;

template <typename T>
bool operator== (const Foo<T>&, const Foo<T>&);

template <typename T>
struct Foo
{
friend bool operator== <> (const Foo<T>&, const Foo<T>&);
};

template <typename T>
bool operator== (const Foo<T>&, const Foo<T>&)
{
return false;
}

int main()
{
Foo<int> foo;
return 0;
}
--------- [C++] foo.cpp : END -----------

Now GNU gpp 3.4.1 accepts it.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top