编译错误:需要指针而不是对象

发布于 2024-12-20 08:40:09 字数 1040 浏览 0 评论 0原文

在制作两个对象的数组时,我收到以下错误:Edge 和 Box。

error: conversion from 'const Edge*' to non-scalar type 'Edge' requested.

我希望返回一组边。

在此头文件上:

class Box
{
private:
    bool playerOwned; 
    bool computerOwned;
    Edge boxEdges[4];
    int openEdges;
    bool full;

public:
    Box();
    Box(int x, int y);
    void setEdges(Edge boxTop, Edge boxBottom, Edge boxLeft, Edge boxRight);
    void addEdgeToBox(Edge edge); //add edge to edgeArray.
    void setPlayerOwned(bool point);
    Edge getBoxEdges() const {return boxEdges;}                ****//Error****
    bool getPlayerOwned() const {return playerOwned;}
    void setComputerOwned(bool point);
    bool getComputerOwned()const {return computerOwned;}
    int getOpenEdges() const {return openEdges;}
    bool isFull()const {return full;}

};
std::ostream& operator<< (std::ostream& out, Box box);

除了在尝试创建 Box 的非头文件中的下一行将“Edge”替换为“Box”之外,我遇到了相同的错误。

  Box box = new Box(x+i,y);

I am getting the following errors when making Arrays of two obecjts.. Edge and Box.

error: conversion from 'const Edge*' to non-scalar type 'Edge' requested.

I am hoping to return an array of Edges.

On this header file:

class Box
{
private:
    bool playerOwned; 
    bool computerOwned;
    Edge boxEdges[4];
    int openEdges;
    bool full;

public:
    Box();
    Box(int x, int y);
    void setEdges(Edge boxTop, Edge boxBottom, Edge boxLeft, Edge boxRight);
    void addEdgeToBox(Edge edge); //add edge to edgeArray.
    void setPlayerOwned(bool point);
    Edge getBoxEdges() const {return boxEdges;}                ****//Error****
    bool getPlayerOwned() const {return playerOwned;}
    void setComputerOwned(bool point);
    bool getComputerOwned()const {return computerOwned;}
    int getOpenEdges() const {return openEdges;}
    bool isFull()const {return full;}

};
std::ostream& operator<< (std::ostream& out, Box box);

I get the same error except replace 'Edge' with 'Box' on the following line in a non-header file attempting to create a Box.

  Box box = new Box(x+i,y);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2024-12-27 08:40:09
Box box = new Box(x+i,y);  //error

这里有一个错误。你应该这样写:

Box *box = new Box(x+i,y); //ok

这是因为当你使用new时,你正在分配内存,并且只有指针才能保存内存,所以box必须是指针类型。

类似地,

Edge getBoxEdges() const {return boxEdges;}  //error

应该写成:

const Edge* getBoxEdges() const {return boxEdges;}  //ok

这是因为boxEdges是一个数组,它可以衰减为指向其第一个元素的指针类型,并且由于它是const成员函数,所以boxEdges将衰减为const Edge*


顺便说一句,在第一种情况下,您可以使用自动对象,而不是第一种情况下的指针:

Box box(x+i, y); //ok

我建议您将运算符<<的第二个参数设置为常量引用:

//std::ostream& operator<< (std::ostream& out, Box box); //don't use this
std::ostream& operator<< (std::ostream& out, Box const & box); //use this

这避免了不必要的复制!

Box box = new Box(x+i,y);  //error

One error is right here. You should write this as:

Box *box = new Box(x+i,y); //ok

It is because when you use new, you're allocating memory, and only a pointer can hold a memory, so box has to be pointer type.

Similarly,

Edge getBoxEdges() const {return boxEdges;}  //error

should be written as:

const Edge* getBoxEdges() const {return boxEdges;}  //ok

It is because boxEdges is an array, which can decay into pointer type to its first element, and since it is const member function, boxEdges will decay into const Edge*.


By the way, instead of pointer in the first case, you use automatic object as:

Box box(x+i, y); //ok

I would suggest you to make the second parameter of operator<< a const reference:

//std::ostream& operator<< (std::ostream& out, Box box); //don't use this
std::ostream& operator<< (std::ostream& out, Box const & box); //use this

This avoids unnecessary copy!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文