Hi,
If this should be directed to another group, please let me know...
I've been working with templates for a few weeks and have been able to
develop some nice code on solaris using the Forte C++ compiler (version 7).
However, nothing related to templates seems to be compiling correctly when I
use g++ on netbsd. I can't tell if it is either: a problem with my code, a
problem with NetBSD, or a problem with GCC. I have tried to create the
most basic of tests to illustrate the problem. Could someone tell me why
this compiles and runs fine using the Forte compiler but does not with g++?

MyTemp.h
--------------------------

#include <iostream>
template <class T> class MyTemp
{
        private:
        T temp;

        public:
        MyTemp (T temp1);
        void print();


};

MyTemp.cpp
----------------------------------------

#include "MyTemp.h"

template<class T> MyTemp<T>::MyTemp( T temp1 )
{
        temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
        cout<<temp;
}

Test.cpp
------------------------------------------

#include <unistd.h>
#include <iostream>
#include <string>
#include <MyTemp.h>
using namespace std;
int main()
{
        cout<<"In Test"<<endl;
        MyTemp<string> mt("TEST");
        mt.print();
        return 0;
}

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

g++  -c -I./  -g -D_DEBUG  -Wall -Wno-parentheses -c *.cpp
g++ *.o -o Test
/usr/lib/libstdc++.so: warning: reference to compatibility vfork(); include 
<unistd.h> for correct reference
Test.o: In function `main':
/arpa/ag/d//Test/Test.cpp:7: undefined reference to 
`MyTemp<basic_string<char, string_char_traits<char>, 
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char, 
string_char_traits<char>, __default_alloc_template<false, 0> >)'
/arpa/ag/d//Test/Test.cpp:7: undefined reference to 
`MyTemp<basic_string<char, string_char_traits<char>, 
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char, 
string_char_traits<char>, __default_alloc_template<false, 0> >)'
/arpa/ag/d//Test/Test.cpp:8: undefined reference to 
`MyTemp<basic_string<char, string_char_traits<char>, 
__default_alloc_template<false, 0> > >::print(void)'
/arpa/ag/d//Test/Test.cpp:8: undefined reference to 
`MyTemp<basic_string<char, string_char_traits<char>, 
__default_alloc_template<false, 0> > >::print(void)'
*** Error code 1

Stop.

<< moderator edit: added [code][/code] tags >>

not sure what is wrong there! However this code (untested) should work

#include <iostream> // std::cout
#include <string> // std::string
#include <cstdlib> // system

template <class t> class mytemp
{
    public:
    
    mytemp(t temp) { data = temp; }
    void print(void) { std::cout << data; }

    private:

    t data;
};

int main(void)
{
    mytemp<std::string> m("test"); // cast string("test") if you want
    mytemp.print();
    system("PAUSE");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.