public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: templates undefined refrence problem
@ 2002-02-18 11:11 mike stump
  2002-02-18 12:10 ` Jeff Dever
  0 siblings, 1 reply; 4+ messages in thread
From: mike stump @ 2002-02-18 11:11 UTC (permalink / raw)
  To: jsdever; +Cc: gcc

> Date: Mon, 18 Feb 2002 09:28:44 -0500
> From: Jeff Dever <jsdever@sympatico.ca>
> To: gcc@gcc.gnu.org

> I can't seem to be able to instantiate template classes of different
> types.  I am using a template class that contains another template
> class with seperate definitions and declarations in headers and
> implementation files for each class.

> What am I doing wrong?

Get rid of all .cc files, except main.  Welcome to C++.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: templates undefined refrence problem
  2002-02-18 11:11 templates undefined refrence problem mike stump
@ 2002-02-18 12:10 ` Jeff Dever
  0 siblings, 0 replies; 4+ messages in thread
From: Jeff Dever @ 2002-02-18 12:10 UTC (permalink / raw)
  To: mike stump; +Cc: gcc

Okay, that worked.  Its pretty ugly having implementation in my header
files, and I'm not creating nice intermediate .o files anymore, but at
least it compiles and runs.  I didn't realize that was industry practice
...

I'm surprised that this was not a FAQ answer.  This issue does not appear
on the c++ faq as far as I could find with Google.

Thanks!


mike stump wrote:

> > Date: Mon, 18 Feb 2002 09:28:44 -0500
> > From: Jeff Dever <jsdever@sympatico.ca>
> > To: gcc@gcc.gnu.org
>
> > I can't seem to be able to instantiate template classes of different
> > types.  I am using a template class that contains another template
> > class with seperate definitions and declarations in headers and
> > implementation files for each class.
>
> > What am I doing wrong?
>
> Get rid of all .cc files, except main.  Welcome to C++.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: templates undefined refrence problem
@ 2002-02-19  8:47 Arun Saini , Gurgaon
  0 siblings, 0 replies; 4+ messages in thread
From: Arun Saini , Gurgaon @ 2002-02-19  8:47 UTC (permalink / raw)
  To: Jeff Dever; +Cc: mike stump, gcc

| Its pretty ugly having implementation in my header
| files, and I'm not creating nice intermediate .o files anymore, but at
| least it compiles and runs.  I didn't realize that was industry practice

It is ugly, I agree but it is a limitation of the compiler and not the
language. It is an industry practice because most compilers still do not
support the "export" keyword.

| I'm surprised that this was not a FAQ answer.  This issue does not appear
| on the c++ faq as far as I could find with Google.

It is part of the file bugs.html distributed with gcc 3.x. I qoute

"Most C++ compilers (G++ included) do not yet implement export, which is
necessary for separate compilation of template declarations and definitions.
Without export, a template definition must be in scope to be used. The
obvious workaround is simply to place all definitions in the header itself.
Alternatively, the compilation unit containing template definitions may be
included from the header."

| mike stump wrote:
|
| > Get rid of all .cc files, except main.  Welcome to C++.

This is not C++, just a workaround.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* templates undefined refrence problem
@ 2002-02-18  6:40 Jeff Dever
  0 siblings, 0 replies; 4+ messages in thread
From: Jeff Dever @ 2002-02-18  6:40 UTC (permalink / raw)
  To: gcc

I can't seem to be able to instantiate template classes of different
types.  I am using a template class that contains another template class
with seperate definitions and declarations in headers and implementation
files for each class.  When I compile, I consistantly get compiler
errors "undefined refrence" even though they compile to .o files
successfully.  The problem comes when it tries to link.

It appears to me that the compiler is not automaticly producing the
specializations that I am instantiating in my main function.  I have
tried differennt method to try and make this happen, by using -frepo,
bare compiler options, compiling all files at once but to no avail.
I was unable to find any compiler options to turn on template based on
the instantitation.  Using GCC 3.0.3 and 2.95.3.

What am I doing wrong?


A.hh
#ifndef T_HH
#define T_HH
template <class T> class A
{
    public:
        A();
};
#endif

A.cc
#include "A.hh"
template <class T> A<T>::A()
{  //...
}

B.hh
#include "A.hh"
#ifndef B_HH
#define B_HH
template <class T> class B
{
    public:
        B();
    private:
        A<T> a;
};

B.cc
#include "B.hh"
template <class T> B<T>::B()
{ //...
}

main.cc
#include "B.hh"
int main()
{
    A<int> ai;
    A<double> ad;
    return 0;
}

Makefile
CXX = g++
CXXFLAGS = -g -Wall -frepo
objects = A.o B.o main.o
all: $(objects)
 $(CXX) $(CXXFLAGS) -o $(program) $(objects)
A.o: A.cc A.hh
B.o: B.cc B.hh A.hh
main.o: main.cc B.hh

$ make
g++ -g -Wall -frepo   -c -o A.o A.cc
g++ -g -Wall -frepo   -c -o B.o B.cc
g++ -g -Wall -frepo   -c -o main.o main.cc
main.cc: In function `int main()':
main.cc:4: warning: unused variable `A<int> ai'
main.cc:5: warning: unused variable `A<double> ad'
g++ -g -Wall -frepo -o  A.o B.o main.o
main.o: In function `main':
main.cc:4: undefined reference to `A<int>::A()'
main.cc:5: undefined reference to `A<double>::A()'
collect2: ld returned 1 exit status
make: *** [all] Error 1




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-02-19 16:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-18 11:11 templates undefined refrence problem mike stump
2002-02-18 12:10 ` Jeff Dever
  -- strict thread matches above, loose matches on Subject: below --
2002-02-19  8:47 Arun Saini , Gurgaon
2002-02-18  6:40 Jeff Dever

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).