-1

Sorry in advance as I am a novice. I'm struggling with pointers in general, and this case is no different. When I call my getter that's supposed to output elements of an array, I get a random long integer that changes every time I run the program. I know the code is all over the place. I took out as much as possible to keep it short, but let me know if more is needed.

Class Class1 {

public:

const static int SIZE = 4;

// Actual project is split .h and .cpp

Class1() {
  for (int i = 0; i < SIZE; ++i) {
    numThings[i] = 0;
  }
}
Class1(int* thingsPtr) {
  SetNumThings(thingsPtr);
}

void SetNumThings(int numThings[]) {
  for (int i = 0; i < SIZE; ++i) {
    this->numThings[i] = numThings[i];
  }
}
int GetNumThings() {
  return *thingsPtr;
}

// Calling GetNumThings() in my Print() function outputs garbage

private:
int numThings[SIZE];
int* thingsPtr = numThings;

Kyle
  • 11
  • 2
  • `int GetNumThings() {` did you want to return a single integer? – drescherjm Apr 29 '22 at 03:00
  • Ideally I'd be able to return all integers separated by a comma, but even just the first integer at this point would be better than the garbage output! – Kyle Apr 29 '22 at 03:06
  • 1
    The code shown looks fine (though you don't need the `thingsPtr` member at all). Please provide a [mcve] showing the problem in action. You are likely making a mistake in your other code not shown here, such as calling `GetNumThings()` on an invalid `Class1` object. – Remy Lebeau Apr 29 '22 at 03:08
  • 1
    Your getter does not output elements of the array. It returns the value of the first element of the array only. You could just do away with `thingsPtr` entirely, and achieve the same effect by changing `GetNumThings()` so it returns `*numThings` or (equivalently) `numThings[0]`. – Peter Apr 29 '22 at 03:08
  • 2
    Having a pointer as a member-variable means that you'll get bad behavior if you try to copy a `Class1` object without creating an explicit copy-constructor and/or assignment operator to handle the pointer appropriately. If you don't want to implement those things, you might want to declare them privately or (as `= delete`) so the compiler will catch any attempts to copy a `Class1` at compile-time for you, and spare you the pain of run-time debugging that problem. https://stackoverflow.com/questions/6077143/disable-copy-constructor – Jeremy Friesner Apr 29 '22 at 03:09
  • *"Sorry in advance as I am a novice."* -- save the apologies for the end. Apologizing in advance just means fewer people will take the time to read your question. Same goes for explanations/excuses ("I'm struggling [...]"). As advised in [ask], get to the point right away. Start with the question or at least the setting ("When I [X], I get [Z]"). – JaMiT Apr 29 '22 at 03:13
  • @JeremyFriesner Yep. That's likely exactly what's happening and would happen if, for instance, he passes the Class1 object by value instead of reference to his print function. And just copying kills it too. – doug Apr 29 '22 at 03:14
  • *"but let me know if more [code] is needed."* -- this is something you can verify yourself. Head over to an online compiler (there's a list in [the c++ tag info](https://stackoverflow.com/tags/c%2b%2b/info)), copy the code from your question, paste into the compiler, and see if trying to run your code reproduces the issue. *That's the procedure I use most of the time when I want to see a question's issue in action. In this case, I got `error: 'Class' does not name a type; did you mean 'class'?`* – JaMiT Apr 29 '22 at 03:19
  • I'm going to be slowly going through all of the feedback. Thank you! – Kyle Apr 29 '22 at 03:36

0 Answers0