首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >请求将“Point*”转换为非标量类型的“Point”

请求将“Point*”转换为非标量类型的“Point”
EN

Stack Overflow用户
提问于 2019-12-06 00:12:57
回答 2查看 893关注 0票数 0

我读过几篇类似的文章,我要么不明白它们能提供什么,要么它们似乎不适用。我是新来的,尽我最大努力遵守规则。

我们在课程的最后两周学习c++,40小时后进入期末考试:),所以我是个初学者。我对C很熟悉。

以下是任务中与此相关的部分:

代码语言:javascript
复制
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()

我的执行情况:

Point.hpp

代码语言:javascript
复制
#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();
};

Point.cpp

代码语言:javascript
复制
#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 << ")";
    }

--问题的一部分--我被困在上了

代码语言:javascript
复制
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

代码语言:javascript
复制
#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 (,这是不会编译的东西)

代码语言:javascript
复制
#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 (远程访问的大学实验室,在实验室中编译所需的任务)上编译时的错误消息

错误消息:

代码语言:javascript
复制
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());
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EN

回答 2

Stack Overflow用户

发布于 2019-12-06 00:21:59

你眼前的问题是

代码语言:javascript
复制
Point p = new Point(coord.getX(),coord.getY());

没有道理。new表达式返回指向动态分配对象的指针。所以,你写的代码应该是

代码语言:javascript
复制
Point *p = new Point(coord.getX(),coord.getY());

但是,您不需要指针,也不需要动态分配的对象:您只是尝试初始化一个数据成员。所以你的构造函数应该是

代码语言:javascript
复制
GeometricShape::GeometricShape(Point coord) : point(coord) {}

因为您已经拥有数据成员GeometricShape::point,并且Point类有一个副本构造函数。这样写起来会更平常

代码语言:javascript
复制
GeometricShape::GeometricShape(Point const &coord) : point(coord) {}

因为你不需要复制两份原件,但这里没什么大不了的。

票数 3
EN

Stack Overflow用户

发布于 2019-12-06 00:37:27

在接受了上述所有帮助并咨询了我的教授之后:

它应该按以下方式实现,并且工作正常

代码语言:javascript
复制
#include <iostream>
#include "Point.hpp"

using namespace std;

class GeometricShape { 

private:
    Point point;
    float area;
    float perimeter;

public: 
    // Parameterized Constructor 
    GeometricShape(Point coord) :  point(coord.getX(), coord.getY())  // initialize the data field
    {
      area = 0;
      perimeter = 0;
    }

    virtual float getArea();

    virtual float getPerimeter(); 

    virtual void print() = 0;
}; 

注意,因为它是一个抽象类,所以作为初学者,我觉得让所有的一个文件都不是hpp和cpp文件不那么让人费解。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59205063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档