undefined reference виртуального метода из шаблонного класса
От: gencoder  
Дата: 01.03.17 13:20
Оценка:
Помогите заставить видеть реализацию метода bar()

$ cat foo.hpp 
#ifndef QWERTY123
#define QWERTY123


namespace XYZ {
    template<class T> class Foo {
        public:
            T x;
        virtual void bar() = 0;
    };

    template<class T> class Bar: public Foo<T> {
        virtual void bar() final;
    };
}
#endif

$ cat foo.cpp 
#include <iostream>
#include "foo.hpp"

namespace XYZ {
    template<class T>
    void Foo<T>::bar(){ std::cout << "bar print: " << x << std::endl; }

    template<> class Foo<float>;
    template<> class Foo<int>;
}

$ cat main.cpp
#include <iostream>
#include "foo.hpp"

int main()
{
    XYZ::Bar<int> f;
    f.x=25;
    std::cout << f.x ;
}

$ g++ -std=c++11 -c foo.cpp
$ ar crs libfoo.a ./foo.o
$ g++ -std=c++11 main.cpp -L./ -lfoo && ./a.out
undefined reference to `XYZ::Bar<int>::bar()'
collect2: error: ld returned 1 exit status
template cpp
Re: undefined reference виртуального метода из шаблонного класса
От: night beast СССР  
Дата: 01.03.17 13:24
Оценка:
Здравствуйте, gencoder, Вы писали:

G>Помогите заставить видеть реализацию метода bar()


G>
G>$ cat foo.cpp 
G>#include <iostream>
G>#include "foo.hpp"

G>namespace XYZ {
G>    template<class T>
G>    void Bar<T>::bar(){ std::cout << "bar print: " << x << std::endl; }

G>    template<> class Bar<float>;
G>    template<> class Bar<int>;
G>}
G>
Re[2]: undefined reference виртуального метода из шаблонного класса
От: gencoder  
Дата: 01.03.17 13:34
Оценка:
$ cat foo.cpp 
#include <iostream>
#include "foo.hpp"
namespace XYZ {
    template<class T>
    void Bar<T>::bar(){ std::cout << "bar print" << std::endl; }
    template<> class Bar<float>;
    template<> class Bar<int>;
}

$ cat foo.hpp
#ifndef QWERTY123
#define QWERTY123
namespace XYZ {
    template<class T> class Foo {
        public:
        virtual void bar() = 0;
    };

    template<class T> class Bar: public Foo<T> {
        public:
        virtual void bar() final;
    };
}
#endif

$ cat main.cpp
#include "foo.hpp"
int main()
{
    XYZ::Bar<int> f;
    f.bar();
}

$ g++ -std=c++11 -c foo.cpp
$ ar crs libfoo.a ./foo.o
$ g++ -std=c++11 main.cpp -L./ -lfoo && ./a.out
undefined reference to `XYZ::Bar<int>::bar()'
collect2: error: ld returned 1 exit status
Re[3]: undefined reference виртуального метода из шаблонно
От: night beast СССР  
Дата: 01.03.17 13:37
Оценка: 35 (2)
Здравствуйте, gencoder, Вы писали:

попробуй template без скобочек в инстанцировании.
template class Bar<int>;
Отредактировано 01.03.2017 13:38 night beast . Предыдущая версия .
Re[3]: Explicit template instance
От: Qbit86 Кипр
Дата: 01.03.17 13:40
Оценка: 1 (1)
Здравствуйте, gencoder, Вы писали:

G>
G>$ cat foo.cpp 
G>#include <iostream>
G>#include "foo.hpp"
G>namespace XYZ {
G>    template<class T>
G>    void Bar<T>::bar(){ std::cout << "bar print" << std::endl; }
G>    template<> class Bar<float>;
G>    template<> class Bar<int>;
G>}


template class Bar<float>;
template class Bar<int>;
Глаза у меня добрые, но рубашка — смирительная!
Re[4]: undefined reference виртуального метода из шаблонно
От: gencoder  
Дата: 01.03.17 13:48
Оценка:
Здравствуйте, night beast, Вы писали:

NB>Здравствуйте, gencoder, Вы писали:


NB>попробуй template без скобочек в инстанцировании.

NB>
NB>template class Bar<int>;
NB>



спасибо, я правильно понял, что без скобочек насильное инстанцирование из шаблонного класса, а со скобочками подразумевается написание полной специализации (которую я забыл написать)?
Re[5]: undefined reference виртуального метода из шаблонно
От: night beast СССР  
Дата: 01.03.17 13:50
Оценка:
Здравствуйте, gencoder, Вы писали:

G>спасибо, я правильно понял, что без скобочек насильное инстанцирование из шаблонного класса, а со скобочками подразумевается написание полной специализации (которую я забыл написать)?


да.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.