Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem creating an array of "class"

Problem creating an array of "class"

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelp
27 Posts 4 Posters 74 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Roberto64_Ge
    wrote on last edited by
    #1

    Hi I'm getting problem running the sw I attach here below. I am trying to make an array of a certain class. In the header I have the class then a .cpp with public methods. In the main I'm doing this:

    #include "Anagrafica.h"
    #include
    #include
    #include
    using namespace std;

    void cod (anagrafica arr,string n, string c,int chiave)
    { arr.codifica(n,c,chiave); }

    void decod (anagrafica arr)
    { arr.decodifica(); }

    int main()
    {
    ifstream file;
    file.open("input.txt");
    int N;
    string n,c;

    int chiave;
    int chiave_input;
    file>>N;
    cout<>chiave;
    file>>n;
    file>>c;
    //Arr_[i].setChiave(chiave);
    //cod(Arr_[i],n,c,chiave);
    }
    for (int i=0; i>chiave_input;

    }

    here is header

    #include
    #include
    using namespace std;

    class anagrafica
    {
    private:

    string nome_cifrato;
    string cognome_cifrato;
    int chiave_cifratura;

    public :

    anagrafica(string ="", string ="", int=0); //costruttore

    void setNome(string );

    void setCognome(string );

    string getNome();

    string getCognome();

    void codifica(string , string , int );

    void decodifica();

    void setChiave(int);

    int getChiave();

    void stampa();

    };

    I do not attach the public method here because problem is before using methods. Problem occurs when I create instance of array of class anagrafica. At the end that is not an array. Program does not raise any exception in the for with i from 1 to N. As if array was of dimension N. But it isn't. I tried also with the double pointer like this

    anagrafica** Arr_ = new anagrafica*[N];

    for (int i = 0; i < N; i++)
    {
    Arr_[i] = new anagrafica("ciao", "miao", i*25);
    }

    but Arr_ is of only 1 element, the first no other :confused:

    Mircea NeacsuM L 2 Replies Last reply
    0
    • R Roberto64_Ge

      Hi I'm getting problem running the sw I attach here below. I am trying to make an array of a certain class. In the header I have the class then a .cpp with public methods. In the main I'm doing this:

      #include "Anagrafica.h"
      #include
      #include
      #include
      using namespace std;

      void cod (anagrafica arr,string n, string c,int chiave)
      { arr.codifica(n,c,chiave); }

      void decod (anagrafica arr)
      { arr.decodifica(); }

      int main()
      {
      ifstream file;
      file.open("input.txt");
      int N;
      string n,c;

      int chiave;
      int chiave_input;
      file>>N;
      cout<>chiave;
      file>>n;
      file>>c;
      //Arr_[i].setChiave(chiave);
      //cod(Arr_[i],n,c,chiave);
      }
      for (int i=0; i>chiave_input;

      }

      here is header

      #include
      #include
      using namespace std;

      class anagrafica
      {
      private:

      string nome_cifrato;
      string cognome_cifrato;
      int chiave_cifratura;

      public :

      anagrafica(string ="", string ="", int=0); //costruttore

      void setNome(string );

      void setCognome(string );

      string getNome();

      string getCognome();

      void codifica(string , string , int );

      void decodifica();

      void setChiave(int);

      int getChiave();

      void stampa();

      };

      I do not attach the public method here because problem is before using methods. Problem occurs when I create instance of array of class anagrafica. At the end that is not an array. Program does not raise any exception in the for with i from 1 to N. As if array was of dimension N. But it isn't. I tried also with the double pointer like this

      anagrafica** Arr_ = new anagrafica*[N];

      for (int i = 0; i < N; i++)
      {
      Arr_[i] = new anagrafica("ciao", "miao", i*25);
      }

      but Arr_ is of only 1 element, the first no other :confused:

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Roberto64_Ge wrote:

      but Arr_ is of only 1 element, the first no other

      How did you verify that? If you just looked in VisualStudio debugger, it will indeed show only one element because it doesn't know the size of the array. You can enter a length specifier in the inspector window (like Arr_,10) to make it show 10 elements. Alternatively you can just say:

      anagrafica *Arr_[10];
      for (int i = 0; i < N; i++)
      {
      Arr_[i] = new anagrafica("ciao", "miao", i*25);
      }

      In this case the debugger will know the size of the array and show all elements. Otherwise your code looks good as far as array creation goes.

      Mircea

      R 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        Roberto64_Ge wrote:

        but Arr_ is of only 1 element, the first no other

        How did you verify that? If you just looked in VisualStudio debugger, it will indeed show only one element because it doesn't know the size of the array. You can enter a length specifier in the inspector window (like Arr_,10) to make it show 10 elements. Alternatively you can just say:

        anagrafica *Arr_[10];
        for (int i = 0; i < N; i++)
        {
        Arr_[i] = new anagrafica("ciao", "miao", i*25);
        }

        In this case the debugger will know the size of the array and show all elements. Otherwise your code looks good as far as array creation goes.

        Mircea

        R Offline
        R Offline
        Roberto64_Ge
        wrote on last edited by
        #3

        Problem is that I tried also with a costant as in your example but I could only see the first element. In the snippet I posted some parts of the code is commented. But when I remove comments app is working but the behaviour is that as if array was of 1 only element. I will read further replies tomorrow, thank you. ;) :zzz:

        1 Reply Last reply
        0
        • R Roberto64_Ge

          Hi I'm getting problem running the sw I attach here below. I am trying to make an array of a certain class. In the header I have the class then a .cpp with public methods. In the main I'm doing this:

          #include "Anagrafica.h"
          #include
          #include
          #include
          using namespace std;

          void cod (anagrafica arr,string n, string c,int chiave)
          { arr.codifica(n,c,chiave); }

          void decod (anagrafica arr)
          { arr.decodifica(); }

          int main()
          {
          ifstream file;
          file.open("input.txt");
          int N;
          string n,c;

          int chiave;
          int chiave_input;
          file>>N;
          cout<>chiave;
          file>>n;
          file>>c;
          //Arr_[i].setChiave(chiave);
          //cod(Arr_[i],n,c,chiave);
          }
          for (int i=0; i>chiave_input;

          }

          here is header

          #include
          #include
          using namespace std;

          class anagrafica
          {
          private:

          string nome_cifrato;
          string cognome_cifrato;
          int chiave_cifratura;

          public :

          anagrafica(string ="", string ="", int=0); //costruttore

          void setNome(string );

          void setCognome(string );

          string getNome();

          string getCognome();

          void codifica(string , string , int );

          void decodifica();

          void setChiave(int);

          int getChiave();

          void stampa();

          };

          I do not attach the public method here because problem is before using methods. Problem occurs when I create instance of array of class anagrafica. At the end that is not an array. Program does not raise any exception in the for with i from 1 to N. As if array was of dimension N. But it isn't. I tried also with the double pointer like this

          anagrafica** Arr_ = new anagrafica*[N];

          for (int i = 0; i < N; i++)
          {
          Arr_[i] = new anagrafica("ciao", "miao", i*25);
          }

          but Arr_ is of only 1 element, the first no other :confused:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The array variable needs to point to an array of pointers, so the syntax needs to be:

          anagrafica** Arr_ = new anagrafica*[N]; // a pointer to an array of pointers

          for (int i = 0; i < N; i++)
          {
          Arr_[i] = new anagrafica("ciao", "miao", i*25);
          }

          Remember that Arr_ is a pointer to the actual array so it will appear to contain only one element. But you can check the contents with another simple loop. You could make life much simpler for yourself by using one of the STL containers, such as vector.

          R 1 Reply Last reply
          0
          • L Lost User

            The array variable needs to point to an array of pointers, so the syntax needs to be:

            anagrafica** Arr_ = new anagrafica*[N]; // a pointer to an array of pointers

            for (int i = 0; i < N; i++)
            {
            Arr_[i] = new anagrafica("ciao", "miao", i*25);
            }

            Remember that Arr_ is a pointer to the actual array so it will appear to contain only one element. But you can check the contents with another simple loop. You could make life much simpler for yourself by using one of the STL containers, such as vector.

            R Offline
            R Offline
            Roberto64_Ge
            wrote on last edited by
            #5

            ok, i better checked, so thank you. Now, how is the syntax to access the method of any instance (of the class) pointed by the i element af Arr_. I don't find the way to do that. I mean that I need to do something like Example : Arr_[i].setChiave(3) By the way, may I call a method , example setNome(stringaNome) from within another method?

            Greg UtasG L 2 Replies Last reply
            0
            • R Roberto64_Ge

              ok, i better checked, so thank you. Now, how is the syntax to access the method of any instance (of the class) pointed by the i element af Arr_. I don't find the way to do that. I mean that I need to do something like Example : Arr_[i].setChiave(3) By the way, may I call a method , example setNome(stringaNome) from within another method?

              Greg UtasG Offline
              Greg UtasG Offline
              Greg Utas
              wrote on last edited by
              #6

              Arr_[i]**->**setChiave(3) EDIT: And sure, one method can call another, subject to access controls (public, protected, private).

              Robust Services Core | Software Techniques for Lemmings | Articles
              The fox knows many things, but the hedgehog knows one big thing.

              <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
              <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

              R 1 Reply Last reply
              0
              • R Roberto64_Ge

                ok, i better checked, so thank you. Now, how is the syntax to access the method of any instance (of the class) pointed by the i element af Arr_. I don't find the way to do that. I mean that I need to do something like Example : Arr_[i].setChiave(3) By the way, may I call a method , example setNome(stringaNome) from within another method?

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Roberto64_Ge wrote:

                Example : Arr_[i].setChiave(3)

                Yes, as a simple test would show you.

                Roberto64_Ge wrote:

                may I call a method , example setNome(stringaNome) from within another method?

                Yes again. All methods* within a class may be called from other methods, whether they are private or public. *But you cannot call non-static from static methods, as the non-statics need an object reference.

                R 1 Reply Last reply
                0
                • Greg UtasG Greg Utas

                  Arr_[i]**->**setChiave(3) EDIT: And sure, one method can call another, subject to access controls (public, protected, private).

                  Robust Services Core | Software Techniques for Lemmings | Articles
                  The fox knows many things, but the hedgehog knows one big thing.

                  R Offline
                  R Offline
                  Roberto64_Ge
                  wrote on last edited by
                  #8

                  OK thanks. By the way, may I call a method , example setNome(stringaNome) from within another method? I attach here three methods of the class, the greater calling the two small.

                  void anagrafica::setNome(string n_)
                  {
                  nome_cifrato=n_;
                  }

                  void anagrafica::setCognome(string c_)
                  {
                  cognome_cifrato=c_;
                  }

                  void anagrafica::codifica(string nome_chiaro, string cognome_chiaro, int chiave)
                  {
                  char car;
                  nome_cifrato = "";
                  cognome_cifrato = "";
                  for (int i=0;i setNome(nome_cifrato);
                  for (int i=0; isetCognome(cognome_cifrato);
                  }

                  The two lines of code hghlighted have no effects.

                  Greg UtasG 1 Reply Last reply
                  0
                  • L Lost User

                    Roberto64_Ge wrote:

                    Example : Arr_[i].setChiave(3)

                    Yes, as a simple test would show you.

                    Roberto64_Ge wrote:

                    may I call a method , example setNome(stringaNome) from within another method?

                    Yes again. All methods* within a class may be called from other methods, whether they are private or public. *But you cannot call non-static from static methods, as the non-statics need an object reference.

                    R Offline
                    R Offline
                    Roberto64_Ge
                    wrote on last edited by
                    #9

                    if I use double pointer compiler don't let me use that syntax.

                    L 2 Replies Last reply
                    0
                    • R Roberto64_Ge

                      if I use double pointer compiler don't let me use that syntax.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      What do you mean by, "double pointer"? If something does not work then please show the actual code and the error message(s).

                      R 1 Reply Last reply
                      0
                      • R Roberto64_Ge

                        if I use double pointer compiler don't let me use that syntax.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        I have commented out all the parts of your code that are not needed to create a simple test. The result is as follows:

                        // header file
                        #include
                        #include
                        using namespace std;

                        class anagrafica
                        {
                        private:

                        string nome_cifrato;
                        string cognome_cifrato;
                        int chiave_cifratura;

                        public :

                        anagrafica(string a, string b, int c); //costruttore

                        // void setNome(string );

                        // void setCognome(string );

                        // string getNome();

                        // string getCognome();

                        // void codifica(string , string , int );

                        // void decodifica();

                        void setChiave(int);
                        void print();
                        

                        // int getChiave();

                        // void stampa();

                        };

                        // implementation
                        #include "Anagrafica.h"
                        #include
                        #include
                        #include
                        using namespace std;

                        // void cod (anagrafica arr,string n, string c,int chiave)
                        // { arr.codifica(n,c,chiave); }

                        // void decod (anagrafica arr)
                        // { arr.decodifica(); }

                        anagrafica::anagrafica(string a, string b, int c) //costruttore
                        {
                        nome_cifrato = a;
                        cognome_cifrato = b;
                        chiave_cifratura = c;
                        }

                        void anagrafica::setChiave(int a)
                        {
                        chiave_cifratura = a;
                        }

                        // added to show the results
                        void anagrafica::print()
                        {
                        cout << nome_cifrato << " " << cognome_cifrato << " " << chiave_cifratura << endl;
                        }

                        int main()
                        {
                        // ifstream file;
                        // file.open("input.txt");
                        int N = 4; // create just four entries
                        //string n,c;

                        // int chiave;
                        // int chiave_input;
                        // file>>N;
                        // cout<print();
                        }

                        // for (int i=0; i>chiave;
                        // file>>n;
                        // file>>c;
                        // //Arr_[i].setChiave(chiave);
                        // //cod(Arr_[i],n,c,chiave);
                        // }
                        // for (int i=0; i>chiave_input;

                        }

                        R 2 Replies Last reply
                        0
                        • L Lost User

                          What do you mean by, "double pointer"? If something does not work then please show the actual code and the error message(s).

                          R Offline
                          R Offline
                          Roberto64_Ge
                          wrote on last edited by
                          #12

                          i do not know how to quote a previous answer. Anyway you suggested me to use a pointer to an array of pointers so we did anagrafica **Arr_ = new anagrafica*[N]; But in this case compiler didn't let me access the method in this way : Arr_[i].setChiave(3); For that reason I asked for the correct syntax. Meanwhile I was waiting your reply I decided to try and so I went back to the first version temporarily and this is working, with respect to my first question " why I cannot see all the array elements?". With this I mean that, also with my first version, if I access Arr_[i] during the debug (within the relevant window "Control expression". I guess this is the correct translation from Italian) I see all the different instances of the class in each element of the array Arr_. I attach here the code of the first version. With this version running then I get a new malfunction that is that when I call the method for cifrating the true name and surname, from within this method I call the two methods to set the name and the surname, setNome and setCognome but this has no effects. If preferred I will attach the complete solution.

                          anagrafica* Arr_ = new anagrafica[N];

                          for (int i = 0; i < N; i++)
                          {
                          Arr_[i] = anagrafica("ciao", "miao", i*25);
                          }

                          for (int i=0; i>chiave;
                          file>>n;
                          file>>c;
                          Arr_[i].setChiave(chiave);
                          cod(Arr_[i],n,c,chiave);
                          }
                          for (int i=0; i

                          L 1 Reply Last reply
                          0
                          • R Roberto64_Ge

                            i do not know how to quote a previous answer. Anyway you suggested me to use a pointer to an array of pointers so we did anagrafica **Arr_ = new anagrafica*[N]; But in this case compiler didn't let me access the method in this way : Arr_[i].setChiave(3); For that reason I asked for the correct syntax. Meanwhile I was waiting your reply I decided to try and so I went back to the first version temporarily and this is working, with respect to my first question " why I cannot see all the array elements?". With this I mean that, also with my first version, if I access Arr_[i] during the debug (within the relevant window "Control expression". I guess this is the correct translation from Italian) I see all the different instances of the class in each element of the array Arr_. I attach here the code of the first version. With this version running then I get a new malfunction that is that when I call the method for cifrating the true name and surname, from within this method I call the two methods to set the name and the surname, setNome and setCognome but this has no effects. If preferred I will attach the complete solution.

                            anagrafica* Arr_ = new anagrafica[N];

                            for (int i = 0; i < N; i++)
                            {
                            Arr_[i] = anagrafica("ciao", "miao", i*25);
                            }

                            for (int i=0; i>chiave;
                            file>>n;
                            file>>c;
                            Arr_[i].setChiave(chiave);
                            cod(Arr_[i],n,c,chiave);
                            }
                            for (int i=0; i

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Arr_[i].setChiave(3);

                            Sorry, I misread that. Arr_[i] is a pointer so you need to use the correct pointer dereferencing syntax:

                            Arr_[i]**->**setChiave(3);

                            R 1 Reply Last reply
                            0
                            • L Lost User

                              I have commented out all the parts of your code that are not needed to create a simple test. The result is as follows:

                              // header file
                              #include
                              #include
                              using namespace std;

                              class anagrafica
                              {
                              private:

                              string nome_cifrato;
                              string cognome_cifrato;
                              int chiave_cifratura;

                              public :

                              anagrafica(string a, string b, int c); //costruttore

                              // void setNome(string );

                              // void setCognome(string );

                              // string getNome();

                              // string getCognome();

                              // void codifica(string , string , int );

                              // void decodifica();

                              void setChiave(int);
                              void print();
                              

                              // int getChiave();

                              // void stampa();

                              };

                              // implementation
                              #include "Anagrafica.h"
                              #include
                              #include
                              #include
                              using namespace std;

                              // void cod (anagrafica arr,string n, string c,int chiave)
                              // { arr.codifica(n,c,chiave); }

                              // void decod (anagrafica arr)
                              // { arr.decodifica(); }

                              anagrafica::anagrafica(string a, string b, int c) //costruttore
                              {
                              nome_cifrato = a;
                              cognome_cifrato = b;
                              chiave_cifratura = c;
                              }

                              void anagrafica::setChiave(int a)
                              {
                              chiave_cifratura = a;
                              }

                              // added to show the results
                              void anagrafica::print()
                              {
                              cout << nome_cifrato << " " << cognome_cifrato << " " << chiave_cifratura << endl;
                              }

                              int main()
                              {
                              // ifstream file;
                              // file.open("input.txt");
                              int N = 4; // create just four entries
                              //string n,c;

                              // int chiave;
                              // int chiave_input;
                              // file>>N;
                              // cout<print();
                              }

                              // for (int i=0; i>chiave;
                              // file>>n;
                              // file>>c;
                              // //Arr_[i].setChiave(chiave);
                              // //cod(Arr_[i],n,c,chiave);
                              // }
                              // for (int i=0; i>chiave_input;

                              }

                              R Offline
                              R Offline
                              Roberto64_Ge
                              wrote on last edited by
                              #14

                              Ok , so the syntax is not Arr_[i].setChiave(3) but

                              Arr_[i]->setChiave(3)

                              as per Greg Utas and your new indication. As I wrote I would say that also the version

                              anagrafica* Arr_ = new anagrafica[N];

                              for (int i = 0; i < N; i++)
                              {
                              Arr_[i] = anagrafica("ciao", "miao", i*25);
                              }

                              is working the same. Then I have the further problem of the two methods for setting cifrated name and surname tha have no effects. I see that there is some problem of synchronizations with the replies. But ok syntax has been clarified.

                              L 1 Reply Last reply
                              0
                              • L Lost User

                                Arr_[i].setChiave(3);

                                Sorry, I misread that. Arr_[i] is a pointer so you need to use the correct pointer dereferencing syntax:

                                Arr_[i]**->**setChiave(3);

                                R Offline
                                R Offline
                                Roberto64_Ge
                                wrote on last edited by
                                #15

                                Ok i realized that. thank you very much. What about the other problem? Methods ....

                                1 Reply Last reply
                                0
                                • L Lost User

                                  I have commented out all the parts of your code that are not needed to create a simple test. The result is as follows:

                                  // header file
                                  #include
                                  #include
                                  using namespace std;

                                  class anagrafica
                                  {
                                  private:

                                  string nome_cifrato;
                                  string cognome_cifrato;
                                  int chiave_cifratura;

                                  public :

                                  anagrafica(string a, string b, int c); //costruttore

                                  // void setNome(string );

                                  // void setCognome(string );

                                  // string getNome();

                                  // string getCognome();

                                  // void codifica(string , string , int );

                                  // void decodifica();

                                  void setChiave(int);
                                  void print();
                                  

                                  // int getChiave();

                                  // void stampa();

                                  };

                                  // implementation
                                  #include "Anagrafica.h"
                                  #include
                                  #include
                                  #include
                                  using namespace std;

                                  // void cod (anagrafica arr,string n, string c,int chiave)
                                  // { arr.codifica(n,c,chiave); }

                                  // void decod (anagrafica arr)
                                  // { arr.decodifica(); }

                                  anagrafica::anagrafica(string a, string b, int c) //costruttore
                                  {
                                  nome_cifrato = a;
                                  cognome_cifrato = b;
                                  chiave_cifratura = c;
                                  }

                                  void anagrafica::setChiave(int a)
                                  {
                                  chiave_cifratura = a;
                                  }

                                  // added to show the results
                                  void anagrafica::print()
                                  {
                                  cout << nome_cifrato << " " << cognome_cifrato << " " << chiave_cifratura << endl;
                                  }

                                  int main()
                                  {
                                  // ifstream file;
                                  // file.open("input.txt");
                                  int N = 4; // create just four entries
                                  //string n,c;

                                  // int chiave;
                                  // int chiave_input;
                                  // file>>N;
                                  // cout<print();
                                  }

                                  // for (int i=0; i>chiave;
                                  // file>>n;
                                  // file>>c;
                                  // //Arr_[i].setChiave(chiave);
                                  // //cod(Arr_[i],n,c,chiave);
                                  // }
                                  // for (int i=0; i>chiave_input;

                                  }

                                  R Offline
                                  R Offline
                                  Roberto64_Ge
                                  wrote on last edited by
                                  #16

                                  Clarified the syntax problem I resume here. The code works also in this way ::

                                  anagrafica* Arr_ = new anagrafica[N];

                                  for (int i = 0; i < N; i++)
                                  {
                                  Arr_[i] = anagrafica("ciao", "miao", i*25);
                                  }

                                  for (int i=0; i>chiave;
                                  file>>n;
                                  file>>c;
                                  Arr_[i].setChiave(chiave);
                                  cod(Arr_[i],n,c,chiave);
                                  }
                                  for (int i=0; i

                                  Because I can see all the Arr_ elements containing instances of the class. But two methods recalled from one other method do not heve effects.

                                  L 1 Reply Last reply
                                  0
                                  • R Roberto64_Ge

                                    Ok , so the syntax is not Arr_[i].setChiave(3) but

                                    Arr_[i]->setChiave(3)

                                    as per Greg Utas and your new indication. As I wrote I would say that also the version

                                    anagrafica* Arr_ = new anagrafica[N];

                                    for (int i = 0; i < N; i++)
                                    {
                                    Arr_[i] = anagrafica("ciao", "miao", i*25);
                                    }

                                    is working the same. Then I have the further problem of the two methods for setting cifrated name and surname tha have no effects. I see that there is some problem of synchronizations with the replies. But ok syntax has been clarified.

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #17

                                    You have declared a number of methods in your header file but you have not added their implementation to the .cpp source. In my sample code above you can see the implementation for the setChiave method. You need to do the same for all the other methods declared in the class.

                                    R 1 Reply Last reply
                                    0
                                    • L Lost User

                                      You have declared a number of methods in your header file but you have not added their implementation to the .cpp source. In my sample code above you can see the implementation for the setChiave method. You need to do the same for all the other methods declared in the class.

                                      R Offline
                                      R Offline
                                      Roberto64_Ge
                                      wrote on last edited by
                                      #18

                                      I attach the method file .cpp

                                      #include "Anagrafica.h"
                                      #include
                                      #include

                                      using namespace std;

                                      anagrafica::anagrafica(string name,string surname, int key)
                                      {
                                      nome_cifrato=name;
                                      cognome_cifrato=surname;
                                      chiave_cifratura=key;
                                      }

                                      void anagrafica::setNome(string n_)
                                      {
                                      nome_cifrato=n_;
                                      }
                                      void anagrafica::setCognome(string c_)
                                      {
                                      cognome_cifrato=c_;
                                      }

                                      string anagrafica::getNome()
                                      {
                                      return nome_cifrato;
                                      }
                                      string anagrafica::getCognome()
                                      {
                                      return cognome_cifrato;
                                      }

                                      int anagrafica::getChiave()
                                      {
                                      return chiave_cifratura;
                                      }

                                      void anagrafica::setChiave(int c)
                                      {
                                      chiave_cifratura=c;
                                      }

                                      void anagrafica::stampa()
                                      {
                                      string nom = nome_cifrato;
                                      string cog = cognome_cifrato;
                                      cout<<" nome cifrato "<

                                      I can't understand where is the

                                      1 Reply Last reply
                                      0
                                      • R Roberto64_Ge

                                        Clarified the syntax problem I resume here. The code works also in this way ::

                                        anagrafica* Arr_ = new anagrafica[N];

                                        for (int i = 0; i < N; i++)
                                        {
                                        Arr_[i] = anagrafica("ciao", "miao", i*25);
                                        }

                                        for (int i=0; i>chiave;
                                        file>>n;
                                        file>>c;
                                        Arr_[i].setChiave(chiave);
                                        cod(Arr_[i],n,c,chiave);
                                        }
                                        for (int i=0; i

                                        Because I can see all the Arr_ elements containing instances of the class. But two methods recalled from one other method do not heve effects.

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #19

                                        I think this thread is getting confused (I certainly am). I suggest you start a new thread and post the actual code that you are now using, and explain what methods do not work.

                                        R 1 Reply Last reply
                                        0
                                        • R Roberto64_Ge

                                          OK thanks. By the way, may I call a method , example setNome(stringaNome) from within another method? I attach here three methods of the class, the greater calling the two small.

                                          void anagrafica::setNome(string n_)
                                          {
                                          nome_cifrato=n_;
                                          }

                                          void anagrafica::setCognome(string c_)
                                          {
                                          cognome_cifrato=c_;
                                          }

                                          void anagrafica::codifica(string nome_chiaro, string cognome_chiaro, int chiave)
                                          {
                                          char car;
                                          nome_cifrato = "";
                                          cognome_cifrato = "";
                                          for (int i=0;i setNome(nome_cifrato);
                                          for (int i=0; isetCognome(cognome_cifrato);
                                          }

                                          The two lines of code hghlighted have no effects.

                                          Greg UtasG Offline
                                          Greg UtasG Offline
                                          Greg Utas
                                          wrote on last edited by
                                          #20

                                          Those lines have no effect because the default in C++ is to pass an argument by value. That is, to make a copy of it. If you want to modify the data that is passed to the function, it must be passed by reference; another option is to pass a pointer to the argument. To pass by reference, you append a & to the argument's type. So, you need to change your two small functions like this:

                                          void anagrafica::setNome(string**&** n_)
                                          {
                                          nome_cifrato=n_;
                                          }

                                          void anagrafica::setCognome(string**&** c_)
                                          {
                                          cognome_cifrato=c_;
                                          }

                                          Robust Services Core | Software Techniques for Lemmings | Articles
                                          The fox knows many things, but the hedgehog knows one big thing.

                                          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
                                          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

                                          L 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups