can not convert 'int *' to 'Trade *' for argument '1' to 'void request data (Trade *)

1

Why is this error?

#include <iostream>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <string.h>

using namespace std;

struct Comercio{
    int codigo,cactual,cminima;
    float precio;
    string descripcion;
};
int i,n;
void pedirdatos(Comercio *v);
int *v;

int main(){

    cout<<"Ingrese cantidad de articulos: "<<endl;
    cin>>n;
    v= new Comercio[n];

    pedirdatos(v,n);

    return 0;   
}

void pedirdatos(Comercio *v){
    for(int i=0;i<n;i++){

        cout << "Ingrese codigo:" <<endl;

        cin >>v[i].codigo;
        cout<< "Ingrese descripcion: "<<endl;
        fflush(stdin);
        cin>>v[i].descripcion;
        cout<< "Cantidad actual: "<<endl;
        cin>> v[i].cactual;
        cout<< "Cantidad minima: "<<endl;
        cin>> v[i].cminima;
        cout<< "Precio:  "<<endl;
        cin>>v[i].precio;
    }
}
    
asked by Agustin Bobadilla 30.10.2018 в 21:23
source

2 answers

4

The error is clear, concise and self-explanatory:

cannot convert 'int*' to 'Comercio*' for argument '1' to 'void pedirdatos(Comercio*)'

Maybe you do not understand it because it's in English, I translate it to you:

no se puede convertir 'int*' a 'Comercio*' para el argumento '1' de 'void pedirdatos(Comercio*)

The point where you are calling pedirdatos is on this line:

pedirdatos(v,n);

Argument 1 is v , which we see is defined in this line:

int *v;

So v is type int * , what do you expect pedirdatos as the first argument ?, Let's see your statement:

void pedirdatos(Comercio *v);

The first argument of pedirdatos is of type Comercio * . And there's the error: You can not convert a pointer-to-integer data ( int * ) to a pointer-type data to Commerce.

But that is the least of your problems, your code is incorrect for many other reasons:

  • Headers <stdio.h> , <stdlib.h> , <string.h> and <ctype.h> are from not from . These headers have a version adapted to C ++ that has the prefix c and has no extension. If you really need to use the C headers (which will never be the case) you should use the C ++ equivalents <cstdio> , <cstdlib> , <cstring> and <cctype> . Read this thread to find out why.
  • Besides you include twice <stdio.h> , you are not using any of your utilities, so you would not need to include it even once. You also do not use anything of <stdlib.h> or <ctype.h> so its inclusion is also unnecessary.
  • You include the string header of C <string.h> and that of C ++ <string> but you only use the second so the first inclusion is unnecessary.
  • There is no obligation to use the using namespace std; clause since it is only an aid to the writing of code; If you decide to use this clause do not do it in the global scope, use it in the smallest possible scope. Read this thread to find out why. Avoid abusing std::endl (because it can cause performance problems) and favors the use of the explicit line break ( \n ). Read this thread to find out why.
  • Declare and define pedirdatos with a single argument, but use it with two.
  • You use dynamic memory and do not erase it. Switch to a data container.
  • Let your code breathe, add some spaces and line breaks. In the 90s we had screens of 80 characters wide and 25 lines high, by then it made sense to squeeze the code to fit on the screen. Today we have huge screens and separating things makes the code more readable.
  • The names of your variables or types are not self-explanatory; the names should let you know at a glance what they do, making the code less prone to errors and making it easier to read and understand, which any person who works with you (including your future self) will be very grateful for.

Keeping in mind the above, your code could look like:

#include <iostream>
#include <vector>

struct Comercio{
    int codigo = 0, cactual = 0, cminima = 0;
    float precio = 0f;
    std::string descripcion = "";
};

void pedirdatos(std::vector<Comercio> &comercios) {

    using namespace std;

    for (auto &comercio : comercios) {

        cout << "Ingrese codigo:\n";
        cin >> comercio.codigo;

        cout << "Ingrese descripcion: \n";
        cin >> comercio.descripcion;

        cout << "Cantidad actual: \n";
        cin >> comercio.cactual;

        cout << "Cantidad minima: \n";
        cin >> comercio.cminima;

        cout << "Precio:  \n";
        cin >> comercio.precio;
    }
}

int main() {

    using namespace std;

    int cantidad;
    cout << "Ingrese cantidad de articulos: \n";
    cin >> cantidad;

    vector<Comercio> comercios(cantidad);
    pedirdatos(comercios);

    return 0;
}
    
answered by 31.10.2018 в 08:40
1

You are assigning a different value type to the one you declared in the variable v .

The beam declared as int* .

Int *v

But here you add a value of type Comercio .

v= new Comercio[n];

You can solve it if you declare the variable in this way:

Comercio* v;
    
answered by 30.10.2018 в 21:35