Return modified object

Asked

Viewed 356 times

6

I have the following problem, as the didactic example below:

    #include "b.h"
    #include "c.h"
    class A
    {
        public:
           int start();
        private:            
            B b;
            C c;
            int x;      


    }

    a.cpp

    int A::start()
    {
        c = b.copy();
        x = c.d;
        return x;
    }



    #include "c.h"
    class B
    {
        private:
            C c;        
        public:
            C copy();
    }

    b.cpp

    C B::copy()
    {
        c.add(1);   
        return c;
    }


    class C
    {           
        public:
            void add(int x);
            int d;
    }

    c.cpp

    void c.add(int x)
    {
        C::d = x + 10;
        return;
    }


    main()
   {   
      A a;
      int y;

      y = a.start();

      cout << y ;

   }

When compiling it shows the error "error: use of Deleted Function", because when returning the object c this no longer exists.

How can I get around such a situation, knowing I have to return to the class To the modified object of the class C and that the only function in the class C must be of the void type.

It would already help a way to read the variable d (date member) of the class C in class To

1 answer

4


There’s a lot of syntax errors in there, very much just typo. Programming is not playing any text and anyway it will work. I made some modifications and at least compiled. I don’t know if you do what you expect.

class C {
    private:
        int d;
    public:
        void add(int x);
};

void C::add(int x) {
    d = x + 10;
    return;
}

class B {
    private:
        C c;        
    public:
        C copy();
};

C B::copy() {
    c.add(1);   
    return c;
}

class A {
    private:
        void start();
        B b;
        C c;        
};

void A::start() {
    c = b.copy();
}

int main() {}

Behold working (compiling and running) on ideone. And in the repl it.. Also put on the Github for future reference.

  • I was really unhappy in transforming a very complex code, in a didactic example (as described), to exemplify the compilation error, but this code does not compile in the Linux platform, because the C class is destroyed before it passes, therefore of the described error. What I intend to receive is the object of class C (which has been modified) in class A.

  • I can only answer what is in the question and showed that taking out the syntax errors compiles. More than that I could not do. If you have other problems ask a new question, this time ask a [mcve].

  • I made some modifications to the original code. So I can access the date int d member of class C from class A.

  • @Morse you have to ask another question, that, as you posted, was answered. See the [tour] and the [Ask].

Browser other questions tagged

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