473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Templatized constructors & temp variables

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 TMessageHeader(T& iter)
{
m_count = set_m_count_from_iter(iter);
}

int get_count() const { return m_count; }
};
void foo();
{
vector<charbuf;
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<charbuf;
vec_iter iter(buf);
Messageheader header(iter);

then it appears to work.

Why is this? Thanks

Jul 12 '07 #1
4 1720
Zachary Turner wrote:
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 TMessageHeader(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<charbuf;
'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<charbuf;
vec_iter iter(buf);
Messageheader header(iter);

then it appears to work.

Why is this? Thanks
Read up on declarations.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 12 '07 #2
On Jul 12, 11:14 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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:-).)

[...]
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 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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 13 '07 #3
James Kanze wrote:
On Jul 12, 11:14 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
[...]
>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.)
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 13 '07 #4
On Jul 13, 2:55 am, James Kanze <james.ka...@gmail.comwrote:
On Jul 12, 11:14 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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);
};
Jul 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
1
by: aurgathor | last post by:
Howdy, I got the templatized class Matrix working in the way it's written in the faq , and it works fine as: Matrix<sqr_T> display(80,25); However, I'd like to have this variable in the...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
6
by: brian_harris | last post by:
I have a function that passes a string class pointer to a function, this function is then suppose to fill it and the outer function uses it. But I believe that I am running into problem due to...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
8
by: Gemma Fletcher | last post by:
Hi all :) I'm implementing a menu system for my assignment. User inputs command & an if/else switch statement executes the command. I'm just not quite sure the best way to take user input. ...
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
21
by: A n g l e r | last post by:
Hi all. I'm facing the following construction: class ClOld { public ClOld(string a) { ... } } class ClNew: ClOld
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.