Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.11 KB  |  hits: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C   Taking address of temporary - error while assigning reference to pointer
  2. struct Line
  3. {
  4.     int length;
  5.     Line* nextLine;
  6. };
  7.  
  8. Line NewLine(Line& lineRef)
  9. {
  10.     Line newLine;
  11.     newLine.length = lineRef.length * 2;
  12.     return newLine;
  13. }
  14.  
  15. void Expand(Line& lineRef)
  16. {
  17.     //Error here states: Taking address of temporary [-fpermissive]
  18.     lineRef.nextLine = &NewLine(lineRef);
  19. }
  20.  
  21. int main() {
  22.  
  23.     Line line;
  24.  
  25.     Expand(line);
  26.  
  27.     cout << line.length << endl;
  28.     cout << line.nextLine->length << endl;
  29.  
  30.     return 0;
  31. }
  32.        
  33. struct Line
  34.     {
  35.             int length;
  36.             Line* nextLine;
  37.             ~Line(){delete nextLine;}
  38.              //Make copy constructor and assignment operator private
  39.     };
  40.  
  41.     void Expand(Line* lineRef)
  42.     {
  43.             lineRef->nextLine = new Line;
  44.             lineRef->nextLine->length = 2*(lineRef->length) ;
  45.     }
  46.  
  47. int main()
  48. {
  49.  
  50.         Line* line = new Line;
  51.         line->length = 5;
  52.  
  53.         Expand(line);
  54.  
  55.         cout << line->length << endl;
  56.         cout << line->nextLine->length << endl;
  57.  
  58.         delete line;
  59.         return 0;
  60. }
  61.        
  62. lineRef.nextLine = &NewLine(lineRef);