编译错误:"指针只能从另一个指针中减去"
Compilation Error: "pointer can only be subtracted from another pointer"
只是学习C++,我喜欢制作我的第一个程序,它并不多,它只是解决了数学问题,据我所知,距离公式有一些问题,我的逻辑是正确的sqrt((x2-x1)+(y2-y1))
。但是我收到错误
错误 C2113:"-":指针只能从另一个让我失望的指针中减去。
奖金问题...如果有人能指出一种在用户完成先前选择后将用户引导回第一个菜单的方法,那将很酷。我假设了某种循环,但我还没有在我的C++课上涵盖它。
这是代码(对不起,如果它很乱,但我还没有学会正确的格式)
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int selection;
int choice;
int choice2;
double height;
double length;
double a;
double x;
double y;
double const pi = 3.14;
double r;
double m;
double x1;
double x2;
double y1;
double y2;
double distance;
int main(){
cout<<"-------------------------"<<endl;
cout<<" MATH DESTROYER "<<endl;
cout<<"-------------------------"<<endl;
cout<<" 1-Geometry Solver "<<endl;
cout<<" 2-Algebra Solver "<<endl;
cout<<" 3-"<<endl;
cout<<endl;
cout<<" Select Option number: "<<endl;
cin>>selection;
cout<<endl;
cout<<endl;
switch(selection){
case 1:
cout<<"-------------------------"<<endl;
cout<<" GEOMETRY SOLVER ";cout<<endl;
cout<<endl;
cout<<" 1-Perimeter of a square "<<endl;
cout<<" 2-Area of a square "<<endl;
cout<<" 3-Perimeter of a rectangle"<<endl;
cout<<" 4-Area of a rectangle "<<endl;
cout<<" 5-Perimeter of a circle "<<endl;
cout<<" 6-Area of a circle "<<endl;
cout<<" Select Option number: "<<endl;
cout<<"-------------------------"<<endl;
cin>>choice;
cout<<endl;
cout<<endl;
switch(choice){
case 1:
cout<<"-------------------------"<<endl;
cout<<" PERIMITER OF A SQUARE "<<endl;
cout<<endl;
cout<<"Enter Height"<<endl;
cin>>height;
cout<<"Enter Length"<<endl;
cin>>length;
cout<<"Perimiter of Square ="<<(height*2)+(length*2)<<endl;
cout<<"-------------------------"<<endl;
cout<<endl;
cout<<endl;
system("pause");
return 0;
case 2:
cout<<"AREA OF A SQUARE"<<endl;
cout<<endl;
cout<<"Enter Length"<<endl;
cin>>length;
cout<<endl;
cout<<endl;
cout<<"FORMULA L^2";
cout<<endl;
cout<<endl;
cout<<"AREA of Square ="<<pow(length,2)<<endl;//(pow)(length,2)= length to the power of 2 or lenght^2
cout<<endl;
cout<<endl;
system("pause");
return 0;
case 3:
cout<<"PERIMITER OF A RECTANGLE"<<endl;
cout<<endl;
cout<<"Enter Height"<<endl;
cin>>height;
cout<<"Enter Length"<<endl;
cin>>length;
cout<<endl;
cout<<endl;
cout<"FORMULA L2+H2";
cout<<endl;
cout<<endl;
cout<<"Perimiter of Rectangle ="<<(height*2)+(length*2)<<endl;
cout<<endl;
cout<<endl;
system("pause");
return 0;
case 4:
cout<<"AREA OF A RECTANGLE"<<endl;
cout<<endl;
cout<<"Enter Height"<<endl;
cin>>height;
cout<<"Enter Length"<<endl;
cin>>length;
cout<<endl;
cout<<endl;
cout<"FORMULA L2+H2";
cout<<endl;
cout<<endl;
cout<<"Perimiter of Rectangle =" <<height*length<<endl;
cout<<endl;
cout<<endl;
system("pause");
return 0;
case 5:
cout<<"PERIMITER OF A CIRCLE"<<endl;
cout<<endl;
cout<<"Enter Radius"<<endl;
cin>>r;
cout<<endl;
cout<<endl;
cout<<"FORMULA peremiter=PI X D";
cout<<endl;
cout<<endl;
cout<<"Perimiter of circle ="<<pi*(r*2)<<endl;
cout<<endl;
cout<<endl;
system("pause");
return 0;
case 6:
cout<<"AREA OF A CIRCLE"<<endl;
cout<<endl;
cout<<"Enter Radius"<<endl;
cin>>r;
cout<<endl;
cout<<endl;
cout<<"FORMULA PI X R^2";
cout<<endl;
cout<<endl;
cout<<"Area of Circle ="<<pi*(pow(r,2))<<endl;
cout<<endl;
cout<<endl;
system("pause");
return 0;
}
case 2:
cout<<" ALGEBRA MASTER "
cout<<"-------------------------"<<endl;
cout<<" 1-Distance Formula "<<endl;
cout<<" 2-Slope "<<endl;
cout<<" 3-Pythagorean Theorm "<<endl;
cout<<"-------------------------"<<endl;
cout<<endl;
cout<<"Select Option number:"<<endl;
cin>>choice2;
switch(choice2){
case 1:
cout<<"Distance Formula"<<endl;
cout<<endl;
cout<<"Enter first y point (y1)"<<endl;
cin>>y1;
cout<<endl;
cout<<"Enter second y point (y2)"<<endl;
cin>>y2;
cout<<endl;
cout<<"Enter first x point (x1)"<<endl;
cin>>x1;
cout<<endl;
cout<<"Enter second x point (x2)"<<endl;
cin>>x2;
cout<<"D=sqrt (x2-x1)+(y2-y1)";
cout<<endl;
cout<<endl;
cout<<"Distance ="<<sqrt((x2-x1)+(y2-y1))<<endl;
cout<<endl;
cout<<endl;
system("pause");
return 0;
case 2:
cout<<"AREA OF A CIRCLE"<<endl;
cout<<endl;
cout<<"Enter Radius"<<endl;
cin>>r;
cout<<endl;
cout<<endl;
cout<<"FORMULA PI X R^2";
cout<<endl;
cout<<endl;
cout<<"Area of Circle ="<<pi*(pow(r,2))<<endl;
cout<<endl;
cout<<endl;
system("pause");
return 0;
case 3:
cout<<"AREA OF A CIRCLE"<<endl;
cout<<endl;
cout<<"Enter Radius"<<endl;
cin>>r;
cout<<endl;
cout<<endl;
cout<<"FORMULA PI X R^2";
cout<<endl;
cout<<endl;
cout<<"Area of Circle ="<<pi*(pow(r,2))<<endl;
cout<<endl;
cout<<endl;
system("pause");
return 0;
}
}
}
更新:
看起来y0
、y1
和yn
被指定为 POSIX 的一部分,这可以解释为什么你不会在 C 或 C++ 标准中找到它:
y0()、y1() 和 yn() 函数应分别计算第二类阶 0、1 和 n 的 x 的贝塞尔函数。
全局命名空间污染的解决方案是在自己的命名空间中声明变量。
原文:
在一些cout
调用中,您有<
代替<<
,例如:
cout<"FORMULA L2+H2";
^
应该是:
cout<<"FORMULA L2+H2";
^^
同样在 clang
和 gcc
中,我与您的全局y1
发生冲突,并且来自 cmath
标头的全局y1
,这使得:
cout<<"Distance ="<<sqrt((x2-x1)+(y2-y1))<<endl;
^^
和其他几行中断,我的解决方案是重命名y1
但更好的解决方案是不使用全局变量。
我认为主要问题是您的全局变量y1
可能与 math.h
(或 cmath
)中定义的函数冲突。您可以通过在 main()
函数中本地声明变量而不是全局变量来解决此问题。这允许变量名称屏蔽现有函数,并避免冲突。
编辑:替代解决方案是将y1
重命名为其他名称,例如 Y1
(大写)。
代码中还有其他几个问题。在两个地方,你有cout<
而不是cout <<
,而且你还缺少一个分号。
相关文章:
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将指针从一个void函数传递到另一个C++
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 将指针分配给另一个指针时会发生什么情况?
- 将指向给定子类的指针从一个向量复制到另一个向量
- C++将一个指针分配给另一个指针时执行的类型检查
- 将一个类的方法指针存储到另一个类中
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- 初始化类中的指针数组,并在另一个类中检索它
- 从另一个指针指向函数的指针
- 使用指向成员函数的指针在另一个类中调用类构造函数
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 无法将指向类的成员函数的函数指针作为参数传递给同一类的另一个成员函数
- 无法"this"指针传递到另一个类并在 CPP 中调用该类的任何方法
- 如果类"A"具有指向另一个类的指针,则"B" A 可以访问 B 类公共方法
- 如何使用指针从另一个类访问属性
- 如何从另一个嵌套类中调用某个封闭类的嵌套类的函数指针成员的值?
- C++ 无效双指针干扰另一个
- 两个抽象类,派生自同一个基类.如何访问从一个抽象类到另一个抽象类的指针