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.
  • L Lost User

    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 Offline
    R Offline
    Roberto64_Ge
    wrote on last edited by
    #21

    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();

    };

    methods

    #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 "<setNome(nome_cifrato);
    for (int i=0; isetCognome(cognome_cifrato);
    }

    void anagrafica::decodifica()
    {

    char car;
    string nome_chiaro = "";
    string cognome_chiaro = "";
    for (int i=0;i

    L 1 Reply Last reply
    0
    • R Roberto64_Ge

      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();

      };

      methods

      #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 "<setNome(nome_cifrato);
      for (int i=0; isetCognome(cognome_cifrato);
      }

      void anagrafica::decodifica()
      {

      char car;
      string nome_chiaro = "";
      string cognome_chiaro = "";
      for (int i=0;i

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

      You need to execute that code through the debugger to see exactly what is happening. I tried to reproduce it and it was passing blank parameters in to the method. I suspect that the problem is where you are reading values from the input file. [edit] That method is modifying both variables nome_cifrato and cognome_cifrato inline, and you then call setNome and setCognome. But you have already modified the names so you do not need to call the setter methods. I think you need to look more closely at the design of this application and remove some of the redundancies.

      R 1 Reply Last reply
      0
      • Greg UtasG Greg Utas

        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.

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

        That will not make any difference. I have just tested the code and those methods both do what they are supposed to. The issue is more complex due to the confused (and confusing) nature of this code.

        1 Reply Last reply
        0
        • L Lost User

          You need to execute that code through the debugger to see exactly what is happening. I tried to reproduce it and it was passing blank parameters in to the method. I suspect that the problem is where you are reading values from the input file. [edit] That method is modifying both variables nome_cifrato and cognome_cifrato inline, and you then call setNome and setCognome. But you have already modified the names so you do not need to call the setter methods. I think you need to look more closely at the design of this application and remove some of the redundancies.

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

          Ok you are right saying that

          Richard MacCutchan wrote:

          That method is modifying both variables nome_cifrato and cognome_cifrato inline, and you then call setNome and setCognome. But you have already modified the names so you do not need to call the setter methods.

          I didn't see it, I get confused writing the code, probably I inteded to use local variables to build cifrated name and surname. But problem was in this function within the main

          void cod (anagrafica arr,string n, string c,int chiave)
          {

          arr.codifica(n,c,chiave);

          }

          that i re-arranged this way

          void cod (anagrafica &arr,string n, string c,int chiave)
          {

          arr.codifica(n,c,chiave);

          }

          passing the address to "cod". And about this, I understood the reason (Greg Utas was right about the rules but didn't apply to the real problem because i did not need to change the strings passed to the methods but the instance of the class itself). But now there is another behaviour that confuses me : In a previous version the following method :

          void anagrafica::stampa()
          {
          cout<<" nome cifrato "<

          I had to modify in this new version :

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

          because in the first version it was just printing nothing. Now it prints correctly. I just tried without a real knowledge behind, only some kind of sensation but I don't understand why it didn't work before and why it works now.

          Anyway thanks for all the efforts/suggestions. I am just starting with c++ in order to help my children in their school exercise.

          L 1 Reply Last reply
          0
          • R Roberto64_Ge

            Ok you are right saying that

            Richard MacCutchan wrote:

            That method is modifying both variables nome_cifrato and cognome_cifrato inline, and you then call setNome and setCognome. But you have already modified the names so you do not need to call the setter methods.

            I didn't see it, I get confused writing the code, probably I inteded to use local variables to build cifrated name and surname. But problem was in this function within the main

            void cod (anagrafica arr,string n, string c,int chiave)
            {

            arr.codifica(n,c,chiave);

            }

            that i re-arranged this way

            void cod (anagrafica &arr,string n, string c,int chiave)
            {

            arr.codifica(n,c,chiave);

            }

            passing the address to "cod". And about this, I understood the reason (Greg Utas was right about the rules but didn't apply to the real problem because i did not need to change the strings passed to the methods but the instance of the class itself). But now there is another behaviour that confuses me : In a previous version the following method :

            void anagrafica::stampa()
            {
            cout<<" nome cifrato "<

            I had to modify in this new version :

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

            because in the first version it was just printing nothing. Now it prints correctly. I just tried without a real knowledge behind, only some kind of sensation but I don't understand why it didn't work before and why it works now.

            Anyway thanks for all the efforts/suggestions. I am just starting with c++ in order to help my children in their school exercise.

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

            Roberto64_Ge wrote:

            I am just starting with c++ in order to help my children in their school exercise.

            Well, I am sorry to have to say this, but what you have created is not a good example of C++ code for them to follow. The two static methods cod and decod serve no purpose other than to call the actual methods of the anagrafica class. So they are totally redundant; you could call the codifica method direct from main. There are a number of other issues that look incorrect to me but I do not have time to teach you C++.

            R 1 Reply Last reply
            0
            • L Lost User

              Roberto64_Ge wrote:

              I am just starting with c++ in order to help my children in their school exercise.

              Well, I am sorry to have to say this, but what you have created is not a good example of C++ code for them to follow. The two static methods cod and decod serve no purpose other than to call the actual methods of the anagrafica class. So they are totally redundant; you could call the codifica method direct from main. There are a number of other issues that look incorrect to me but I do not have time to teach you C++.

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

              That was a request (strange to me too. Exercise : implement a function that calls the methods ..., not directly in the main. Could also be that it was some type of cut and paste from other exercises so some error in the exercise definition ). As I said I am starting too so I do what I can and for that reason I am writing here otherwise why should I do this (to ask a forum)? Then I think that everyone is doing something makes mistakes some for distraction (some I did here) and some for poor knowledge (and also here). But going straight to my last question is there an explanation? Regards

              L 1 Reply Last reply
              0
              • R Roberto64_Ge

                That was a request (strange to me too. Exercise : implement a function that calls the methods ..., not directly in the main. Could also be that it was some type of cut and paste from other exercises so some error in the exercise definition ). As I said I am starting too so I do what I can and for that reason I am writing here otherwise why should I do this (to ask a forum)? Then I think that everyone is doing something makes mistakes some for distraction (some I did here) and some for poor knowledge (and also here). But going straight to my last question is there an explanation? Regards

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

                I have no idea where that question came from or what it actually says. But I would assume that if you want an explanation then the place where you found it is the best place to ask. The forums here are more for help in diagnosing and fixing specific problems. There are other sites that offer well written tutorials, such as Learn C++ – Skill up with our free tutorials[^] and C++ Tutorial[^].

                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