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,591 of 33,346   
   Ulrich Eckhardt to late-enthusiast   
   Re: Template-based code doesn't compile   
   29 Oct 11 10:38:27   
   
   02de9003   
   From: doomster@knuut.de   
      
   late-enthusiast wrote:   
   > #ifndef __TOTAL_H_   
   > #define __TOTAL_H_   
      
   Your program is already ill-formed here. All identifiers containing two   
   consecutive underscores are reserved.   
      
      
   > template class Total   
   > {   
   > public:   
   >     Total(const list& l);   
      
   Make that   
      
      Total(const list& l);   
      
   The first "" is not required. Also consider adding "explicit" unless   
   you want implicit conversion.   
      
      
   >    ~Total();   
   >   
   >     virtual T compute();   
      
   The object is both copyable and assignable, but this usually doesn't play   
   nice with inheritance, because you can't change the actual type of an   
   object through assignment. Also, you typically want a virtual destructor   
   in case of inheritance.   
      
      
   > template class Sum : public Total   
   > {   
   > public:   
   >   
   >     Sum(const list& l) : figures(l) {}   
      
   Note: This will not(!) initialize the "Total" base subobject with "l",   
   i.e. not call the constructor you declared above. IOW, its baseclass'   
   "figures" member will remain empty. Also note that having a member named   
   "figures" in both the baseclass and the derived class doesn't help   
   clarity.   
      
      
   >     Sum *sum = new Sum(figures);   
   >     Average *avg = new Average(figures);   
   [...]   
   > sum.cpp: In function `int main(int, char**)':   
   > sum.cpp:24: error: `Sum' is not a type   
   > sum.cpp:25: error: `Average' is not a type   
      
   Two things here:   
   1. There is no reason to use "new" here. If you have a Java background,   
   forget about its use of "new" and relearn it the C++ way.   
   2. "Sum" and "Average" are not types, they are templates. The types you   
   want here are Sum and Average, which is what you must use   
   with "new" (apart from not using new in the first place).   
      
      
   > This is the error I get when I run "gcc sum.cpp"   
      
   Two more things:   
   1. The C++ frontend is called g++ typically. I think gcc works, too,   
   because it autodetects the used language.   
   2. Always compile with warnings.   
      
      
   Uli   
      
      
   --   
         [ 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