By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Question regarding templates

P: n/a
Hi,

I am trying to create a class that can write any type of object to a
file. I would like to use a template function which is a member of the
class. Something like:

class MyWriter
{
public:
...
template <class T>
void write(const T& buffer);
...
protected:
...
ofstream ofs;
...
};

template <class T>
void MyWriter::write(const T& buffer)
{
ofs.write((char*)(&buffer), sizeof(buffer));
}

The idea is that the function will be able to determine the size of the
object, so the user of the class will not have to pass the size to the
function. A possible use would be:

MyWriter writer;
MySpecialClass writeMe;
....
writer.write(writeMe);

This code does not link and i get an 'unresolved external' message. Can
anyone please help me get this resolved?

Jul 4 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* zv*****@gmail.com:
>
I am trying to create a class that can write any type of object to a
file. I would like to use a template function which is a member of the
class. Something like:

class MyWriter
{
public:
...
template <class T>
void write(const T& buffer);
...
protected:
...
ofstream ofs;
...
};

template <class T>
void MyWriter::write(const T& buffer)
{
ofs.write((char*)(&buffer), sizeof(buffer));
}

The idea is that the function will be able to determine the size of the
object, so the user of the class will not have to pass the size to the
function. A possible use would be:

MyWriter writer;
MySpecialClass writeMe;
...
writer.write(writeMe);

This code does not link and i get an 'unresolved external' message. Can
anyone please help me get this resolved?
Your question is answered by FAQ item 5.8, at <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 4 '06 #2

P: n/a
I have noticed that making the function inline solves this issue.
But if anyone knows of a better solution please let me know.

zvir...@gmail.com wrote:
Hi,

I am trying to create a class that can write any type of object to a
file. I would like to use a template function which is a member of the
class. Something like:

class MyWriter
{
public:
...
template <class T>
void write(const T& buffer);
...
protected:
...
ofstream ofs;
...
};

template <class T>
void MyWriter::write(const T& buffer)
{
ofs.write((char*)(&buffer), sizeof(buffer));
}

The idea is that the function will be able to determine the size of the
object, so the user of the class will not have to pass the size to the
function. A possible use would be:

MyWriter writer;
MySpecialClass writeMe;
...
writer.write(writeMe);

This code does not link and i get an 'unresolved external' message. Can
anyone please help me get this resolved?
Jul 4 '06 #3

P: n/a
I can compile and link your code successfully using gcc!
zv*****@gmail.com wrote:
I have noticed that making the function inline solves this issue.
But if anyone knows of a better solution please let me know.

zvir...@gmail.com wrote:
Hi,

I am trying to create a class that can write any type of object to a
file. I would like to use a template function which is a member of the
class. Something like:

class MyWriter
{
public:
...
template <class T>
void write(const T& buffer);
...
protected:
...
ofstream ofs;
...
};

template <class T>
void MyWriter::write(const T& buffer)
{
ofs.write((char*)(&buffer), sizeof(buffer));
}

The idea is that the function will be able to determine the size of the
object, so the user of the class will not have to pass the size to the
function. A possible use would be:

MyWriter writer;
MySpecialClass writeMe;
...
writer.write(writeMe);

This code does not link and i get an 'unresolved external' message. Can
anyone please help me get this resolved?
Jul 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.