将数组指针传递到函数时出错
Error when passing array pointer into a function
我正在这个项目上练习我的面向对象编程,但在 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]);
显然,这与声明或函数的定义都不匹配。
编译器认为您有两个不同的函数,一个采用指针,另一个采用指向指针数组的指针。两者都与智能指针不匹配。
您已将显示函数参数声明为指针,但在定义它时,您正在传递指针数组。
相关文章:
- 类中私有结构的构造函数出错
- 字符串验证函数出错
- 我的类默认构造函数出错
- Dev-C++和Code::Blocks中的析构函数出错
- int对的哈希函数出错
- MSVC 编译器错误导致迭代器和友元函数出错
- 复制构造函数出错
- 使用 WinSock2 开发,使用 CreateThread() 函数出错
- 链接列表的Push_back函数出错
- 结构初始化中的隐式复制构造函数出错
- 异步函数出错
- 编译器错误消息错误:令牌之前的预期')' '*'。我的构造函数出错
- c++中使用std::bind和std::函数出错
- 在vector排序中使用c++模板函数出错
- 使用std::for_each lambda函数出错
- 二叉搜索树插入函数出错
- 调用类的函数出错
- c++使用删除函数出错
- 使用已删除的函数出错
- 调用构造函数出错