Помогите заставить видеть реализацию метода 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
Здравствуйте, 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>
$ 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
Здравствуйте, gencoder, Вы писали:
попробуй template без скобочек в инстанцировании.
template class Bar<int>;
Здравствуйте, 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>;
Здравствуйте, night beast, Вы писали:
NB>Здравствуйте, gencoder, Вы писали:
NB>попробуй template без скобочек в инстанцировании.
NB>NB>template class Bar<int>;
NB>
спасибо, я правильно понял, что без скобочек насильное инстанцирование из шаблонного класса, а со скобочками подразумевается написание полной специализации (которую я забыл написать)?