خطأ في التجميع: "لا يمكن طرح المؤشر إلا من مؤشر آخر"

StackOverflow https://stackoverflow.com/questions/19824557

  •  05-07-2022
  •  | 
  •  

سؤال

مجرد تعلم 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;

    }


    }
}
هل كانت مفيدة؟

المحلول 2

أعتقد أن المشكلة الرئيسية هي أن متغيرك العالمي y1 قد تكون متضاربة مع وظيفة محددة في math.h (أو cmath). يمكنك الالتفاف على ذلك بإعلان متغيراتك محليًا في main() وظيفة ، بدلا من الكرات. يسمح ذلك للاسم المتغير بإخفاء الوظيفة الحالية ، وتجنب الصراع.

تحرير: الحل البديل هو إعادة تسمية y1 لشيء آخر ، على سبيل المثال Y1 (الأحرف الكبيرة).

هناك أيضًا بعض المشكلات الأخرى في الكود. في مكانين حصلت عليهما cout< بدلاً من cout <<, ، وأنت أيضًا تفتقد إلى نصف كولون.

نصائح أخرى

تحديث:

يبدو y0, y1 و yn نكون محدد كجزء من POSIX مما يفسر سبب عدم العثور على هذا الموثق في ج أو C ++ المعايير:

يجب أن تحسب وظائف Y0 () و y1 () و yn () وظائف bessel من x من النوع الثاني من الطلبات 0 و 1 و n ، على التوالي.

الحل ل تلوث مساحة الأسماء العالمية هو ل أعلن متغيراتك في مساحة الاسم الخاصة بك.

إبداعي:

عندك < بدلاً من << في عدد قليل من cout المكالمات ، على سبيل المثال هنا:

cout<"FORMULA L2+H2";
    ^

يجب ان يكون:

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

ايضا في clang و gcc, ، أنا أتضاعف مع عالمك y1 وعالمية y1 من cmath رأس ، مما يجعل هذا:

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

وبعض الخطوط الأخرى تنكسر ، كان حلي هو إعادة تسمية y1 لكن الحل الأفضل هو عدم استخدام Globals.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top