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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with get/set method [modified]

Problem with get/set method [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
18 Posts 5 Posters 0 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.
  • N Nuri Ismail

    Sivyo wrote:

    Unhandled exception at 0x1026edac (msvcr90d.dll) in Zoo.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd

    Most likely, you are either accessing a memory location that is not been allocated or has been freed already. I've tried the code from your modified post and it works fine. Maybe you should go step by step through your main method and track your heap objects. You should also use the call stack window in order to see the exact position of the crash in your code.

    Nuri Ismail

    S Offline
    S Offline
    Sivyo
    wrote on last edited by
    #9

    I don't actually know how to use the call stack window. I will google it see if that will help. I did debug it and create break points and it always crashes right after set weight. I checked out the watch. and everything goes where it is supossed to but then it just crashes right after everything is set. I commented out set weight and it seems that is the problem, it doesn't crash if I take it out

    D 1 Reply Last reply
    0
    • S Sivyo

      I modified my origional post. Its all up there

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #10

      Your code is working fine on my system, however I had to do the following modifications, in order to compile it: Commented out few lines (I haven't all your animal classes :rolleyes: ), hence

      int main(int argc,char* argv[])
      {
      Animals *ourAnimals[6];

      Animals *goldfish = new Goldfish();
      //Animals *crocodile = new Crocodiles();
      //Animals *elephant = new Elephants();
      //Animals *gazelles = new Gazelles();
      //Animals *shark = new Sharks();
      //Animals *snakes = new Snakes();

      ourAnimals[0]=goldfish;
      //ourAnimals[1]=crocodile;
      //ourAnimals[2]=elephant;
      //ourAnimals[3]=gazelles;
      //ourAnimals[4]=shark;
      //ourAnimals[5]=snakes;

      string name, gender, weight;

      name = "the Goldfish";
      gender = "Female";
      weight = "One Ounce";

      goldfish -> setName(name);
      goldfish -> setGender(gender);
      goldfish -> setWeight(weight);

      }

      Added the constructor and destructor for the Animals (misname? Wouldn't be better 'Animal'?) class:

      class Animals
      {
      public:
      Animals(void){};
      virtual ~Animals(void){};
      //...

      Again and again: use the debugger and have a look at the call stack window. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      S 1 Reply Last reply
      0
      • S Sivyo

        I have been trying to figure out what is wrong with this code. its probably something really obvious just that isn't clear to me. I would like any help that can be given, thanks.

        int main(int argc,char* argv[])
        {
        Animals *ourAnimals[6];

        Animals *goldfish = new Goldfish();
        Animals *crocodile = new Crocodiles();
        Animals *elephant = new Elephants();
        Animals *gazelles = new Gazelles();
        Animals *shark = new Sharks();
        Animals *snakes = new Snakes();

        ourAnimals[0]=goldfish;
        ourAnimals[1]=crocodile;
        ourAnimals[2]=elephant;
        ourAnimals[3]=gazelles;
        ourAnimals[4]=shark;
        ourAnimals[5]=snakes;

        string name, gender, weight;

        name = "the Goldfish";
        gender = "Female";
        weight = "One Ounce";

        goldfish -> setName(name);
        goldfish -> setGender(gender);
        goldfish -> setWeight(weight);

        }

        and this is the class

        #pragma once
        #include <iostream>
        #include <string>

        using namespace std;
        class Animals
        {
        public:
        Animals(void);
        virtual ~Animals(void);

        string getName() {return m\_name;}
        string setName(string &name){return m\_name = name;}
        
        string getGender(){return m\_gender;}
        string setGender(string &gender){return m\_gender = gender;}
        
        string getWeight(){return m\_weight;}
        string setWeight(string &weight){return m\_weight = weight;}
        
        virtual void goOut() = 0;
        virtual void converse() = 0;
        virtual void getBack() = 0;
        

        protected:
        string m_name;
        string m_gender;
        string m_weight;

        };

        it gives me an unhandled exception error. this is my goldfish class

        #pragma once
        

        #include "marine.h"

        class Goldfish :
        public Marine
        {
        public:
        Goldfish(void);
        virtual ~Goldfish(void);

        virtual void converse();
        

        };

        Goldfish::Goldfish(void)
        {
        }

        Goldfish::~Goldfish(void)
        {
        }

        void Goldfish::converse()
        {
        cout<<"The Gold fish speak: "<<"Bloob Bloob"<<endl<<endl;
        }

        there is another derived class from Animal which is Called Marine in this case which looks like this

        #pragma once
        #include "animals.h"

        class Marine :
        public Animals
        {
        public:
        Marine(void);
        virtual ~Marine(void);

        virtual void goOut();
        virtual void converse()= 0;
        virtual void getBack();
        

        };

        #include "Marine.h"

        Marine::Marine(void)
        {
        }

        Marine::~Marine(void)
        {
        }

        void Marine::goOut()
        {
        cout<<"The Marine animals: "<<"swim out and are fed"<<endl<<endl;
        }

        void Marine::getBack

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

        Sivyo wrote:

        In main() you have goldfish -> setName(name); but in class Animals you have string setName(string &name){return m_name = name;}

        I think your setName() definition should read:

        string setName(string name){return m_name = name;}

        Also as mentioned previously there is little to be gained in the setXXX() functions in returning the parameter values. Using void return types is better as:

        void setName(string name)
        {
        m_name = name;
        }

        CPalliniC 1 Reply Last reply
        0
        • L Lost User

          Sivyo wrote:

          In main() you have goldfish -> setName(name); but in class Animals you have string setName(string &name){return m_name = name;}

          I think your setName() definition should read:

          string setName(string name){return m_name = name;}

          Also as mentioned previously there is little to be gained in the setXXX() functions in returning the parameter values. Using void return types is better as:

          void setName(string name)
          {
          m_name = name;
          }

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #12

          I think there's nothing wrong in his method signature. Why should he pass the string by value? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          L 1 Reply Last reply
          0
          • S Sivyo

            I don't actually know how to use the call stack window. I will google it see if that will help. I did debug it and create break points and it always crashes right after set weight. I checked out the watch. and everything goes where it is supossed to but then it just crashes right after everything is set. I commented out set weight and it seems that is the problem, it doesn't crash if I take it out

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #13

            Sivyo wrote:

            I commented out set weight and it seems that is the problem, it doesn't crash if I take it out

            Don't be so sure. You may have just moved the problem to another, less-sensitive location.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            S 1 Reply Last reply
            0
            • CPalliniC CPallini

              Your code is working fine on my system, however I had to do the following modifications, in order to compile it: Commented out few lines (I haven't all your animal classes :rolleyes: ), hence

              int main(int argc,char* argv[])
              {
              Animals *ourAnimals[6];

              Animals *goldfish = new Goldfish();
              //Animals *crocodile = new Crocodiles();
              //Animals *elephant = new Elephants();
              //Animals *gazelles = new Gazelles();
              //Animals *shark = new Sharks();
              //Animals *snakes = new Snakes();

              ourAnimals[0]=goldfish;
              //ourAnimals[1]=crocodile;
              //ourAnimals[2]=elephant;
              //ourAnimals[3]=gazelles;
              //ourAnimals[4]=shark;
              //ourAnimals[5]=snakes;

              string name, gender, weight;

              name = "the Goldfish";
              gender = "Female";
              weight = "One Ounce";

              goldfish -> setName(name);
              goldfish -> setGender(gender);
              goldfish -> setWeight(weight);

              }

              Added the constructor and destructor for the Animals (misname? Wouldn't be better 'Animal'?) class:

              class Animals
              {
              public:
              Animals(void){};
              virtual ~Animals(void){};
              //...

              Again and again: use the debugger and have a look at the call stack window. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              S Offline
              S Offline
              Sivyo
              wrote on last edited by
              #14

              noticed (with some help from inside sources) that in the problem is with the compiler for some odd reason it doesn't like creating a 3rd variable of string in Animals. I moved weight be to be first and name was last (3rd) then it showed Bad ptr message in the debugger. its a very strange error....

              1 Reply Last reply
              0
              • S Sivyo

                I have been trying to figure out what is wrong with this code. its probably something really obvious just that isn't clear to me. I would like any help that can be given, thanks.

                int main(int argc,char* argv[])
                {
                Animals *ourAnimals[6];

                Animals *goldfish = new Goldfish();
                Animals *crocodile = new Crocodiles();
                Animals *elephant = new Elephants();
                Animals *gazelles = new Gazelles();
                Animals *shark = new Sharks();
                Animals *snakes = new Snakes();

                ourAnimals[0]=goldfish;
                ourAnimals[1]=crocodile;
                ourAnimals[2]=elephant;
                ourAnimals[3]=gazelles;
                ourAnimals[4]=shark;
                ourAnimals[5]=snakes;

                string name, gender, weight;

                name = "the Goldfish";
                gender = "Female";
                weight = "One Ounce";

                goldfish -> setName(name);
                goldfish -> setGender(gender);
                goldfish -> setWeight(weight);

                }

                and this is the class

                #pragma once
                #include <iostream>
                #include <string>

                using namespace std;
                class Animals
                {
                public:
                Animals(void);
                virtual ~Animals(void);

                string getName() {return m\_name;}
                string setName(string &name){return m\_name = name;}
                
                string getGender(){return m\_gender;}
                string setGender(string &gender){return m\_gender = gender;}
                
                string getWeight(){return m\_weight;}
                string setWeight(string &weight){return m\_weight = weight;}
                
                virtual void goOut() = 0;
                virtual void converse() = 0;
                virtual void getBack() = 0;
                

                protected:
                string m_name;
                string m_gender;
                string m_weight;

                };

                it gives me an unhandled exception error. this is my goldfish class

                #pragma once
                

                #include "marine.h"

                class Goldfish :
                public Marine
                {
                public:
                Goldfish(void);
                virtual ~Goldfish(void);

                virtual void converse();
                

                };

                Goldfish::Goldfish(void)
                {
                }

                Goldfish::~Goldfish(void)
                {
                }

                void Goldfish::converse()
                {
                cout<<"The Gold fish speak: "<<"Bloob Bloob"<<endl<<endl;
                }

                there is another derived class from Animal which is Called Marine in this case which looks like this

                #pragma once
                #include "animals.h"

                class Marine :
                public Animals
                {
                public:
                Marine(void);
                virtual ~Marine(void);

                virtual void goOut();
                virtual void converse()= 0;
                virtual void getBack();
                

                };

                #include "Marine.h"

                Marine::Marine(void)
                {
                }

                Marine::~Marine(void)
                {
                }

                void Marine::goOut()
                {
                cout<<"The Marine animals: "<<"swim out and are fed"<<endl<<endl;
                }

                void Marine::getBack

                S Offline
                S Offline
                Sivyo
                wrote on last edited by
                #15

                noticed when moving around the protected names...

                protected:
                string m_weight;
                string m_name;
                string m_gender;

                the problem moves to gender. ect... very very strange

                L 1 Reply Last reply
                0
                • S Sivyo

                  noticed when moving around the protected names...

                  protected:
                  string m_weight;
                  string m_name;
                  string m_gender;

                  the problem moves to gender. ect... very very strange

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

                  Even stranger ... I have copied your code, compiled and run it and it works perfectly! May I suggest the extracts you have posted lack some information relevant to the problem.

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    I think there's nothing wrong in his method signature. Why should he pass the string by value? :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

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

                    Yes, the old brain only working on one cylinder...confusing string with char*. :sigh:

                    1 Reply Last reply
                    0
                    • D David Crow

                      Sivyo wrote:

                      I commented out set weight and it seems that is the problem, it doesn't crash if I take it out

                      Don't be so sure. You may have just moved the problem to another, less-sensitive location.

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      S Offline
                      S Offline
                      Sivyo
                      wrote on last edited by
                      #18

                      well, apparently something was wrong with that computer. I was working with it at school and now that I am home, the program works fine. Makes me feel really stupid.... worked 4 hours on that thing and the problem was with the computer. i didn't get anything done....

                      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