我读了几篇类似的文章,或者我不明白他们必须提供什么,或者他们似乎没有适用。我是新来的,我会尽力遵守规则。
我们将在本课程的最后两周学习c ++,并在40小时内最终学习c ::,所以我是一个初学者。我熟悉C。
这是相关任务的一部分:
Part 1 [20 points]
Reading material:
Copy constructor: https://www.geeksforgeeks.org/copy-constructor-in-cpp/
References (the & symbol): https://www.geeksforgeeks.org/references-in-c/
------------------------------------------------------------------------------------------------------
Write a class Point representing a point in 2d.
The class will have the following public methods:
// constructor - sets the coordinates to be x,y
Point( int x, int y)
// copy constructor - creates a new point with the same
coordinates
Point(Point ©)
// getters
int getX()
int getY()
// setters
void setX()
void setY()
// prints to screen the coordinates in the format (x,y)
void print()
我的实现:
点
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1);
~Point();
Point (const Point &p2);
int getX();
int getY();
void setX(int x2);
void setY(int y2);
void print();
};
点.cpp
#include "Point.hpp"
Point::Point(int x1, int y1)
{
x = x1;
y = y1;
}
Point::Point (const Point &p2)
{
x = p2.x;
y = p2.y;
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
void Point::setX(int x2)
{
x = x2;
}
void Point::setY(int y2)
{
y = y2;
}
void Point::print()
{
cout << "(" << x << "," << y << ")";
}
我坚持的问题部分
Part 2 [20 points]
Reading material:
Abstract classes and pure virtual methods:
https://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/
---------------------------------------------------------------------------------------------------
Write an abstract class GeometricShape representing an abstract geometric
shape. The class will have the following public methods:
// Constructor: gets a coordinate. The purpose of the
coordinate depends on the specific shape
GeometricShape(Point coord)
// returns the area of the shape
// returns 0 as default. To be implemented in each
concrete shape
virtual float getArea() { return 0 ; }
// returns the perimeter of the shape
// returns 0 as default. To be implemented in each
concrete shape
virtual float getPerimeter() { return 0 ; }
// virtual method. To be implemented in each concrete
method
virtual void print() = 0 ;
我的实现:
GeometricShape.hpp
#include <iostream>
#include "Point.hpp"
using namespace std;
class GeometricShape {
private:
Point point;
public:
// Parameterized Constructor
GeometricShape(Point coord);
virtual float getArea();
virtual float getPerimeter();
virtual void print() = 0;
};
GeometricShape.cpp(这不会编译)
#include <iostream>
#include "GeometricShape.hpp"
GeometricShape::GeometricShape(Point coord)
{
Point p = new Point(coord.getX(),coord.getY());
}
int main()
{
return 0;
}
在Linux Ubunto 18.04.3上编译时出现错误消息(可远程访问大学实验室,需要在实验室中的Linux上进行编译的分配)
错误信息:
gsg27@csil-cpu2:~/sfuhome/cmpt-125/5$ g++ GeometricShape.cpp
GeometricShape.cpp: In constructor ‘GeometricShape::GeometricShape(Point)’:
GeometricShape.cpp:9:11: error: conversion from ‘Point*’ to non-scalar type ‘Point’ requested
Point p = new Point(coord.getX(),coord.getY());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
最佳答案
您的直接问题是该行
Point p = new Point(coord.getX(),coord.getY());
没有道理。
new
表达式返回指向动态分配对象的指针。因此,您编写的代码应该是Point *p = new Point(coord.getX(),coord.getY());
除了您不需要指针,并且不需要动态分配的对象之外,您只是在尝试初始化数据成员。所以实际上您的构造函数应该是
GeometricShape::GeometricShape(Point coord) : point(coord) {}
因为您已经有数据成员
GeometricShape::point
,并且Point
类具有副本构造函数。写起来会更平常GeometricShape::GeometricShape(Point const &coord) : point(coord) {}
因为您不需要制作两个原始副本,但这在这里没什么大不了的。
关于c++ - 请求从“点*”转换为非标量类型“点”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59205063/