472,986 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Question regarding templates

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
3 1070
* 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
9
by: Hasani \(remove nospam from address\) | last post by:
I was reading a ppt ( http://www.gotdotnet.com/team/pdc/4064/tls310.ppt ) and came aross this statement. "Users can leverage a destructor. The C++ compiler generates all the Dispose code...
1
by: | last post by:
Hoping to get some ideas from more experienced hands regarding the best way to use object-oriented design to assist my development of a content management system destined for multiple devices. ...
2
by: Terrance | last post by:
Hello, I have a question in regards to hashtables that I was hoping someone can share some light on. I'm currently working with VB.net and I'm trying to learn as much as possible about the...
2
by: vikram_p_nayak | last post by:
This is quite fundamental. So I apologize if it has already been discussed. >From what I understand, a template declaration and definition is generally in the same file. (like the standard...
56
by: Chris Hills | last post by:
Hi, It came up in a standards panel meeting the other day that "all c or C++ programmers" have a copy of ISO C and/or C++ ... I challenged this and said most don't (outside those working on...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.