Linker error

This is a discussion on Linker error within the C++ Programming forums, part of the General Programming Boards category; After searching through the other threads, can't quite find my problem with linker errors. Here is a sample of a ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    11

    Linker error

    After searching through the other threads, can't quite find my problem with linker errors. Here is a sample of a part of the code.

    Header
    Code:
    #ifndef Rectangle_h
    #define Rectangle_h
    #include <iostream>
    using namespace std;
    
    class Rectangle
    {              
                            
          public:
                 double avgPerimeter;
                 double avgArea,count; //count used a a counter to keep track of number of times i've computed the perimeter and area.
                 double x,y,h,w;
                 double Area, Perimeter;
                 Rectangle();
                 double getPerimeter();           
                 double getArea();                   
                 friend ostream& operator<<(ostream& outputStream, Rectangle& z);
                 friend istream& operator>>(istream& inputStream, Rectangle& z); 
                 
           private: 
                 char color[]; 
    };
    #endif
    .cpp
    Code:
    #include <iostream>
    #include "Rectangle.h"
    
    
    
    double getPerimeter()
           {
           count=count +1;
           Perimeter=w+w+h+h;
           avgPerimeter=(avgPerimeter+Perimeter);
           return(Perimeter);
           }
    double getArea()
           {
           Area=w*h;
           avgArea=(avgArea+(Area));
           return(Area);
           }
    
    istream& operator >>(istream& inputStream, Rectangle& z)
             {
             cout << "X:";
             inputStream >> z.x;
             cout << "Y:"; 
             inputStream >> z.y;       
             cout << "Width: ";
             inputStream >> z.w;
             cout << "Height: ";
             inputStream >> z.h;
             cout << "Color:";
             inputStream >> z.color;
             return inputStream;
             }
    ostream& operator <<(ostream& outputStream, Rectangle& z)
             {
             outputStream << "<rect x=\""; 
             outputStream << z.x; 
             outputStream << "\" y=\""; 
             outputStream << z.y; 
             outputStream << "\" Width=\""; 
             outputStream << z.w;
             outputStream << "\" Height=\""; 
             outputStream << z.h ;
             outputStream << "\" fill=\"" ;
             outputStream << z.color; 
             outputStream <<  "\"/>";
             return outputStream;
             }
    main
    Code:
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include "Rectangle.h"
    #include "Triangle.h"
    #include "Circle.h"
    #include "Shapes.h"
    using namespace std;
    
    Rectangle::Rectangle() //rectangle constructor
           {
           avgPerimeter = 0;
           avgArea = 0;
           count = 0;
           }
    Circle::Circle() //circle construtor
           {
           avgPerimeter = 0;
           avgArea = 0;
           count = 0;
           }
    Triangle::Triangle() //triangle constructor
           {
           avgPerimeter = 0;
           avgArea = 0;
           count = 0;
           }
    int main()
    {
        char choice[10] = "";
        double p;
        double a;
            
        Rectangle z;
        Triangle t;
        Circle c;
        
        
        while ((stricmp(choice, "uit") != 0))
        {
              cout << "\n\n\n";
              cout << "------Main Menu------\n";
              cout << "(R)ectangle\n";
              cout << "(C)ircle\n";
              cout << "T(riangle)\n";
              cout << "Q(uit)\n";
              cout << "Please Select One ---->";
              cin >> choice;
                       
             
             if (stricmp(choice, "r") == 0){                      
                          cin >> z;
                          cout << z;
                          }
                          
             else if (stricmp(choice, "c") == 0){
                          cin >> c;
                          cout << c;
                          /*cout <<"\n\n\n";
                          cout << "X--->";
                          cin >> c.x;
                          cout << "Y--->";
                          cin >> c.y;
                          cout << "Radius--->";
                          cin >> c.r;
                          assert(c.r >0);
                          p = c.getPerimeter();
                          a = c.getArea(); */
                        
                          }
             else if (stricmp(choice, "riangle") == 0){
                          cin >> t;
                          cout << t
                          ;
                          /*cout <<"\n\n\n";
                          cout << "X1--->";
                          cin >> t.x1;
                          cout << "Y1--->";
                          cin >> t.y1;
                          cout << "X2--->";
                          cin >> t.x2;
                          cout << "Y2--->";
                          cin >> t.y2;
                          cout << "X3--->";
                          cin >> t.x3;
                          cout << "Y3--->";
                          cin >> t.y3;
                          p = t.getPerimeter();
                          a = t.getArea(); */
                          
                         }
             
             else(cout << "Blah, bad user!");
                         
              
        }
    return 0;
    }
    I'm getting the error
    [Linker error] undefined reference to 'operator >> [std::istream&, Rectangle&]'
    [Linker error] undefined reference to 'operator << [std:stream&, Rectangle&]'
    Not sure where to go from here. Should my overloaded operators be in my main?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > double getPerimeter
    Don't you need to qualify these with Rectangle:: etc ?
    So that the compiler knows which class they belong to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You need to include the cpp file in the project or makefile or whatever so that it is also compiled and then linked to main.cpp.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    11
    Thanks salem. I haven't posted much, but I've learned ALOT from you and once again your right on target. I feel like such a goof when I miss a minor mistake like that. /bow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21