Templatized constructors & temp variables

Z

Zachary Turner

Is it possible to declare a class with a templatized constructor? I
was sure it was, but GCC is giving me errors when I try to do this.
Most likely user error. Some example code is:

class MessageHeader
{
private:
int m_count;

public:
MessageHeader(int count) : m_count(count) {}
template<typename T> MessageHeader(T& iter)
{
m_count = set_m_count_from_iter(iter);
}

int get_count() const { return m_count; }
};


void foo();
{
vector<char> buf;
MessageHeader header ( vec_iter(buf) );

int count = header.get_count(); // Error on this line.
}

The exact error is:

request for memeber 'get_count' in 'header', which is of non-class
type 'MessageHeader ()(vec_iter)'

Clearly MessageHeader is a class type. If I refrain from using a
temporary variable, and instead write:

vector<char> buf;
vec_iter iter(buf);
Messageheader header(iter);

then it appears to work.

Why is this? Thanks
 
V

Victor Bazarov

Zachary said:
Is it possible to declare a class with a templatized constructor?
Yes.

I
was sure it was, but GCC is giving me errors when I try to do this.
Most likely user error. Some example code is:

class MessageHeader
{
private:
int m_count;

public:
MessageHeader(int count) : m_count(count) {}
template<typename T> MessageHeader(T& iter)
{
m_count = set_m_count_from_iter(iter);

'set_m_count_from_iter' undefined.
}

int get_count() const { return m_count; }
};


void foo();
{
vector<char> buf;

'vector' undefined.
MessageHeader header ( vec_iter(buf) );

The line above declares a _function_. And since I have no idea
what 'vec_iter' is (most likely a type), I am not sure what to
change it to, although most likely it should be

MessageHeader header((vec_iter)buf);
int count = header.get_count(); // Error on this line.
}

The exact error is:

request for memeber 'get_count' in 'header', which is of non-class
type 'MessageHeader ()(vec_iter)'

Clearly MessageHeader is a class type. If I refrain from using a
temporary variable, and instead write:

vector<char> buf;
vec_iter iter(buf);
Messageheader header(iter);

then it appears to work.

Why is this? Thanks

Read up on declarations.

V
 
A

Alf P. Steinbach

* Zachary Turner:
Is it possible to declare a class with a templatized constructor?
Yes.

I
was sure it was, but GCC is giving me errors when I try to do this.
Most likely user error. Some example code is:

class MessageHeader
{
private:
int m_count;

public:
MessageHeader(int count) : m_count(count) {}
template<typename T> MessageHeader(T& iter)
{
m_count = set_m_count_from_iter(iter);
}

int get_count() const { return m_count; }
};


void foo();
{
vector<char> buf;
MessageHeader header ( vec_iter(buf) );

int count = header.get_count(); // Error on this line.
}

The exact error is:

request for memeber 'get_count' in 'header', which is of non-class
type 'MessageHeader ()(vec_iter)'

Clearly MessageHeader is a class type.

The error message says "header" is of function type.

"header" is not of type MessageHeader.

You have declared it as a function.

If I refrain from using a
temporary variable, and instead write:

vector<char> buf;
vec_iter iter(buf);
Messageheader header(iter);

then it appears to work.

Why is this?

In this case there's no way to parse the declaration as a function
declaration, because the argument is not a type.
 
J

James Kanze

Zachary Turner wrote:

[...]
The line above declares a _function_. And since I have no idea
what 'vec_iter' is (most likely a type),

If vec_iter isn't a type, then this line doesn't declare a
function. If vec_iter is a function or a pointer to a function,
then this line defines a variable. And if vec_iter is anything
else, I think the line itself is illegal. (Unless, of course,
vec_iter is a macro. Then it's anybody's guess as to what the
line does:).)

[...]
If he'd have read the error message completely, he'd have seen
that the compiler was complaining about the type of header, and
that the compiler considered header as a "function(vec_iter)
returning MessageHeader". For once, the compiler error message
is very exact, and points its finger directly to the problem.

[...]
Read up on declarations.

To be fair to him, it's rather a subtle issue, and is considered
by many to be an embarassing issue in C++. Declaration syntax
was already seriously broken in C, and C++ just made it worse.
(It couldn't add features otherwise, without completely breaking
C compatibility.)
 
V

Victor Bazarov

James said:
To be fair to him, it's rather a subtle issue, and is considered
by many to be an embarassing issue in C++. Declaration syntax
was already seriously broken in C, and C++ just made it worse.
(It couldn't add features otherwise, without completely breaking
C compatibility.)

IMNSHO it makes very little sense to expose our views on brokenness
of C++ to a newbie, who's much better off not knowing (or even
suspecting) those things until gaining some experience with the
language. All goes to the bed-side manner, you know.

V
 
Z

Zachary Turner

Zachary Turner wrote:
[...]
MessageHeader header ( vec_iter(buf) );
The line above declares a _function_. And since I have no idea
what 'vec_iter' is (most likely a type),

If vec_iter isn't a type, then this line doesn't declare a
function. If vec_iter is a function or a pointer to a function,
then this line defines a variable. And if vec_iter is anything
else, I think the line itself is illegal. (Unless, of course,
vec_iter is a macro. Then it's anybody's guess as to what the
line does:).)

vec_iter is declared as

template<typename T>
class vec_iter
{
public:
vec_iter(std::vector<T>& rVector);
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top