Problems in #include C ++ [closed]

1

then when I want to compile the program I get an error in both #include iostream and #include sstream, (I put it here because in the code that I paste it does not show it) it gives me the error of 'In file from'.

#include <iostream>
#include<sstream>

using namespace std;


typedef struct{

int num;
int cont;
int suma;
stringstream ss;

}punto;

punto funcion1();
/*
 * 
 */
int main(int argc, char** argv) {

   //Se crea variable de tipo 'punto'
    punto datos;

   //Se define el valor de la variable coordenadaA mediate el retorno de la funcion PedirPunto()
    datos = funcion1();

    //se trabaja con los valores de la estructura.
    cout<<"suma: "<<datos.suma<<endl;
    cout<<"numeros: "<<datos.num<<endl;
    cout<<"veces: "<<datos.cont<<endl;

    return 0;
}

punto funcion1()
{

    punto valor;
    cout<<"Ingrese el número: ";cin>>valor.num;
    valor.cont = 0;
    valor.suma = 0;
    while (valor.num > 0)
    {
        valor.suma = valor.suma + valor.num;
        valor.cont++;
        valor.ss<<valor.num<<",";
        cout<<"Ingrese otro número: ";cin>>valor.num;          
    }
    return valor;
}
    
asked by CheleJavier 24.09.2017 в 21:46
source

1 answer

0

The problem is really in this line of your code:

datos = funcion1();

You are assigning to a statically declared structure the value returned by a function. As a general rule, each of the values is copied and if one is a class the assignment operator is called ( = ), but the error that is giving you is that it is private:

/usr/include/c++/4.8/bits/ios_base.h:795:5: error:
  ‘std::ios_base& std::ios_base::operator=(const std::ios_base&)’ is private
     operator=(const ios_base&);
     ^

And here it says that the first time you need that operator is in the assignment that I indicated:

pr.cpp: In function ‘int main(int, char**)’:
pr.cpp:27:11: note:
  synthesized method ‘punto& punto::operator=(const punto&)’ first required here 
     datos = funcion1();
           ^

So to avoid triggering the assignment operator you should pass as a parameter the data you want to modify instead of returning it as a value:

void funcion1(punto *);

The call would change to a parameter step by reference:

funcion1(&datos);

In this way the function changes in its definition and access to the data would be done with -> and not with . :

void funcion1(punto *valor)
{

    cout<<"Ingrese el número: ";cin>>valor->num;
    valor->cont = 0;
    valor->suma = 0;
    while (valor->num > 0)
    {
        valor->suma = valor->suma + valor->num;
        valor->cont++;
        valor->ss << valor->num << ",";
        cout << "Ingrese otro número: ";
        cin >> valor->num;
    }
}

The final complete code would be:

#include<iostream>
#include<sstream>

using namespace std;

typedef struct {
  int num;
  int cont;
  int suma;
  std::stringstream ss;
} punto;

void funcion1(punto *);
/*
 * 
 */
int main(int argc, char** argv) {

   //Se crea variable de tipo 'punto'
    punto datos;

   //Se define el valor de la variable coordenadaA mediate el retorno de la funcion PedirPunto()
    funcion1(&datos);

    //se trabaja con los valores de la estructura.
    cout << "suma: " << datos.suma << endl;
    cout << "numeros: " << datos.num << endl;
    cout << "veces: " << datos.cont << endl;

    return 0;
}

void funcion1(punto *valor)
{

    cout << "Ingrese el número: ";
    cin >> valor->num;
    valor->cont = 0;
    valor->suma = 0;
    while (valor->num > 0)
    {
        valor->suma = valor->suma + valor->num;
        valor->cont++;
        valor->ss << valor->num << ",";
        cout << "Ingrese otro número: ";
        cin >> valor->num;
    }
}
    
answered by 25.09.2017 в 09:00