home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,589 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Template-based code doesn't compile   
   29 Oct 11 10:36:58   
   
   02de9003   
   From: daniel.kruegler@googlemail.com   
      
   Am 29.10.2011 10:20, schrieb late-enthusiast:   
   > Hi   
   >   
   > I can't compile the following code (with gcc 3.4.4):   
      
   There is a lot going wrong in this code. Let's start dissecting it:   
      
   > I have this code in the "totals.h" file:   
   >   
   > // beginning of totals.h   
   >   
   > #include   
   > using namespace std;   
      
   Please don't do this in a header. Any user-code, that includes this   
   header might get name collisions, because of this.   
      
   > #ifndef __TOTAL_H_   
   > #define __TOTAL_H_   
      
   I wonder, why you have this header protection *following* the above   
   shown code.   
      
   > // Parent   
   >   
   > template  class Total   
   > {   
   > public:   
   >      Total(const list&  l);   
   >     ~Total();   
      
   Why are you declaring a user-defined destructor? There is no need for   
   it, just don't do it.   
      
   >      virtual T compute();   
   >   
   > private:   
   >   
   >      list  figures;   
   > };   
      
   So, template Total does not have a default constructor, right? What is   
   actually the reason for the member figures here? You never seem to use   
   it in your program.   
      
   > // Sum template   
   >   
   > template  class Sum : public Total   
   > {   
   > public:   
   >   
   >      Sum(const list&  l) : figures(l) {}   
      
   This constructor implicitly calls the default constructor of the base   
   class Total. I assume, the intention is to call the constructor   
   Total(const list&) here. I also note, that member total is never   
   initialized, so your code runs amok in compute, if this is a built-in type.   
      
   >     ~Sum() {}   
      
   Same argument as with the destructor of Total, just remove that.   
      
   >      T compute();   
   >   
   > private:   
   >   
   >      list  figures;   
   >      T total;   
   >   
   > };   
   >   
   > template   
   > T Sum::compute()   
   > {   
   >      typename list  >::iterator i = figures.begin();   
   >   
   >      while (i != figures.end())   
   >      {   
   >          total += *i;   
   >          i++;   
   >      }   
   >   
   >      return total;   
   > }   
      
   The reference to list  > is not wrong, but misleading,   
   why not using list as in your member declaration?   
      
   This function shows that it is essential that member total is somehow   
   initialized, either in the constructor or at the beginning of compute().   
      
   > // Average template   
   >   
   > template  class Average : public Total   
   > {   
   > public:   
   >   
   >      Average(const list&  l) : figures(l) {}   
      
   Same problem here as in Sum: You need to call the constructor   
   Total(const list&). This code implicitly calls the default   
   constructor of Total, but there is none.   
      
   >     ~Average() {}   
      
   Same problem as before, just remove that.   
      
   >      T compute();   
   >   
   > private:   
   >   
   >      list  figures;   
   >      T total;   
      
   Same problem as in Sum: You need to initialize member total in the   
   constructor or in your compute function.   
      
   > };   
   >   
   > template   
   > T Average::compute()   
   > {   
   >      typename list  >::iterator i = figures.begin();   
   >   
   >      while (i != figures.end())   
   >      {   
   >          total += *i;   
   >          i++;   
   >      }   
   >   
   >      return (total/figures.size());   
   > }   
      
   Same problems as in Sum::compute.   
      
   > Then I have this in "sum.cpp"   
   >   
   > // beginning of sum.cpp   
   >   
   > #include   
   > #include   
   > #include "totals.h"   
   >   
   > using namespace std;   
   >   
   > int main(int argc, char *argv[])   
   > {   
   >      int N = atoi(argv[1]);   
      
   You miss to add a header to atoi, presumably this should be    
      
   >      list  figures;   
   >      int x = 0;   
   >   
   >      while (x != N)   
   >      {   
   >          figures.push_front((double) (x * (x + 2) + 1));   
   >          x++;   
   >      }   
   >   
   >      Sum  *sum = new Sum(figures);   
      
   You need to write new Sum(figures) here, because you are   
   instantiating new Sum   
      
   >      Average  *avg = new Average(figures);   
      
   Same problem here, write new Average(figures) instead.   
      
   >      printf(" sum = %8.2f, average = \n", sum->compute(), avg-   
   >> compute());   
      
   You miss a format specifier for the argument avg->compute().   
      
   > It's probably because I haven't placed the "typename" keyword   
   > somewhere, but I can't spot the place.   
      
   No, but several things else ;-)   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca