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?