-3

我正在使用 MSVS2013。'Point' 对象在 Rectangle.h 文件中生成错误。这就是我所得到的。(我已将显示错误的行标记为:“ /** <<---HERE **/ ”)我已经在类语法中对类进行了多次搜索,但我要么问错了问题,要么措辞不正确。

谢谢,Mark L.(你们应该在底部的列表中添加一个“newb”标签)

// Ch9.DRAFT-Point.h
#ifndef POINT_H
#define POINT_H

class Point
{
public:
    Point(double = 0.0, double = 0.0); // constructor

    //set functions & get functions
    void setX(double); 
    void setY(double);
    double getX();
    double getY();
private:
    double x; // 0.0 <= x <= 20
    double y; // 0.0 <= y <= 20
}; //end class Point

#endif

//  end Point.h
///////////////////////////////////////////////////////////////////////

/** THIS FILE IS THE PROBLEM **/
/** the object 'Point' is causing errors **/

// Ch9.DRAFT-Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "Point.h"

class Rectangle 
{
public:
    // default constructor
    Rectangle(Point = Point(0.0,1.0), Point(1.0,1.0), /** <<<---HERE**/
        Point(1.0,0.0), Point(0.0,0.0));    /** <<<---AND HERE**/

    // set x, y, x2, y2 coordinates
    void setCoord(Point, Point, Point, Point);  /** <<<---AND HERE**/
    double length(); // length
    double width(); // width
    void  perimeter(); // perimeter
    void area(); // area
    bool square(); // square
private:
    Point point1;   /**  <<<---AND HERE**/
    Point point2;   /**  <<<---AND HERE**/
    Point point3;   /**  <<<---AND HERE**/
    Point point4;   /**  <<<---AND HERE**/
}; // end class Rectangle

#endif

//  end Rectangle.h
///////////////////////////////////////////////////////////////////////

// Ch9.DRAFT-Point.cpp
// Member function definitions for class Point

#include "Ch9.DRAFT-Point.h"

Point::Point(double xCoord, double yCoord)
{
    setX(xCoord); // functio setX()
    setY(yCoord); // functio setY()
} // end Point constructor

// set x coordinate
void Point::setX(double xCoord)
{
    x = (xCoord >= 0.0 && xCoord <= 20.0) ? xCoord : 0.0;
} // end setX()

// set y coordinate
void Point::setY(double yCoord)
{
    y = (yCoord >= 0.0 && yCoord <= 20.0) ? yCoord : 0.0;
} // end setY()

// return x coodinate
double Point::getX()
{
    return x;
} //end getX()

// return y coodinate
double Point::getY()
{
    return y;
} //end getY()

//  end Point.cpp
///////////////////////////////////////////////////////////////////////

// Ch9.DRAFT-Rectangle.cpp
// Member-function definitions for class Rectangle.
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Rectangle.h"
using namespace std;

Rectangle::Rectangle(Point a, Point b, Point c, Point d)
{
    setCoord(a, b, c, d); // function setCoord()
} // end Rectangle constructor

void Rectangle::setCoord(Point p1, Point p2, Point p3, Point p4)
{
    // verify rectangle formed
    if((p1.getY() == p2.getY() && p1.getX() == p4.getX()
    p2.getX() == p3.getX() && p3.getY() == p4.getY()))
    {
        point1 = p1;
        point2 = p2;
        point3 = p3;
        point4 = p4;
    } // end if
    else
    {
        cout<< "Coordinates do not form a rectangle!\n"
        << "Use default values.\n";
        point1 = Point(0.0,1.0);
        point2 = Point(1.0,1.0);
        point3 = Point(1.0,0.0);
        point4 = Point(0.0,0.0);
    } // end else
} // setCoord()

void Rectangle::length( ) 
{ 
    double side1 = fabs(point4.getY() - point1.getY()); // get side1
    double side2 = fabs(point2.getX() - point1.getX()); // get side2
    double length = (side1 < side2 ? side1 : side2);
    return length;
} // end length()

void Rectangle::width( ) 
{ 
    double side1 = fabs(point4.getY() - point1.getY()); // get side1
    double side2 = fabs(point2.getX() - point1.getX()); // get side2
    double width = (side1 < side2 ? side1 : side2);
    return width;
} // end width()

void Rectangle::perimeter() 
{ 
    cout << fixed << "\nThe perimeter is " << setprecision(1)
        << 2 * (length() + width()) << endl;
} // end perimeter()

void Rectangle::area() 
{ 
    cout << fixed << "\nThe area is " << setprecision(1)
        << (length() * width()) << endl;
} // end area()

