Home > Net >  error: conversion from 'double' to non-scalar type '' requested" struct in
error: conversion from 'double' to non-scalar type '' requested" struct in

Time:11-16

I want to change one of attribute in my simple struct. I can't change anything in main function. But the compiler giving me error about scalar type - what does it exactly mean and what do i wrong?

#include <iostream>

using namespace std;

struct Number{
    int a;
    double b;
};

double zmiana(Number *number,double scale){
    number->a*=scale;
    return number->a;
}

int main()
{
    Number number1={2,3.14};
    Number number2=zmiana(&number1,2.);
    cout<<&number2;
    return 0;
}

Expected output: 4 3.14

CodePudding user response:

Here is the potential fix:

#include <iostream>


struct Number
{
    int a;
    double b;
};

double zmiana( Number* number, double scale )
{
    number->a *= scale;

    return number->a;
}

int main()
{
    Number number1 = { 2, 3.14 };
    Number number2;
    number2.b = zmiana( &number1, 2. );
    std::cout << &number2; // Notice that what you do here is printing a pointer's value

    return 0;
}
  • Related