473,403 Members | 2,366 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,403 software developers and data experts.

Integer printer won't compile

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!!!!!

Feb 22 '06 #1
10 1305
OK, I fixed the compiling prob, but now, instead of printing 0-5, it
prints all 0s. And it's static !!!!!

Feb 22 '06 #2
"Protoman" <Pr**********@gmail.com> wrote in news:1140576150.527670.67460
@f14g2000cwb.googlegroups.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.
Feb 22 '06 #3
Andre Kostur <nn******@kostur.net> wrote in
news:Xn******************************@207.35.177.1 34:
"Protoman" <Pr**********@gmail.com> wrote in news:1140576150.527670.67460 @f14g2000cwb.googlegroups.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".
Feb 22 '06 #4
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!!!!!

Feb 22 '06 #5
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!!!!!!!!!!!!!!!!!!!!

Feb 22 '06 #6
"Protoman" <Pr**********@gmail.com> wrote in news:1140587166.821389.21660
@o13g2000cwo.googlegroups.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?
Feb 22 '06 #7

Andre Kostur wrote:
"Protoman" <Pr**********@gmail.com> wrote in news:1140587166.821389.21660
@o13g2000cwo.googlegroups.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!!!!

Feb 22 '06 #8
"Protoman" <Pr**********@gmail.com> wrote in
news:11*********************@z14g2000cwz.googlegro ups.com:

Andre Kostur wrote:
"Protoman" <Pr**********@gmail.com> wrote in
news:1140587166.821389.21660 @o13g2000cwo.googlegroups.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); }
Feb 22 '06 #9

Andre Kostur wrote:
"Protoman" <Pr**********@gmail.com> wrote in
news:11*********************@z14g2000cwz.googlegro ups.com:

Andre Kostur wrote:
"Protoman" <Pr**********@gmail.com> wrote in
news:1140587166.821389.21660 @o13g2000cwo.googlegroups.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!!!!

Feb 22 '06 #10
Protoman wrote:
Andre Kostur wrote:
"Protoman" <Pr**********@gmail.com> wrote in
news:11*********************@z14g2000cwz.googlegro ups.com:
Andre Kostur wrote:
"Protoman" <Pr**********@gmail.com> wrote in

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 ...


no, I don't have anyother
requirements other than printing 0-5. Thanks!!!!


Well then you're making it much harder than need be. The solution is
above, although I'm unsure if you want that or:

#include <iostream>
int main() { std::cout << "0-5"; }

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 22 '06 #11

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

Similar topics

0
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...
3
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...
7
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...
20
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.,...
2
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. ...
61
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...
0
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...
2
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...
17
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:...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.