【问题标题】:Compilation Error: "pointer can only be subtracted from another pointer"编译错误:“指针只能从另一个指针中减去”
【发布时间】:2013-11-06 22:53:26
【问题描述】:

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

错误 C2113: '-' : 指针只能从另一个让我失望的指针中减去。

BONUS 问题...如果有人可以指出我在用户完成之前的选择后将其引导回第一个菜单的方法,那将是很酷的。我假设有某种循环,但我还没有在我的 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;

    }


    }
}

【问题讨论】:

    标签: c++ posix math.h cmath


    【解决方案1】:

    更新

    看起来 y0y1ynspecified as part of POSIX,这可以解释为什么您在 CC++ 中找不到此文档> 标准:

    y0()、y1() 和 yn() 函数分别计算 x 的二阶 0、1 和 n 的 Bessel 函数。

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

    原文

    在您的几个cout 调用中,您使用&lt; 代替&lt;&lt;,例如这里:

    cout<"FORMULA L2+H2";
        ^
    

    应该是:

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

    同样在clanggcc 中,我与cmath 标头中的全局y1 和全局y1 发生冲突,这使得:

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

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

    【讨论】:

      【解决方案2】:

      我认为主要问题是您的全局变量y1 可能与math.h(或cmath)中定义的函数冲突。您可以通过在 main() 函数中本地声明变量而不是全局变量来解决这个问题。这允许变量名屏蔽现有函数,并避免冲突。

      编辑:替代解决方案是将 y1 重命名为其他名称,例如Y1(大写)。

      代码中还有一些其他问题。在两个地方你得到了cout&lt; 而不是cout &lt;&lt;,而且你还缺少一个分号。

      【讨论】:

      • 什么C函数被命名为y1
      • @PeterR.Bloomfield:显然,这是一个“IBM XL C/C++ for z/VM”扩展;在 ISO/IEC 9899:1999 中没有提到任何称为 y1 的东西。不过,如果这个人正在使用上述扩展名,那就够公平了。
      • 是的,double 可以处理底片。我认为您的方程式中的错误是您没有对 X 和 Y 项进行平方。它应该类似于:sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
      • @LightnessRacesinOrbit 我知道。我认为它是非标准的或已弃用的,但它肯定在 math.h 的某些主流版本中(尽管它可能大部分时间都是有条件地编译出来的)。
      • @LightnessRacesinOrbit 它是由POSIX指定的,这就是为什么你在标准中找不到它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      相关资源
      最近更新 更多