gpt4 book ai didi

c++ - 请求从 ‘Point*’ 到非标量类型 ‘Point’ 的转换

转载 作者:行者123 更新时间:2023-11-27 23:35:55 28 4
gpt4 key购买 nike

我看了几个类似的帖子,但我要么不明白他们提供的是什么,要么他们似乎不适用。我是新来的,我会尽力遵守规则。

我们在类(class)的最后 2 周学习 c++,期末学习 40 小时 :),所以我是初学者。我熟悉 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 &copy)
// getters
int getX()
int getY()
// setters
void setX()
void setY()
// prints to screen the coordinates in the format (x,y)
void print()

我的实现:

点.hpp

#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 ;

我的实现:

几何形状.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++ - 请求从 ‘Point*’ 到非标量类型 ‘Point’ 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59205063/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com