Hi, so I was wondering if someone could take a look at my code and help me out, I know it's far from perfect but I tried it best as I could. I made three constructors and the respective functions, any help would be appreciated.
You've asked for help without really specifying what exactly it is that you're seeking help about. Is it just about the sections commented in your code or something else? Also, need to see the Triangle header file
Yes I need help on the sections that I have commented out, but also want to know if I did the other functions right (which I don't think I did).
This is the header:
You're assigning color to vertexThreeColor, etc so it should be vertexThreeColor = color and not the other way round (see l-values and r-values in C++)
You can't do: Color=vertexOneColor && vertexTwoColor && vertexThreeColor; ... but have to assign them individually and remember Color has to be on the right
Also look-up member initialization for constructors so that you can have:
1 2
Triangle(const Point& pt1, const Point& pt2, const Point& pt3, const Color& color): // passing parameters as reference to avoid unnecessary copying and as const;
vertexOne(pt1), vertexTwo(pt2), vertexThree(pt3), vertexOneColor(color), vertexTwoColor(color), vertexThree(color) {}
Similarly in setColor() pass color as reference
Then, later on, it should be:
1 2 3 4
void Triangle::setvertexOne (Point& pt1)//don't write vertexOne in the ctor and then, later, VertexOne - it is just confusing and inconsistent style;
{
vertexOne = pt1;
}
All the <return-type> get...() functions should be const i.e. <return-type> get...()const
The read() method should be a friend of class Triangle and on the following lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void read (istream& ins, Triangle t)
{
ins("F:\\test.txt");
if(ins.is_open())
{
string line;
while(getline(ins, line))
{
stringstream stream(line);
stream>>t.vertexOne>>t.vertexTwo>>t.vertexThree>>t.vertexOneColor;//extraction operators have to be overloaded for class Point and class Color;
//assuming no delimiters apart from white-space in the istream;
//if there's only one color it can be read into vertexOneColor and the other set from it using the setters;
v.emplace_back(Triangle(t.vertexOne, t.vertexTwo, t.vertexThree, t.vertexOneColor, t.vertexTwoColor, t.vertexThreeColor));
// where v is a container like vector<Triangle> that is declared outside the scope of read() so that it remains after read() returns;
// Triangle objects are created directly into the container;
}
}
}
The write() method should actually be a friend of class Triangle and an overload of the insertion operator i.e.
1 2 3 4 5 6 7 8 9 10
ostream& operator<<(ostream& outs, const Triangle& t)
{
os<<"The vertices of the triangle are: \n";
os<<t.vertexOne<<t.vertexTwo<<t.vertexThree<<"\n";//again, insertion operators also need to be overloaded for Point and Color;
os<<"The colors of the triangle are: \n";
os<<t.vertexOneColor<<t.vertexTwoColor<<t.vertexThreeColor<<"\n"return os;
}