目标c-在c++中用指针在类中输入数据
objective c - inputting data with pointers in class in c++
我想写一段代码(使用指针)来获取矩形的两侧并计算其周长和面积,但它不起作用代码来了:
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
class PAndArea{
private:
int x, y;
void getxy(int *,int *);
int area(int *, int *);
int perimeter(int *, int *);
public:
void showperimeterandarea(int *, int *);
};
void PAndArea::showperimeterandarea(int *w, int *e)
{
getxy(*w, *e);
cout << "area:"<<area(*w, *e)<< "<br>";
cout << "perimeter:" << perimeter(*w, *e);
}
void PAndArea::getxy(int *x, int*y)
{
cout << "enter two numbers:";
cin >> *x >> *y;
}
int PAndArea::area(int *x,int *y)
{
return (*x * *y);
}
int PAndArea::perimeter(int *x, int *y)
{
return((*x * 2) + (*y * 2));
}
int main(){
int x, y;
PAndArea i;
i.showperimeterandarea(&x, &y);
_getch();
return 0;
}
您正在将int传递给一个接受int指针的函数。
int area(int *, int *);
//...
area(*w, *e);
w和e是int*。当您在调用中取消引用它们时,a*w和*e是int(而不是指针)。
可以将area函数更改为int,也可以将指针传递到area。
int area(int *, int *);
//...
area(w, e);
// or
int area(int, int);
//...
area(*w, *e);
您在其他调用中也会犯同样的错误。例如:
void PAndArea::showperimeterandarea(int *w, int *e)
{
// getxy(*w, *e); // you are passing int's here, not pointers. This is wrong
getxy(w, e); // correction
相关文章:
- 我的程序没有从文件中读取并输入数据
- 输入数据成员未按要求工作
- 使用 pybind11 调用 Python 函数时避免复制输入数据
- 使用 python 或 c ++ 中的 sql 根据输入数据输出输出示例
- c++用输入数据创建数组
- Caffe 或 Caffe2 可以直接从 GPU 获得输入数据吗?
- C++ QTextDocument::contentChange() 信号提供无效的输入数据
- boost::asio 以async_read复制输入数据
- 输入数据类型检查循环未按预期工作 (C++)
- SVM 训练时间是否取决于输入数据的内容?
- 将用户输入数据相加
- 在线评委如何传递输入数据?
- 如何传递私有地图和输入数据
- C++ 将用户输入数据用于多个函数
- 通过按空格键结束键盘输入数据,而不是输入键 c/c++
- 如何为作为雇员类的指针传入的构造函数输入数据
- C++:检查输入数据
- 关于输入数据
- 防止在管道中输入数据时退出
- C/C++中固定长度实输入数据的高效二维FFT