D
Daniel
Using Visual Studio v. 10
In test.hpp:
#ifndef TEST_HPP
#define TEST_HPP
#include <ostream>
namespace ns
{
template <class T>
void serialize(std:
stream& os, const T& A)
{
os << "null";
}
template <class T>
class A
{
public:
A(const T& A)
: A_(A)
{
}
void serialize(std:
stream& os) const
{
ns::serialize(os,A_);
}
T A_;
};
}
#endif
In test.cpp:
#include "test.hpp"
namespace ns
{
template <>
void serialize(std:
stream& os, const double& A)
{
os << A;
}
}
int main()
{
ns::A<double> a(10.0);
a.serialize(std::cout);
ns::B<double> a(10.0); // *
}
This compiles and links, and the output is
10
(as expected.)
However, if I include a second cpp file in the build, and move line *
to a function in that file, I get a linker error
serialize ... already defined in test.obj
Am I doing something wrong?
Thanks,
Daniel
In test.hpp:
#ifndef TEST_HPP
#define TEST_HPP
#include <ostream>
namespace ns
{
template <class T>
void serialize(std:
{
os << "null";
}
template <class T>
class A
{
public:
A(const T& A)
: A_(A)
{
}
void serialize(std:
{
ns::serialize(os,A_);
}
T A_;
};
}
#endif
In test.cpp:
#include "test.hpp"
namespace ns
{
template <>
void serialize(std:
{
os << A;
}
}
int main()
{
ns::A<double> a(10.0);
a.serialize(std::cout);
ns::B<double> a(10.0); // *
}
This compiles and links, and the output is
10
(as expected.)
However, if I include a second cpp file in the build, and move line *
to a function in that file, I get a linker error
serialize ... already defined in test.obj
Am I doing something wrong?
Thanks,
Daniel