如何使用指针打印返回函数输出的地址[重复]

问题描述 投票:0回答:0

我正在尝试打印输出值的地址,但我不断收到错误:第 54 行的一元“&”操作数需要左值。我不完全确定如何创建一个允许我打印的指针变量地址。代码在没有 & 的情况下工作正常,但我需要在代码中的某些点使用指针。

#include <iostream>

using namespace std;
struct Triangle{//struct called Traingle created
    int side =0;
    int side1= 0;
    int side2= 0;
    int height= 0;
    int base= 0;
   //variables are classed inside the construct
    int perimeter(){//function created and is defined
        return side+side1+side2;//adds value to the predefined variable
            }
    int area1(){//function created and is defined
        return  base*height/2; //calculation for area
    }
};
int main()
//main function of the code
{
   struct Triangle triangle = Triangle();
    int options;//defines the variable options
    cout<<"Choose one of the following options:" <<"\n" <<"1.Calculate the perimeter of a Traingle"<<"\n"<< "2.Calculate the area of a traingle:"<<"\n";//Shows menu to the user
    cin>>options;
    //adds the input to the variable
    if(options==1){
    //if statement to check what the user input from the menu option
    //struct declaration
    cout<<"Enter the lengths of the three sides: ";
    //asks user for the three sides of the traingle
    cin>>triangle.side;
    // adds the first input to this variable
    cin>>triangle.side1;
    //secound number user enters is added to this variable
    cin>>triangle.side2;
    //third number user enters is added to this variable

      }
     cout<<"The Perimeter of a Triangle with sides"<<"<"<<triangle.side<<">"<<"<"<<triangle.side1<<">"<<"<"<<triangle.side2<<">"<<"is"<<"<"<<triangle.perimeter()<<">";
        //prints the perimeter of the triangle after calculations 
     

    if(options==2){
        cout<<"Enter the base and the height of the triangle: ";
        //asks the user for an input for the height and base
        cin>>triangle.base;
        //adds the first input to this variable
        cin>>triangle.height;
        //adds the second input to this variable
       
        cout<<"The area of a Traingle with a base of"<<"<"<<triangle.base<<">"<<"and height of"<<"<"<<triangle.height<<">"<<"is"<<"<"<<triangle.area1()<<">";
            //prints the area of the triangle after calculations
       cout<<"The area of a Traingle with a base of"<<"<"<<triangle.base<<">"<<"and height of"<<"<"<<triangle.height<<">"<<"is"<<"<"<<&triangle.area1()<<">";
            //prints the area of the triangle after calculations
       
        }
    return 0;
}
c++ pointers
© www.soinside.com 2019 - 2024. All rights reserved.