簡體   English   中英

將數組指針傳遞給函數時出錯

[英]Error when passing array pointer into a function

我正在這個項目上練習我的面向對象編程,但是在嘗試在main.cpp末尾調用display()函數時遇到問題。 我創建了一個名為rectangles的Rectangle對象,該對象是使用指針的數組。

基本上我想做的就是將我的rectangles數組傳遞給display()函數,這樣我就可以從那里打印了,但是我得到了錯誤:

  • 無效的參數'候選者為: void display(Rectangle *) void display(Rectangle * *) '
  • 沒有匹配的函數來調用“顯示”

我已經嘗試修復了幾個小時,但似乎無法正常工作。 抱歉,這是一個簡單的錯誤,但我希望您提供一些有關如何使其正常工作的信息。

“ main.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;
}

矩形

#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 << "Square\n\n";
        cout << "================\n\n";
        cout << "Label  Width   Hight\n";
        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]);

顯然,這與函數的聲明或定義都不匹配。

編譯器認為你有兩個不同的功能,一個采用指針和一個 取指針數組 指針數組。 都不匹配智能指針。

您已經將顯示函數參數聲明為指針,但是在定義它時,您正在傳遞指針數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM