gpt4 book ai didi

c++ - 编译错误 : "pointer can only be subtracted from another pointer"

转载 作者:搜寻专家 更新时间:2023-10-31 00:37:26 24 4
gpt4 key购买 nike

刚刚学习 C++,我很享受我的第一个程序 它并不多,它只是解决了数学问题 据我所知我的逻辑是距离公式有一些问题右 sqrt((x2-x1)+(y2-y1))。但是我收到错误

error C2113: '-' : pointer can only be subtracted from another pointer which is throwing me off.

奖金问题...如果有人能告诉我一种方法,可以在用户完成先前的选择后将其引导回第一个菜单,那会很酷。我假设有某种循环,但我还没有在我的 C++ 类(class)中介绍它。

这是代码(抱歉,如果它乱七八糟,但我还没有学会正确的格式)

#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 , y1ynspecified as part of POSIX这可以解释为什么您不会在 CC++ 标准中找到此文档:

The y0(), y1(), and yn() functions shall compute Bessel functions of x of the second kind of orders 0, 1, and n, respectively.

全局命名空间污染的解决方案是 declare your variables in your own namespace .

原创:

你有 <代替 <<在你的一些cout调用,例如这里:

cout<"FORMULA L2+H2";
^

应该是:

cout<<"FORMULA L2+H2";
^^

也在 clang 中和 gcc , 我与你的全局冲突 y1和全局 y1来自 cmath header ,这使得:

cout<<"Distance ="<<sqrt((x2-x1)+(y2-y1))<<endl;
^^

还有其他几行中断,我的解决方案是重命名 y1但更好的解决方案是不使用全局变量。

关于c++ - 编译错误 : "pointer can only be subtracted from another pointer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19824557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com