C++ Passing Variables Between Classes

Click For Summary

Discussion Overview

The discussion revolves around the challenges of passing variables between classes in C++, particularly focusing on issues related to garbage values being printed when attempting to display passed variables. The scope includes programming concepts and debugging techniques.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • A participant describes an issue with passing variables from one class to another and receiving garbage values when attempting to display them.
  • Another participant suggests that commas are necessary to separate the variables in the function call.
  • A later reply requests more information about the actual code, including variable declarations and expected parameter types, indicating that the provided sample is insufficient.
  • Participants express that garbage values could stem from various issues in the code, including potential pointer misuse.
  • Several participants agree that creating a small, complete example program could help isolate the problem.
  • One participant shares a personal anecdote about the importance of proper programming techniques and debugging, emphasizing that not initializing pointers is critical.

Areas of Agreement / Disagreement

Participants generally agree that a complete example program is beneficial for troubleshooting, but there is no consensus on the specific cause of the garbage values or the best approach to resolve the issue.

Contextual Notes

There are limitations in the discussion due to missing details about variable types, function parameter expectations, and the actual code structure, which may affect the understanding of the problem.

Who May Find This Useful

Individuals learning C++ programming, especially those dealing with class interactions and debugging issues related to variable passing.

chronie
Messages
23
Reaction score
0
Hi,

I am new to programming, but I think this is a simple error. For my programming code I am trying to pass a variable from a certain class to another class. The code is similar to below.Class 1
PHP:
#include "class2"

Class2 class

var1=1
var2=2

class.acceptingvariablesfrom1(var1 var2)

Class 2
PHP:
class.acceptingvariablesfrom1(var1 var2)

count << var1 << endl;

That is what my basic code looks like. I send my var1 and var2 to my acceptingvariablesclass however, how do I have them displayed. When I do my count statement I get garbage. Am I doing something wrong? Class1 and Class2 are different .cpp files. In Class2 do I have to define var1 and var2 again even though they were defined in class1?

I think this is a basic question but I just don't understand why I am getting garbage values?
 
Last edited:
Technology news on Phys.org
You need to use commas to separate the variables.
 
I'm sorry, in my real code I had commas. I am just confused as to why garbage values print out.
 
Post the actual code with declarations. This sample is nothing. (what are var1 and var2? ) And what param types is the function expecting?

Garbage could be from anywhere in your code if you trashed a pointer somewhere.

Create a small simple program that tests the classes, that will at least isolate whether it is the class or not.
 
Agreed. The best thing to do is construct a short, complete example program that can be pasted into a compiler and compiled successfully, and that demonstrates the problem.
 
Besides, with this approach 9 times out of 10 you will find the problem by yourself.
 
Borek said:
Besides, with this approach 9 times out of 10 you will find the problem by yourself.

Yep. My daughter is a Comp Eng and she took a Comp Sci programming course, which was C at her University. I was shocked at how the prof. didn't teach the students proper style or technique. She stopped showing me her projects because I would get on her.

Not initializing pointers? That is not a style preference, it is a must do.

I think that is where the experience really comes into play; debugging.
 

Similar threads

Replies
63
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 25 ·
Replies
25
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K