Topic: problem with multiple file


Author: "maxim" <maxk@touro.edu>
Date: 1999/10/18
Raw View
A template function or a class has to be visible at the time of
instantiation. That means that you can either put the definition into the .h
file or put it into the cpp file where the instantiation is made. (main.cpp
in your case).
Good luck!

RL wrote in message <_NwM3.38305$Hb3.334707@news.total.net>...
>file    main.cpp
>
>#include <iostream>
>#include "file.h"
>
>int main()
>{
>    fonction(12,13);
>    return 0;
>}
>-----
>file.cpp
>
>#include "file.h"
>template <class T>
>void fonction( int a, int b)
>{
>    a = a+b;
>}
>
>-----
>
>file.h
>
>#ifndef file_h
>#define file_h
>
>template <class T>
>void fonction( int a, int b);
>
>#endif
>
>---------------------
>
>I know the code is very useless but even this one don't compile. The error
>is about __cdecl and linking. Is it a c++ error, a linking error ( then my
>post is off topic ) or a compiler bug ?
>If i put out every preprocessor line on file.h, the error is the same, i'm
>use to get error this way " fonction is already define in .... " but now it
>don't even say that.
>Some of my older try, i already make some app., it work this way. The only
>difference is that i just learn template 1 week ago. Maybe is a template
>error.
>
>Thanks.
>---
>[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
>[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
>[              --- Please see the FAQ before posting. ---               ]
>[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "TiTi" <TJunkMyAss@mad.scientist.com>
Date: 1999/10/15
Raw View
This compiles under VC++6 (but probably on other compilers too). Three
files: maintest.cpp; test.h; test.cpp.

test.h:
    #ifndef file_h
    #define file_h
    template <class T>
    void fonction( T a, T b);
    #include "test.cpp"
    #endif

test.cpp:
    template <class T>
    void fonction( T a, T b)
    {a = a+b;}

maintest.cpp:
    #include <iostream>
    #include "test2.h"
    int main()
    {
        fonction <int> (12,13);
        return 0;
    }


Make sure that your test2.cpp is not included into any project (that is
subject to compilation) or so. Only your header file should be in your
project! This is so for VC++6, but I can imagine that with other compilers,
the same method has to be used. You cannot directly compile the template
function, you have to include it, and then it will compile (sort of inline
expansion - can't tell for sure (yet)).



TiTi




> file    main.cpp
> #include <iostream>
> #include "file.h"
> int main()
> {
>     fonction(12,13);
>     return 0;
> }

> file.cpp
> #include "file.h"
> template <class T>
> void fonction( int a, int b)
> {   a = a+b; }

> file.h
> #ifndef file_h
> #define file_h
> template <class T>
> void fonction( int a, int b);
> #endif
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "RL" <vlad3@total.net>
Date: 1999/10/14
Raw View
file    main.cpp

#include <iostream>
#include "file.h"

int main()
{
    fonction(12,13);
    return 0;
}
-----
file.cpp

#include "file.h"
template <class T>
void fonction( int a, int b)
{
    a = a+b;
}

-----

file.h

#ifndef file_h
#define file_h

template <class T>
void fonction( int a, int b);

#endif

---------------------

I know the code is very useless but even this one don't compile. The error
is about __cdecl and linking. Is it a c++ error, a linking error ( then my
post is off topic ) or a compiler bug ?
If i put out every preprocessor line on file.h, the error is the same, i'm
use to get error this way " fonction is already define in .... " but now it
don't even say that.
Some of my older try, i already make some app., it work this way. The only
difference is that i just learn template 1 week ago. Maybe is a template
error.

Thanks.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]