Pointers with methods, where am I wrong?

Asked

Viewed 437 times

4

Creating a class that will have two methods one assign another to print vectors with public and then call this method no main.

I was able to make it this far with the method baby Steps. Where I’m going wrong and what I have to do?

#include <iostream>
using namespace std;


class vetor
{
 int v, tamanho,vetores;

 public:
 void atribuir(int *v, int tamanho){
    for (int i=0; i<tamanho; i++) {
        cout << "Digite um numero: ";
        cin >> v[i];
    }
 }

 void mostrar(void)
 {
    for (int i =0; i<tamanho; i++) {
        cout << "O numero "<< i +1 << " inserido for: "
        <<vetores[&i]<<endl;
     }
 }
};

int main(){
int tamanhos;
 //    vetor *varios_vetor;
//    varios_vetor = new vetor[tamanho];

cout << "Digite o tamanho do vetor: ";
cin >> tamanhos;
cout <<"Tamanho do vetor: "<< tamanhos << endl;

//vetor::atribuir(int i, int tamanho)
//vetor *varios_vetor;
//varios_vetor = new vetor[tamanhos];

//varios_vetor[tamanhos].atribuir(int *v, int tamanhos);


return 0;
}
  • You have declared v (and vectors) as an int variable and not as an int pointer or array.

  • How so ta face talking int v; that is not vector ?

  • No, int v is not a vector is just an int variable. Maybe you mean int v[] or int *v. Or maybe you’re thinking of using the vector class.

1 answer

4


Since the intention is to initialize the vector and then display it, you could simply pass the desired values to the class, not needing any parameters in the declaration of your method assign():

#include <iostream.h>

using namespace std;

class Vetor
{
      int *vetor, tamanho; //Variáveis privadas que são acessadas pelos métodos da classe.

      public:
      Vetor(int t, int *v) //Construtor da classe.
      {
           vetor = v;      
           tamanho = t;            
      }              

      void atribuir(){
           for (int i = 0; i < tamanho; i++){
               cout << "Digite um numero: ";
               cin >> vetor[i];
           }
      }

      void mostrar(void){
           for (int i = 0; i < tamanho; i++) {
               cout << "O numero " << i + 1 << " inserido foi: " << vetor[i] <<endl;
           }
      }
};

int main(){

    int tamanho;

    cout << "Digite o tamanho do vetor: ";
    cin >> tamanho;
    cout << "Tamanho do vetor: " << tamanho << endl;

    int vetorDeInteiros[tamanho]; //Vetor com o tamanho especificado.

    Vetor objVetor(tamanho, vetorDeInteiros); //Instanciando a classe Vetor e passando as variáveis declaradas para seu construtor.

    objVetor.atribuir();
    objVetor.mostrar();

    system("pause");
    return 0;
}
  • It was time to call the obj who was not understanding! Said thank you very much! Gave a light ! Thank you very much!!

  • @user6361 If the answer helped you, mark as accepted answer. So everyone who has the same problem will know how you solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.