Can you help me figure out why this integer printing program won't
compile? I've looked far and wide in my diagnostics to figure out why,
but I'm lost. Here's the code:
#include <iostream>
#include <cstdlib>
using namespace std;
template<int I>
class _name
{
public:
static void f()
{
static int i=0;
cout << i << endl;
i++;
_name<go?(I-1):0>::f();
}
private:
enum {go=(I-1)!=0};
};
// Specialization provides base case for
// recursion
template<>
class _name<0>
{
public:
static void f(int i){return;}
};
int main()
{
// Equivalent loop code
_name<5>::f();
system("PAUSE") ;
return 0;
}
Any help, please? Thanks!!!!! 10 1333
OK, I fixed the compiling prob, but now, instead of printing 0-5, it
prints all 0s. And it's static !!!!!
"Protoman" <Pr**********@g mail.com> wrote in news:1140576150 .527670.67460
@f14g2000cwb.go oglegroups.com: OK, I fixed the compiling prob, but now, instead of printing 0-5, it prints all 0s. And it's static !!!!!
a) Include the text that you are referring to... makes it hard to quote
properly.
#include <iostream> #include <cstdlib> using namespace std;
template<int I> class _name { public: static void f() { static int i=0; cout << i << endl; i++; _name<go?(I-1):0>::f(); } private: enum {go=(I-1)!=0}; };
// Specialization provides base case for // recursion template<> class _name<0> { public: static void f(int i){return;} };
int main() { // Equivalent loop code _name<5>::f(); system("PAUSE") ; return 0; }
b) Because it's static, you have 5 instances of the local variable i,
let's name them as follows:
_name<5>::f::i
_name<4>::f::i
_name<3>::f::i
_name<2>::f::i
_name<1>::f::i
Each one is initialized to zero, and is incremented to 1, but you never
see it.
c) Why aren't you just using a template function instead of an entire
class?
d) Post compilable, running code. Yours doesn't. There is no function
named f in the _name<0> instance.
e) What is "go" contributing to this problem? It does not appear to do
anything useful.
f) And if you fix all of these, you'll get 0-4.
Andre Kostur <nn******@kostu r.net> wrote in
news:Xn******** *************** *******@207.35. 177.134: "Protoman" <Pr**********@g mail.com> wrote in
news:1140576150 .527670.67460 @f14g2000cwb.go oglegroups.com: // Specialization provides base case for // recursion template<> class _name<0> { public: static void f(int i){return;} };
int main() { // Equivalent loop code _name<5>::f(); system("PAUSE") ; return 0; } d) Post compilable, running code. Yours doesn't. There is no function named f in the _name<0> instance.
Err... I meant "no function named f *taking no parameters* in the _name
<0> instance".
OK, I fixed that, but how do I initialize my static var? I have no clue
on how to do it for a template.
Code:
template<int I>
class _name
{
public:
static void f()
{
cout << i << endl;
_name<go?(I-1):0>::f();
i++;
}
private:
enum {go=(I-1)!=0};
static int i;
};
// Specialization provides base case for
// recursion
template<>
class _name<0>
{
public:
static void f(){return;}
};
template<I> int _name<int I>::i=0;
int main()
{
// Equivalent loop code
_name<5>::f();
system("PAUSE") ;
return 0;
}
Help, please. Thanks!!!!!
OK, I inited the static var, but I still get 0s. Why the hell won't it
increment?!!!!! !!!!!!!!!?
Code:
//supplies static var i to _name template
struct var
{
protected:
static int i;
};
template<int I>
class _name:protected var
{
public:
static void f()
{
static int j=i;
cout << j << endl;
j++;
_name<go?(I-1):0>::f();
}
private:
enum {go=(I-1)!=0};
};
int var::i=0;
// Specialization provides base case for
// recursion
template<>
class _name<0>
{
public:
static void f(){return;}
};
int main()
{
// Equivalent loop code
_name<5>::f();
system("PAUSE") ;
return 0;
}
Help, please!!!!!!! Thanks!!!!!!!!! !!!!!!!!!!!
"Protoman" <Pr**********@g mail.com> wrote in news:1140587166 .821389.21660
@o13g2000cwo.go oglegroups.com: OK, I fixed that, but how do I initialize my static var? I have no clue on how to do it for a template.
Code:
template<int I> class _name { public: static void f() { cout << i << endl; _name<go?(I-1):0>::f(); i++; } private: enum {go=(I-1)!=0}; static int i; };
// Specialization provides base case for // recursion template<> class _name<0> { public: static void f(){return;} }; template<I> int _name<int I>::i=0;
int main() { // Equivalent loop code _name<5>::f(); system("PAUSE") ; return 0; }
Help, please. Thanks!!!!!
You're back in the same place you just left. You still have 5 independant
instances of i. Presumably you're trying to only have one instance of i
shared among the 5 completely unrelated classes. There's a couple of ways
of doing it.
1) a simple global variable (ie: int i = 0;).
2) a common base class with a static protected variable, and the template
inherits from that base class.
I guess a more basic question... what are you actually trying to
accomplish?
Andre Kostur wrote: "Protoman" <Pr**********@g mail.com> wrote in news:1140587166 .821389.21660 @o13g2000cwo.go oglegroups.com:
OK, I fixed that, but how do I initialize my static var? I have no clue on how to do it for a template.
Code:
template<int I> class _name { public: static void f() { cout << i << endl; _name<go?(I-1):0>::f(); i++; } private: enum {go=(I-1)!=0}; static int i; };
// Specialization provides base case for // recursion template<> class _name<0> { public: static void f(){return;} }; template<I> int _name<int I>::i=0;
int main() { // Equivalent loop code _name<5>::f(); system("PAUSE") ; return 0; }
Help, please. Thanks!!!!!
You're back in the same place you just left. You still have 5 independant instances of i. Presumably you're trying to only have one instance of i shared among the 5 completely unrelated classes. There's a couple of ways of doing it.
1) a simple global variable (ie: int i = 0;). 2) a common base class with a static protected variable, and the template inherits from that base class.
I guess a more basic question... what are you actually trying to accomplish?
Print 0-5!!!!! And did you read my last post?!? I made _name
protectedly inherit from the struct var, which contains the static var
i. And its still not working!!!!! And globals are evil!!!!
"Protoman" <Pr**********@g mail.com> wrote in
news:11******** *************@z 14g2000cwz.goog legroups.com: Andre Kostur wrote: "Protoman" <Pr**********@g mail.com> wrote in news:1140587166 .821389.21660 @o13g2000cwo.go oglegroups.com:
> OK, I fixed that, but how do I initialize my static var? I have no > clue on how to do it for a template. > > Code: > > template<int I> > class _name > { > public: > static void f() > { > cout << i << endl; > _name<go?(I-1):0>::f(); > i++; > } > private: > enum {go=(I-1)!=0}; > static int i; > }; > > // Specialization provides base case for > // recursion > template<> > class _name<0> > { > public: > static void f(){return;} > }; > template<I> int _name<int I>::i=0; > > int main() > { > // Equivalent loop code > _name<5>::f(); > system("PAUSE") ; > return 0; > } > > Help, please. Thanks!!!!! You're back in the same place you just left. You still have 5 independant instances of i. Presumably you're trying to only have one instance of i shared among the 5 completely unrelated classes. There's a couple of ways of doing it.
1) a simple global variable (ie: int i = 0;). 2) a common base class with a static protected variable, and the template inherits from that base class.
I guess a more basic question... what are you actually trying to accomplish?
Print 0-5!!!!!
That's easy:
#include <iostream>
int main() { cout << "0\n1\n2\n3\n4\ n5\n"; }.
But I suspect you have more requirements than simply printing 0-5 ...
And did you read my last post?!? I made _name protectedly inherit from the struct var, which contains the static var i. And its still not working!!!!! And globals are evil!!!!
Your last post had not made it to my newsserver yet. Do not assume the
method by which I read USENET. Which I why I quoted the post that I'm
replying to. (And, yes, one should avoid global variables... I'm only
pointing it out as an option).
Quoting the other post of yours:
//supplies static var i to _name template struct var { protected: static int i; };
template<int I> class _name:protected var { public: static void f() { static int j=i; cout << j << endl; j++; _name<go?(I-1):0>::f(); } private: enum {go=(I-1)!=0}; }; int var::i=0; // Specialization provides base case for // recursion template<> class _name<0> { public: static void f(){return;} };
int main() { // Equivalent loop code _name<5>::f(); system("PAUSE") ; return 0; }
You did not just make i inhereted from a common base class, you've also
added a j variable for some reason. Also, why do you need a variable at
all? Why not:
#include <iostream>
using namespace std;
template <int I> class _name {
public:
static void f() { _name<I - 1>::f(); cout << I << "\n"; };
};
template <> class _name<0> {
public:
static void f() { cout << "0\n"; };
};
int main() { _name<5>::f(); }
Heck.. why bother with a templated solution at all? Why not:
#include <iostream>
using namespace std;
void f(int i) {
if (i == 0) { cout << "0\n"; return; }
f(i - 1); cout << i << "\n";
}
int main() { f(5); }
Andre Kostur wrote: "Protoman" <Pr**********@g mail.com> wrote in news:11******** *************@z 14g2000cwz.goog legroups.com:
Andre Kostur wrote: "Protoman" <Pr**********@g mail.com> wrote in news:1140587166 .821389.21660 @o13g2000cwo.go oglegroups.com:
> OK, I fixed that, but how do I initialize my static var? I have no > clue on how to do it for a template. > > Code: > > template<int I> > class _name > { > public: > static void f() > { > cout << i << endl; > _name<go?(I-1):0>::f(); > i++; > } > private: > enum {go=(I-1)!=0}; > static int i; > }; > > // Specialization provides base case for > // recursion > template<> > class _name<0> > { > public: > static void f(){return;} > }; > template<I> int _name<int I>::i=0; > > int main() > { > // Equivalent loop code > _name<5>::f(); > system("PAUSE") ; > return 0; > } > > Help, please. Thanks!!!!!
You're back in the same place you just left. You still have 5 independant instances of i. Presumably you're trying to only have one instance of i shared among the 5 completely unrelated classes. There's a couple of ways of doing it.
1) a simple global variable (ie: int i = 0;). 2) a common base class with a static protected variable, and the template inherits from that base class.
I guess a more basic question... what are you actually trying to accomplish?
Print 0-5!!!!!
That's easy:
#include <iostream> int main() { cout << "0\n1\n2\n3\n4\ n5\n"; }.
But I suspect you have more requirements than simply printing 0-5 ...
And did you read my last post?!? I made _name protectedly inherit from the struct var, which contains the static var i. And its still not working!!!!! And globals are evil!!!!
Your last post had not made it to my newsserver yet. Do not assume the method by which I read USENET. Which I why I quoted the post that I'm replying to. (And, yes, one should avoid global variables... I'm only pointing it out as an option).
Quoting the other post of yours:
//supplies static var i to _name template struct var { protected: static int i; };
template<int I> class _name:protected var { public: static void f() { static int j=i; cout << j << endl; j++; _name<go?(I-1):0>::f(); } private: enum {go=(I-1)!=0}; }; int var::i=0; // Specialization provides base case for // recursion template<> class _name<0> { public: static void f(){return;} };
int main() { // Equivalent loop code _name<5>::f(); system("PAUSE") ; return 0; }
You did not just make i inhereted from a common base class, you've also added a j variable for some reason. Also, why do you need a variable at all? Why not:
#include <iostream> using namespace std; template <int I> class _name { public: static void f() { _name<I - 1>::f(); cout << I << "\n"; }; };
template <> class _name<0> { public: static void f() { cout << "0\n"; }; };
int main() { _name<5>::f(); }
Heck.. why bother with a templated solution at all? Why not:
#include <iostream> using namespace std; void f(int i) { if (i == 0) { cout << "0\n"; return; } f(i - 1); cout << i << "\n"; }
int main() { f(5); }
Because I want to make use of TMP!!!! And, no, I don't have anyother
requirements other than printing 0-5. Thanks!!!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kyle |
last post by:
I need to take text from a database, turn it into a barcode from the
Web and print to a Zebra Z4M barcode printer. I am stuck at two
separate approaches.
1. Use ZPL II (Zebra's proprietary language to command the printer)
which is executed at a DOS Prompt with "COPY BarCode.txt LPT1" Is
there any way to run that from the web. Also, is there any other
method of sending the information in the text file to the printer?
2. Use some sort...
|
by: Simon G Best |
last post by:
Hello!
The C++ standard library provides facilities for finding out the sizes
(and other such stuff) of numeric types (::std::numeric_limits<>, for
example).
What I would like to do is to select a type on the basis of its traits.
More specifically, I would like to select an unsigned integer type on
the basis of the number of bits I need it to have. (This is because
there are particularly efficient ways of implementing the Advanced
|
by: Steve M |
last post by:
Hello,
I'm having problems sending information from a python
script to a printer. I was wondering if someone might send me
in the right direction. I wasn't able to find much by Google
TIA
Steve
|
by: GS |
last post by:
The stdint.h header definition mentions five integer categories,
1) exact width, eg., int32_t
2) at least as wide as, eg., int_least32_t
3) as fast as possible but at least as wide as, eg., int_fast32_t
4) integer capable of holding a pointer, intptr_t
5) widest integer in the implementation, intmax_t
Is there a valid motivation for having both int_least and int_fast?
|
by: Ryan Gregg |
last post by:
I've got a dot matrix printer that I need to print a line at a time to.
This is being used for an audit log on an application and every event
that occurs needs to print out right as it occurs.
I've previously had this working well in a serial communication method
where I talk directly to the printer using control codes and what not.
However, it seems that the higher ups want to be able to use a network
printer or other Windows-configured...
| |
by: John Baker |
last post by:
When declaring an integer, you can specify the size by using int16,
int32, or int64, with plain integer being int32.
Is integer the accepted default in the programming community?
If so, is there a way to remove the ones with size predefined from the
autolisting of types when I am declaring something?
--
To Email Me, ROT13 My Shown Email Address
|
by: Arno R |
last post by:
Hi all,
I recently converted an old app to Access 2000.
After a few code-modifications and such the app works (seemed to work ....)
BUT I just discovered that some reports (8 of 112) seemed to be corrupted in a strange way.
I develop (adapt this app) in my home-office and everything is (or seems to be) fine.
When I bring the *very same* mdb to another office where another default printer is installed, some reports won't open.
They...
|
by: Terry Olsen |
last post by:
Can anyone give me some guidance on installing a local printer with VB 2005?
I'm guessing I would probably do this with WMI, but i'm not finding anything
obvious. I see how to add a networked printer, but nothing to add a local
printer, assign it a port & driver.
|
by: matevzb |
last post by:
I've ran into some fishy code that, at first glance, is buggy, but it
seems to work correctly
and none of the compilers I've tried (five so far, on various systems)
gives any warnings.
The code:
============================
#include <stdio.h>
void
fcn (char *str)
|
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...
|
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...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |