473,657 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need non-default constructor in a class to make a member


I have a problem with a template class defined:

// start matrix.h file
template <class Tclass Matrix
{
public:

Matrix() { // default constructor }

Matrix(const Subscript rows, const Subscript cols)
{
// constructs matrix but does special processing depending
// on values of rows/cols
}
private:
.....

};

// end matrix.h file
// start other class file:
const Subscript foo = 3;
const Subscript bar = 4;

class Other
{

private:

Matrix <doubleM(foo, bar); // doesn't compile
Matrix <doubleM; // compiles fine

};

g++ won't let this compile because of a "foo is not a type" error.
I think the compiler thinks that "M" is a private function prototype.
How can I force the compiler to realize M is just a "Matrix<double> " constructed
with the non-default constructor?

Mark
Apr 23 '07 #1
6 2107
Mark wrote:
I have a problem with a template class defined:

// start matrix.h file
template <class Tclass Matrix
{
public:

Matrix() { // default constructor }

Matrix(const Subscript rows, const Subscript cols)
{
// constructs matrix but does special processing depending
// on values of rows/cols
}
private:
.....

};

// end matrix.h file
// start other class file:
const Subscript foo = 3;
const Subscript bar = 4;

class Other
{

private:

Matrix <doubleM(foo, bar); // doesn't compile
Initialisations belong in the constructor initialiser list.
Matrix <doubleM; // compiles fine

};

g++ won't let this compile because of a "foo is not a type" error.
I think the compiler thinks that "M" is a private function prototype.
Yes, it does.
How can I force the compiler to realize M is just a "Matrix<double> "
constructed with the non-default constructor?
Put the proper initialisation of 'M' in the 'Other's constructor's
initialiser list.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 23 '07 #2
Mark <no******@nadas pam.comwrote in
news:f0******** **@aplnetnews.j huapl.edu:
>
I have a problem with a template class defined:

// start matrix.h file
template <class Tclass Matrix
{
public:

Matrix() { // default constructor }

Matrix(const Subscript rows, const Subscript cols)
{
// constructs matrix but does special processing depending
// on values of rows/cols
}
private:
.....

};

// end matrix.h file
// start other class file:
const Subscript foo = 3;
const Subscript bar = 4;

class Other
{

private:

Matrix <doubleM(foo, bar); // doesn't compile
Matrix <doubleM; // compiles fine

};

g++ won't let this compile because of a "foo is not a type" error.
I think the compiler thinks that "M" is a private function prototype.
How can I force the compiler to realize M is just a "Matrix<double> "
constructed with the non-default constructor?
Because you don't initialize member variables at the point of
declaration, you initialize them during the constructor of the enclosing
class:

const Subscript foo = 3;
const Subscript bar = 4;

class Other
{
public
Other() : M(foo, bar) {};

private:
Matrix<doubleM;
};

Apr 23 '07 #3
Victor Bazarov wrote:
Mark wrote:
>>I have a problem with a template class defined:

// start matrix.h file
template <class Tclass Matrix
{
public:

Matrix() { // default constructor }

Matrix(const Subscript rows, const Subscript cols)
{
// constructs matrix but does special processing depending
// on values of rows/cols
}
private:
.....

};

// end matrix.h file
// start other class file:
const Subscript foo = 3;
const Subscript bar = 4;

class Other
{

private:

Matrix <doubleM(foo, bar); // doesn't compile


Initialisations belong in the constructor initialiser list.

I knew that, I was just using the const Subscripts to show
what the two arguments were and to try to keep the example
simple. What I'm really doing is to
be able to instantiatate multiple different "Matrix"
instantiations with several different row and column
values where they are declared in the h files.

So the problem is I can't use an initialization list because
that's obviously fixed for those values.
So is there any way to pass different row and column values from
the header file? I think it might be possible to do this via
templates but I'm not clear on the syntax:

Matrix <double, row, colsM;

and in my Matrix template have a <T, Subscript, Subscriptsectio n
in addition to the usual <T where the former case uses the
non-default constructor.

BTW, I can't just go grab an expression
template library because this code base is too big and there'd be too
many lines to change. So I'm trying to slightly adapt the
code to occasionally use the non-default constructor (this will save memory
for my object pools) but usually use the default constructor (it's designed to
be faster for doing processing in function calls on the stack.)

Anyway, I think I see what the problem is now.

> Matrix <doubleM; // compiles fine

};

g++ won't let this compile because of a "foo is not a type" error.
I think the compiler thinks that "M" is a private function prototype.


Yes, it does.

>>How can I force the compiler to realize M is just a "Matrix<double> "
constructed with the non-default constructor?


Put the proper initialisation of 'M' in the 'Other's constructor's
initialiser list.

V
Apr 23 '07 #4
On Apr 23, 11:58 pm, Mark <none_...@nadas pam.comwrote:
Victor Bazarov wrote:
Mark wrote:
>I have a problem with a template class defined:
>// start matrix.h file
template <class Tclass Matrix
{
public:
Matrix() { // default constructor }
Matrix(const Subscript rows, const Subscript cols)
{
// constructs matrix but does special processing depending
// on values of rows/cols
}
private:
.....
>};
>// end matrix.h file
>// start other class file:
const Subscript foo = 3;
const Subscript bar = 4;
>class Other
{
>private:
Matrix <doubleM(foo, bar); // doesn't compile
Initialisations belong in the constructor initialiser list.
I knew that, I was just using the const Subscripts to show
what the two arguments were and to try to keep the example
simple. What I'm really doing is to
be able to instantiatate multiple different "Matrix"
instantiations with several different row and column
values where they are declared in the h files.
I'm not sure I understand; I don't see a problem. An
initializer can be any expression you want. You can't have more
than one data member with the same name, of course, but if your
class has two Matrix<double(s ay m1 and m2), there's absolutely
no problem with:

Other::Other()
: m1( foo, bar )
, m2() // optional...
{
}
So the problem is I can't use an initialization list because
that's obviously fixed for those values.
No. It can contain any expression you want.
So is there any way to pass different row and column values from
the header file? I think it might be possible to do this via
templates but I'm not clear on the syntax:
Matrix <double, row, colsM;
That's a different thing entirely. There's no problem with
defining non-type parameters, and using them, but then the
arguments must be constant expressions.
and in my Matrix template have a <T, Subscript, Subscriptsectio n
in addition to the usual <T where the former case uses the
non-default constructor.
You cannot overload class templates. If the Matrix template has
three parameters, it has three parameters (although you can
provide default values).

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

Apr 24 '07 #5
James Kanze wrote:
On Apr 23, 11:58 pm, Mark <none_...@nadas pam.comwrote:
>So is there any way to pass different row and column values from
the header file? I think it might be possible to do this via
templates but I'm not clear on the syntax:
>Matrix <double, row, colsM;

That's a different thing entirely. There's no problem with
defining non-type parameters, and using them, but then the
arguments must be constant expressions.
>and in my Matrix template have a <T, Subscript, Subscriptsectio n
in addition to the usual <T where the former case uses the
non-default constructor.

You cannot overload class templates. If the Matrix template has
three parameters, it has three parameters (although you can
provide default values).
In addition to providing default values it is possible to "narrow
down" the set of resulting types by partially specialising class
templates. Just a thought.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 24 '07 #6
On Apr 23, 11:16 pm, Mark <none_...@nadas pam.comwrote:
I have a problem with a template class defined:

// start matrix.h file
template <class Tclass Matrix
{
public:

Matrix() { // default constructor }

Matrix(const Subscript rows, const Subscript cols)
{
// constructs matrix but does special processing depending
// on values of rows/cols
}
private:
.....

};

// end matrix.h file

// start other class file:
const Subscript foo = 3;
const Subscript bar = 4;

class Other
{

private:

Matrix <doubleM(foo, bar); // doesn't compile
Matrix <doubleM; // compiles fine

};

g++ won't let this compile because of a "foo is not a type" error.
I think the compiler thinks that "M" is a private function prototype.
How can I force the compiler to realize M is just a "Matrix<double> " constructed
with the non-default constructor?
not tried....
Matrix <doubleM = Matrix <double>(foo, bar);

Apr 24 '07 #7

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

Similar topics

0
2434
by: Sofia | last post by:
My name is Sofia and I have for many years been running a personals site, together with my partner, on a non-profit basis. The site is currently not running due to us emigrating, but during its last year we got traffic of between 2000 - 2500 unique visitors per day. We are now about to re-launch the site from Sweden and we need to purchase a script to run it. Having looked at what is available on the net I have realised that we need a...
11
8748
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding if they left any field blank. My guess is that someone is trying to hack the site using the form to gain entry or run commands -- I don't really know since I'm not a hacker. I just know that forms are often susceptible to these kinds of...
5
1696
by: angelasg | last post by:
I am working with employee schedules. Each schedule is comprised of segments (shift, lunch, break, training, etc.) that have rankings. Each record has the employee id, the date the shift starts, the start and end time of each segment, the duration,the segment type and its rank. The start and end times of the schedules can overlap, but the segment that has the higher rank takes precedence. As a simple example, an employee working 8a-5p...
70
2783
by: rahul8143 | last post by:
hello, 1) First how following program get executed i mean how output is printed and also why following program gives different output in Turbo C++ compiler and Visual c++ 6 compiler? void main() { int val=5; printf("%d %d %d %d",val,--val,++val,val--); } under turbo compiler its giving
5
2940
by: HotRod | last post by:
I am new to this so please go easy. We currently have some students doing some work on some web based tracking documents for us. They are currently using VB .net to develop what we requested. Anyway I've been calling my local ISP's and no one supports .net it seems to be all apache and MySQL. I'm wondering if everyone here can answer a few questions. 1) Can I run vb .net web pages on a regular IIS server without the .net extensions? 2)...
18
2062
by: anand | last post by:
*********************************************************************************************************** #include<stdio.h> #include<conio.h> #include<math.h> void main() { double a,b,c,fa,fb,fc,err; int count;
17
2696
by: annai | last post by:
hi i want 2 know wat s the special for using conio.h
0
3945
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
1
1296
by: chris | last post by:
Hello, I have following Situation: I have a Dimension "SourceDirectory", whis has Elements "SourceDirectory1", "SourceDirectory2", ... until "SourceDirectory10". My Statement in MDX is whis: SELECT {.AllMembers} ON columns, {.Children} ON rows FROM It works. Now, how can I select all SourceDirectories but not the "SourceDirectory6", whis has only NULL-Values and I need't to see it in
43
4912
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy. That is, replace calls to strcpy with say, my_strcpy(dest,src) which will internally find the destination buffer length. For this we need to know the destination buffer size. For statically allocated strings sizeof is returning the length of the...
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8829
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.