Actually, I also got linker errors with template functions and template
classes. And I avoided both of them successfully, by pouring foo.cpp
into foo.h, according to the C++ FAQ.
( http://www.parashift.com/c++-faq-lit...templates.html)
And then, I pre-declared each template friend function above the
definition of template class. But I still get template friends linker
error. My compiler is gcc 3.3.3. Any hints? Thanks,
--
William Xuuu 10 2178
"william xuuu" <ab*@abc.abc> wrote in message news:87************@abc.abc... Actually, I also got linker errors with template functions and template classes. And I avoided both of them successfully, by pouring foo.cpp into foo.h, according to the C++ FAQ.
(http://www.parashift.com/c++-faq-lit...templates.html)
And then, I pre-declared each template friend function above the definition of template class. But I still get template friends linker error. My compiler is gcc 3.3.3. Any hints? Thanks,
Probably your 'pre-declaration' is not the same as your actual definition.
So the linker tries to find the definiton for you pre-declaration and finds
nothing. Show us the code.
Don't #include foo.cpp in foo.h, that just makes things unnecessarily
complicated. Put everthing into foo.h and throw away foo.cpp. You know it
makes sense.
john
"John Harrison" <jo*************@hotmail.com> writes: Probably your 'pre-declaration' is not the same as your actual definition. So the linker tries to find the definiton for you pre-declaration and finds nothing. Show us the code.
Okay, here it is: (only two files)
//----------------------- "vector_set.h"
//pre declarations.
template <typename T> class Vector_Set;
template <typename T> Vector_Set<T> operator+ (const Vector_Set<T>& s1,
const Vector_Set<T>& s2);
//begin class itself
template <typename T> class Vector_Set
{
friend Vector_Set<T> operator+ (const Vector_Set<T>& s1, const
Vector_Set<T>& s2);
public:
...
};
// friends implementation
template <typename T> Vector_Set<T> operator+ (const Vector_Set<T>& s1,
const Vector_Set<T>& s2)
{
...
}
//----------------------- "vector_set_main.cpp"
typedef Vector_Set<int> Set;
Set a = Set(10), b = Set(10);
for(int i = 0; i < a.size(); i++)
if(i % 2 == 1)
a.add_member(i);
else
b.add_member(i);
// Here causes error!
a + b;
warnings and errors:
g++ -c vector_set_main.cpp
In file included from vector_set_main.cpp:8:
vector_set.h:29: warning: friend declaration `Vector_Set<T> operator+(const
Vector_Set<T>&, const Vector_Set<T>&)' declares a non-template function
g++ -g vector_set_main.o -O2 -lm -o vector_set_main
vector_set_main.o(.text+0x172): In function `main':
: undefined reference to `operator+(Vector_Set<int> const&, Vector_Set<int> const&)'
does this help?
--
William Xuuu
> template <typename T> class Vector_Set { friend Vector_Set<T> operator+ (const Vector_Set<T>& s1, const Vector_Set<T>& s2);
This is a non-template function. It declares (as a friend) this function
Vector_Set<int> operator+ (const Vector_Set<int>& s1, const Vector_Set<int>&
s2); typedef Vector_Set<int> Set;
Set a = Set(10), b = Set(10); for(int i = 0; i < a.size(); i++) if(i % 2 == 1) a.add_member(i); else b.add_member(i);
// Here causes error! a + b;
Now the compiler thinks there are two functions to choose from, the
non-template (which you declared as a friend)
Vector_Set<int> operator+ (const Vector_Set<int>& s1, const Vector_Set<int>&
s2);
and the template (which you pre-declared)
template <class T>
Vector_Set<T> operator+ (const Vector_Set<T>& s1, const Vector_Set<T>& s2);
Given this choice the compiler always prefers the non-template. But since
you didn't define a non-template you get a link error.
The answer is to tell the compiler that the friend declaration refers to a
template function. You do that by adding '<T>' or '<>' after operator+.
template <typename T> class Vector_Set
{
friend Vector_Set<T> operator+ <>(const Vector_Set<T>& s1, const
Vector_Set<T>& s2);
It's easy to forget that you can have template and non-template versions of
the same function.
john
> The answer is to tell the compiler that the friend declaration refers to a template function. You do that by adding '<T>' or '<>' after operator+.
Hmm, seems gcc doesn't always handle the <T> form correctly. I would stick
with <>.
john
In article <87************@abc.abc>, William Xuuu <ab*@abc.abc> wrote: "John Harrison" <jo*************@hotmail.com> writes: Probably your 'pre-declaration' is not the same as your actual definition. So the linker tries to find the definiton for you pre-declaration and finds nothing. Show us the code.
Okay, here it is: (only two files) //----------------------- "vector_set.h"
//pre declarations. template <typename T> class Vector_Set; template <typename T> Vector_Set<T> operator+ (const Vector_Set<T>& s1, const Vector_Set<T>& s2);
//begin class itself template <typename T> class Vector_Set { friend Vector_Set<T> operator+ (const Vector_Set<T>& s1, const Vector_Set<T>& s2); public: ... };
// friends implementation template <typename T> Vector_Set<T> operator+ (const Vector_Set<T>& s1, const Vector_Set<T>& s2) { ... }
//----------------------- "vector_set_main.cpp"
typedef Vector_Set<int> Set;
Set a = Set(10), b = Set(10); for(int i = 0; i < a.size(); i++) if(i % 2 == 1) a.add_member(i); else b.add_member(i);
// Here causes error! a + b; warnings and errors:
g++ -c vector_set_main.cpp In file included from vector_set_main.cpp:8: vector_set.h:29: warning: friend declaration `Vector_Set<T> operator+(const Vector_Set<T>&, const Vector_Set<T>&)' declares a non-template function g++ -g vector_set_main.o -O2 -lm -o vector_set_main vector_set_main.o(.text+0x172): In function `main': : undefined reference to `operator+(Vector_Set<int> const&, Vector_Set<int> const&)'
does this help?
Like John and your warning say: the decl is not the same
as the definition. Your fried declares a non-template op+ however
your definition is for a template one. Figure out which you
need and use the same for both.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
"John Harrison" <jo*************@hotmail.com> writes: The answer is to tell the compiler that the friend declaration refers to a template function. You do that by adding '<T>' or '<>' after operator+.
Hmm, it works fine by adding <> or <T> as follows.
//begin class itself
template <typename T> class Vector_Set
{
friend Vector_Set<T> operator+<T> (const Vector_Set<T>& s1, const
Vector_Set<T>& s2);
};
While, the C++ FAQ didn't add <> or <T> ?!
Then I find it's not allowed to use + as:
a +<int> b;
So ?
Hmm, seems gcc doesn't always handle the <T> form correctly. I would stick with <>.
Could you explain the differences between <T> and <> ?
And, i find that either adding <T> after pre-declaration or its
implementation, as:
//pre declarations.
template <typename T> class Vector_Set;
template <typename T> Vector_Set<T> operator+<T> (const Vector_Set<T>& s1,
const Vector_Set<T>& s2);
// friends implementation
template <typename T> Vector_Set<T> operator+<T> (const Vector_Set<T>& s1,
const Vector_Set<T>& s2)
{ ... }
both would cause same error:
error: partial specialization `operator+<T>' of function template
Why would it happen?
--
William Xuuu
In article <87************@abc.abc>, William Xuuu <ab*@abc.abc> wrote: "John Harrison" <jo*************@hotmail.com> writes:
The answer is to tell the compiler that the friend declaration refers to a template function. You do that by adding '<T>' or '<>' after operator+. Hmm, it works fine by adding <> or <T> as follows.
//begin class itself template <typename T> class Vector_Set {
friend Vector_Set<T> operator+<T> (const Vector_Set<T>& s1, const Vector_Set<T>& s2); };
While, the C++ FAQ didn't add <> or <T> ?!
Then I find it's not allowed to use + as:
a +<int> b;
So ?
Not sure what you're asking, but perhaps you're looking for
the long-hand version like a.operator+<int>(b); Hmm, seems gcc doesn't always handle the <T> form correctly. I would stick with <>.
Could you explain the differences between <T> and <> ?
And, i find that either adding <T> after pre-declaration or its implementation, as:
//pre declarations. template <typename T> class Vector_Set; template <typename T> Vector_Set<T> operator+<T> (const Vector_Set<T>& s1, const Vector_Set<T>& s2);
// friends implementation template <typename T> Vector_Set<T> operator+<T> (const Vector_Set<T>& s1, const Vector_Set<T>& s2) { ... }
both would cause same error:
error: partial specialization `operator+<T>' of function template
Why would it happen?
Because a "primary" template declaration should not mention
it's arguments on its name, which the <T> would do above, but
since it's declared above, the friend is using it in your case,
not declaring it as such.
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it? co****@panix.com (Greg Comeau) writes: Hmm, it works fine by adding <> or <T> as follows.
//begin class itself template <typename T> class Vector_Set {
friend Vector_Set<T> operator+<T> (const Vector_Set<T>& s1, const Vector_Set<T>& s2); };
While, the C++ FAQ didn't add <> or <T> ?!
Then I find it's not allowed to use + as:
a +<int> b;
So ?
Not sure what you're asking, but perhaps you're looking for the long-hand version like a.operator+<int>(b);
Right, a.operator+<int>(b), when we declare it to be a member of a
class. But here it's declared as a friend function! So don't we have to
specifly <T> as well? i.e, a +<int> b
--
William Xuuu
"William Xuuu" <ab*@abc.abc> wrote in message news:87************@abc.abc... "John Harrison" <jo*************@hotmail.com> writes:
The answer is to tell the compiler that the friend declaration refers
to a template function. You do that by adding '<T>' or '<>' after operator+.
Hmm, it works fine by adding <> or <T> as follows.
The following code produces compiler errors on gcc.
#include <iostream>
using namespace std;
template <typename T> class Vector_Set;
template <typename T> Vector_Set<T> operator+ (const Vector_Set<T>& s1,
const Vector_Set<T>& s2);
template <typename T> class Vector_Set
{
friend Vector_Set<T> operator+ <T> (const Vector_Set<T>& s1, const
Vector_Set<T>& s2);
};
template <typename T> Vector_Set<T> operator+ (const Vector_Set<T>& s1,
const Vector_Set<T>& s2)
{
return s1;
}
int main()
{
typedef Vector_Set<int> Set;
Set a, b;
a + b;
}
Change <T> to <> in the friend declaration and it compiles, remove using
namespace std and it compiles. I think its a gcc bug.
john
> >> Then I find it's not allowed to use + as:
a +<int> b;
So ?
Not sure what you're asking, but perhaps you're looking for the long-hand version like a.operator+<int>(b);
Right, a.operator+<int>(b), when we declare it to be a member of a class. But here it's declared as a friend function! So don't we have to specifly <T> as well? i.e, a +<int> b
I think you're confused, a.operator+<int>(b) isn't a declaration, its a
function call.
a + b works because in C++ the template function to call is deduced from the
types of the arguments. You can specify the type by appending them after the
function name
func<int>(a, b)
if you want or need to be explicit about template parameters but this syntax
isn't available for the operator form of a function call, which is why Greg
advised
a.operator+<int>(b)
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: MiniDisc_2k2 |
last post by:
I have a class:
template <class T>
class linkedlist
{
protected:
class node
{
public:
T data;
|
by: Grahamo |
last post by:
Hi,
I have a question that pertains to Templates and link time. This is
totally for my own understanding and to correct what's obviously an
erroneous view of things on my behalf;
Lets say I...
|
by: Robbie Hatley |
last post by:
I'd always thougth that a C++ compiler/linker should be able to
instantiate a template in mulitple places (say, in two different
translation units), even using the same template parameters so
that...
|
by: Robbie Hatley |
last post by:
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote:
> Robbie Hatley wrote:
> >
> > I ran into a problem a few days ago when I added a couple of
> > template functions to one of my personal...
|
by: Mark |
last post by:
Sorry for creating such a newbish topic, but I just can't seem to
figure out what the problem is here.
// main.cpp
#include <cstdlib>
#include <iostream>
#include "Vector.h"
using namespace...
|
by: amhoov |
last post by:
Hi all,
OS - MacOS X 10.4.9
Compiler - gcc 4.0 (XCode)
I'm *extremely* new to C++ (but have extensive experience with Java),
so please go easy on me.
I'm running into a very odd situation...
|
by: Oneironaut |
last post by:
Hello friends,
I have an issue with a linker warning. It is the warning LNK4089. I am
working in MSVC6.0
I investigated and this warning tells that the import of the library
to which it makes...
|
by: meLlamanJefe |
last post by:
The following is an abridged copy of the code I am using in a template function along with the linker error produced during the build. The compiler has no issues with the code, only the linker:
...
|
by: bajajv |
last post by:
Hi,
While implementing CList class, I got linker errors for constructor and destructor. Below is the code -
template<class TYPE, class ARG_TYPE = const TYPE&>
class CList
{
protected:
struct...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |