将数组指针传递到函数时出错

Error when passing array pointer into a function

本文关键字:函数 出错 数组 指针      更新时间:2023-10-16

我正在这个项目上练习我的面向对象编程,但在 main.cpp 结束时尝试调用我的 display() 函数时遇到了问题。我创建了一个名为 rectangles 的 Rectangle 对象,它是一个使用指针的数组。

基本上我要做的是将我的rectangles数组传递到 display() 函数中,以便我可以从那里打印,但我收到错误:

  • 无效参数 ' 候选人是: void display(Rectangle *) void display(Rectangle * *) '
  • 调用"显示"没有匹配功能

我已经尝试解决此问题几个小时了,但似乎无法使其正常工作。如果这是一个简单的错误,请道歉,但我希望对如何使其工作提供一些意见。

"主.cpp"

#include "Rectangle.h"
#include <iostream>
#include <iomanip>
#include <memory>
#include <ctime> //for the time function
#include <cstdlib> //for rand and srand
using namespace std;
const int ARRAY_SIZE = 21, //size of the array
          MIN_VALUE = 1, //min value for random numbers
          MAX_VALUE = 7; //max value for random numbers
void display(Rectangle*); //prototype for display function
int main() {
    unique_ptr<Rectangle> rectangles(new Rectangle[ARRAY_SIZE]);
    double rectWidth;   // local variable for width
    double rectLength;  // local variable for length
    //for random numbers:
    unsigned seed = time(0);
    srand(seed);
    //inputs random numbers into rectWidth and rectLength
    for(int i = 0; i < ARRAY_SIZE; i ++)
    {
        rectWidth = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE; //sets random number to Width
        rectLength = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE; //sets random number to Length
        rectangles->setWidth(rectWidth); //inputs Width to the setWidth function
        rectangles->setLength(rectLength); //inputs Length to the setLengh function
    }
    display(rectangles);
    return 0;
}

矩形.cpp

#include "Rectangle.h"
#include <iostream>
#include<iomanip>
using namespace std;
Rectangle::Rectangle(double w, double len)   //constructor
{
    width = 0.0;
    length = 0.0;
}
Rectangle::~Rectangle()    //destructor
{
}
//******************************************************************
// setWidth assigns a value to the width member.
//**********************************************************************
void Rectangle::setWidth(double w)
{
    width = w;
}
//*********************************************************************
// setLength assigns a value to the length member.
//*********************************************************************
void Rectangle::setLength(double len)
{
    length = len;
}
//********************************************************************
// getWidth returns the value in the width member.
//********************************************************************
double Rectangle::getWidth() const
{
    return width;
}
//***********************************************************************
// getLength returns the value in the length member.
//***********************************************************************
double Rectangle::getLength() const
{
    return length;
}
//********************************************************************
//display will print out a report of the rectangles lable, hight, and width
//********************************************************************
void display(Rectangle* r[])
{
    const int ARRAY_SIZE = 21;
    Rectangle *rectangles[ARRAY_SIZE];
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        cout << "Squarenn";
        cout << "================nn";
        cout << "Label  Width   Hightn";
        cout <<"R" << setfill('0') << setw(4) << i+ 1 << "  ";
        cout << rectangles[i]->getWidth() << "  ";
        cout << rectangles[i]->getLength() <<endl;
        delete rectangles[i]; //to delete the pointer
    }
}

你有

void display(Rectangle*); //prototype for display function

并将其实现为

void display(Rectangle* r[])

但是你用这个变量来调用它

unique_ptr<Rectangle> rectangles(new Rectangle[ARRAY_SIZE]);

显然,这与声明或函数的定义都不匹配。

编译器认为您有两个不同的函数,一个采用指针,另一个采用指向指针数组的指针。两者都与智能指针不匹配。

您已将显示函数参数声明为指针,但在定义它时,您正在传递指针数组。