bool Rectangle::square()
{
    return (fabs(point4.getY() - point1.getY()) ==
    fabs(point2.getX() - point1.getX());
} // end square()

//  end Rectangle.cpp
///////////////////////////////////////////////////////////////////////

// Ch9.DRAFT-Ex09_11.cpp
#include <iostream> 
#include "Rectangle.h" // include definition of class Rectangle
using namespace std;


int main()
{
    Point w(1.0,1.0);
    Point x(5.0,1.0);
    Point y(5.0,3.0);
    Point z(1.0,3.0);
    Point j(0.0,0.0);
    Point k(1.0,0.0);
    Point m(1.0,1.0);
    Point n(0.0,1.0);
    Point v(99.0,-2.3);

    Rectangle rectangles[4]; // array of 4 rectangles

    // output rectangles
    for(int i = 0; i < 4; i++)
    {
        cout << "Rectangle" << i + 1 << ":\n";

        switch (i) // init 4 rectangles
        {
            case 0: // 1st rectangle
                rectangles[i] = Rectangle(z, y, x, w);
                break;
            case 1: // 2nd rectangle
                rectangles[i] = Rectangle(j, k, m, n);
                break;
            case 2: // 3rd rectangle
                rectangles[i] = Rectangle(w, x, m, n);
                break;
            case 3: // 4th rectangle
                rectangles[i] = Rectangle(v, x, y, z);
                break;
        } // end switch

        cout << "length = " << rectangles[i].length();
        cout << "\nwidth = " << rectangles[i].width();
        rectangles[i].perimeter();
        rectangles[i].area();
        cout << "The rectangle " 
            << (rectangles[i].square() ? "is" : "is not")
            << "a square.\n";
    } // end for

    return 0;
} // end main

//  end Ex09_11.cpp
///////////////////////////////////////////////////////////////////////



/**********************************************************************
Chapter 09 exercise instructions:
Do exercise 9.12 on page 429 for 100 points.
This exercise makes a reference to Exercise 9.11. Here is the code for Exercise 9.11: Ex09_11.zip
DO NOT click drag the code into your new project. The code MUST be copied and pasted into new .h/.cpp files which you create. If you drag copy code into your project and then send it to me it probably will not contain that code!! This means you WILL have a rectangle class (Rectangle.h and Rectangle.cpp) and a file called Ex09_11.cpp that has your main function.
Before submitting your project check your archive and be sure it contains your .h and .cpp files.
I will be looking for the following in your program.
*Your program MUST use Cartesian coordinates (x and y coordinates for a total of eight coordinates per figure, each corner of your rectangle is an x,y coordinate). This is a major modification of the supplied code.
*You must follow my previous instructions regarding how to start your program. What you are doing is editing the supplied code so instead of the class Rectangle using width and length it will use cartesian coordinates as defined above.
*Do not have the user enter the coordinates. Hard code them into your program.
*You must include a default constructor for the rectangle class. The default constructor should have the following coordinates ( point 1:(0,0), point 2:(0,1), point 3:(1,1), point 4:(1,0). Be sure the constructor has the set function as described in the textbook assignment. I recommend you pass these points as eight integers (0,0,0,1,1,1,1,0).
*Your program must instantiate four objects of the Rectangle class.
•Your first object should not form a square using your supplied coordinates.
•Your second object should form a square using your coordinates.
•Your third object should should not be instantiated with any points allowing your default constructor points to be used
Hint: Remember that your constructor header can contain default values. If you create an object without passing values those default values will be used. See example program in chapter.
•Your fourth object should not form a rectangle and the default coordinates should replace your original coordinates.
The textbook assignment asks for a set function that tests if any coordinates are over 20. You do not have to write that function.
Do include a function that gets called to check if your coordinates form a square or rectangle as mentioned in the textbook assignment.
Keep your code simple. To decide if the rectangle is a square compare its height verses width.You choose which points and values to compare. If length = width is a square.
This is a programming project intended to show that you know how to program functions and understand constructors..
Review my supplied image of the program's output as a guide.
If you submit the code that I am supplying you will receive zero points.
*AGAIN, do not have the user make the coordinate entries. Code it into the program.
 **************************************************************************/
4

3 回答 3

1

This is not syntacticaly correct...

Rectangle(Point = Point(0.0,1.0), Point(1.0,1.0), /** <<<---HERE**/
        Point(1.0,0.0), Point(0.0,0.0));    /** <<<---AND HERE**/

This would be better (and please name your params)

Rectangle(Point first = Point(0.0,1.0), Point second = Point(1.0,1.0), Point third = Point(1.0,0.0), Point fourth = Point(0.0,0.0));

BTW, looking at the comment at the end, it seems to indicate that you did not understand the goal of the project :)

This is a programming project intended to show that you know how to program functions and understand constructors..

于 2013-11-13T14:50:24.513 回答
1

下面的代码在我看来是错误的。

// default constructor
Rectangle(Point = Point(0.0,1.0), Point(1.0,1.0), /** <<<---HERE**/
    Point(1.0,0.0), Point(0.0,0.0));    /** <<<---AND HERE**/

尝试:

// default constructor
Rectangle(Point a = Point(0.0,1.0), Point b = Point(1.0,1.0),
    Point c = Point(1.0,0.0), Point d = Point(0.0,0.0));
于 2013-11-13T14:41:52.353 回答
0

This is a programming project intended to show that you know how to program functions and understand constructors..

注意you know how to...不是the stack overflow community knows how to

寻求错误信息的帮助 不要只发布所有代码并询问有什么问题

但请尝试查看一些有关构造函数和初始化的 C++ 教程

于 2013-11-13T14:56:13.180 回答