0

I'm trying to write to variables 1 and 2 in Class3 from Class1.

I don't have any problem writing to any variable in Class2 from Class1. However, I can't seem to permanently write to a Class3 variable from Class1. I know they're receiving the right values because a cout within the read() method, after the assignments, will print the correct values. However, that same cout pasted into the print() method will not print the new values. It seems as though their getting temporarily written...

Sorry for the class madness

class Class1
{
public:
  Class1();
private:
  Class2 *myClass2Array;
};

Class1::Class1()
{
  myClass2Array = new Class2[size];
  myClass2Array[i].getArray(myClass2Array[i].getCount()).read(string1, string2); // this line is probably a problem
}

So, the constructor for Class1 is trying to call read() that's in Class3, which is declared in Class2...

class Class2
{
public:
  Class2();
  int getCount();
  Class3 getArray(int i) { return myClass3Array[i]; }
private:
  Class3 *myClass3Array;
  int count;
}; 

Class2::Class2()
{
  count = 0;
  myClass3Array = new Class3[8];
}

I've also tried Class3 myClass3Array[8]; and initializing each one in a for loop... Here's Class3

class Class3
{
  string var1;
  string var2;
public:
  Class3();
  void print();
  void read(string, string);
}; 


Class3::Class3()
{
  var1 = "";
  var2 = "";
}

void Class3::print()
{
  cout << var1 << " and " << var2 << endl; // will print old, initiated values
} // end function print()

void Class3::read(string string1, string string2)
{
  var1 = string1;
  var2 = string2;
  cout << var1 << " " << var2 << endl; // will print new values
} // end function read()

I'm guess my problem is either in the way I declared the array, or in how I'm trying to access (write to) it. Any ideas?

5
  • The problem(or a problem) is that you are not following the Rule of Three. You need to provide a copy constructor, copy assignment operator which will make deep copies of your dynamically allocated member(myClass3Array) in Class2. Alternatively, you can use a smart pointer instead of raw dynamically allocated member.
    – Alok Save
    May 6, 2013 at 4:41
  • You have almost enough here for a complete program. Suggest you fix the bits above that don't compile and have obvious bugs (Class1::Class1 for instance) and add a main function as well so we can try out your code.
    – john
    May 6, 2013 at 4:45
  • Oops, sorry for the typo. Just trying to give good context. Main isn't doing much here, just Class1 myClass1; The program compiles and runs, it just doesn't save the values I write to Class3
    – lambgrp425
    May 6, 2013 at 4:49
  • @Mark in Class1::Class1() you aren't allocating any memory for myClass2Array
    – john
    May 6, 2013 at 5:03
  • myClass2Array = new Class2[size];
    – lambgrp425
    May 6, 2013 at 5:05

1 Answer 1

1

The problem is that your Class2::getArray method is returning a copy.

Change it to return a reference

Class3& getArray(int i) { return myClass3Array[i]; }

The problem was that you are setting the values of a copy of the Class3 object, not on the original in the Class2 object. That's why your values don't stick.

Another way is to add a setArray method

void setArray(int i, const Class3& a) { myClass3Array[i] = a; }

then

Class3 c3;
c3.read(string1, string2);
myClass2Array[i].setArray(myClass2Array[i].getCount(), c3);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.