|
Prev: Improving ugly(?) design: Data validation
Next: ! Bang operator on fstream, how does it supposed to work?
From: coderyogi on 17 Jun 2008 05:23 The problem statement is to print the numbers from 1 to 100 and then back to 1; without using a) recursion b) any loops I've coded the solution as follows: [CODE] #include <iostream.h> class list { private: static int count; public: list () { cout << ++count << endl; } ~list () { cout << count-- << endl; } }; int main (void) { list a[100]; return 0; } [/CODE] Although the code gives correct output with turbo c compiler, i get following errors with g++: /tmp/ccJCckwR.o(.list::gnu.linkonce.t.(void)+0x16): In function `list::list(void)': : undefined reference to `list::count' /tmp/ccJCckwR.o(.list::gnu.linkonce.t.(void)+0x1c): In function `list::list(void)': : undefined reference to `list::count' /tmp/ccJCckwR.o(.gnu.linkonce.t._._4list+0x17): In function `list::~list(void)':: undefined reference to `list::count' /tmp/ccJCckwR.o(.gnu.linkonce.t._._4list+0x1d): In function `list::~list(void)':: undefined reference to `list::count' collect2: ld returned 1 exit status If anybody can help, thanx! -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: d04rp on 17 Jun 2008 11:32 Correction to my last post: ignore the second solution. In order to initialize a member within the class, the member must be cont static and integral. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Dennis on 17 Jun 2008 11:32 On Jun 17, 2:23 pm, coderyogi <zape...(a)gmail.com> wrote: > The problem statement is to print the numbers from 1 to 100 and then > back to 1; without using > a) recursion > b) any loops > I've coded the solution as follows: > > [CODE] > > #include <iostream.h> > > class list { > private: > static int count; > public: > list () > { > cout << ++count << endl; > } > ~list () > { > cout << count-- << endl; > } > > }; > > int main (void) > { > list a[100]; > return 0; > > } > > [/CODE] > > Although the code gives correct output with turbo c compiler, i get > following errors with g++: [SNIP] Functions can be static as declared, but to use a static member variable, you have to declare it outside the class somewhere. Think of it as allocating the space for your variable. I compiled your program and it worked fine for me after putting the following line after the class declaration: int list::count=0; -Dennis -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: d04rp on 17 Jun 2008 11:31 > The problem statement is to print the numbers from 1 to 100 and then > back to 1; without using > a) recursion > b) any loops > I've coded the solution as follows: > > [CODE] > > #include <iostream.h> > > class list { > private: > static int count; > public: > list () > { > cout << ++count << endl; > } > ~list () > { > cout << count-- << endl; > } > > }; > > int main (void) > { > list a[100]; > return 0; > > } > > [/CODE] > > Although the code gives correct output with turbo c compiler, i get > following errors with g++: > > /tmp/ccJCckwR.o(.list::gnu.linkonce.t.(void)+0x16): In function > `list::list(void)': > : undefined reference to `list::count' > /tmp/ccJCckwR.o(.list::gnu.linkonce.t.(void)+0x1c): In function > `list::list(void)': > : undefined reference to `list::count' > /tmp/ccJCckwR.o(.gnu.linkonce.t._._4list+0x17): In function > `list::~list(void)':: undefined reference to `list::count' > /tmp/ccJCckwR.o(.gnu.linkonce.t._._4list+0x1d): In function > `list::~list(void)':: undefined reference to `list::count' > collect2: ld returned 1 exit status > > If anybody can help, thanx! You must add the following line: int list::count = 0; (e.g after the class definition). This is how you initialize static variables in c++. Another solution to this problem, because count is both static and int is to initilize it directly. ie. change static int count; to static int count = 0; HTH Roger Schildmeijer -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Chris Thomasson on 17 Jun 2008 11:42 "coderyogi" <zaperaj(a)gmail.com> wrote in message news:50d15b83-400d-4a6c-a9f8-3987adaa08ec(a)s33g2000pri.googlegroups.com... > The problem statement is to print the numbers from 1 to 100 and then > back to 1; without using > a) recursion > b) any loops > I've coded the solution as follows: > > [CODE] [...] > [/CODE] > > Although the code gives correct output with turbo c compiler, i get > following errors with g++: [...] > > If anybody can help, thanx! try this: __________________________________________________________ #include <iostream> class list { static int count; public: list() { std::cout << ++count << std::endl; } ~list() { std::cout << count-- << std::endl; } }; int list::count = 0; int main() { list a[100]; return 0; } __________________________________________________________ -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 3 Prev: Improving ugly(?) design: Data validation Next: ! Bang operator on fstream, how does it supposed to work? |