I am trying to do an assignment where I declare at first 4 variables (actually they are linked lists), and try to pass them through 10 functions where only the last one actually uses them.
Is there a way so that all my functions don't have to hold 4 parameters which they do not use, but they only pass? a kind of global variable accessed from anywhere (even if my functions are located in a header file)?
I tried to declare them as global variables outside the main, and I tried to declare them on a separate namespace, but it still gives me errors
Thank you for your time.
5 1855
I am trying to do an assignment where I declare at first 4 variables (actually they are linked lists), and try to pass them through 10 functions where only the last one actually uses them.
Is there a way so that all my functions don't have to hold 4 parameters which they do not use, but they only pass? a kind of global variable accessed from anywhere (even if my functions are located in a header file)?
I tried to declare them as global variables outside the main, and I tried to declare them on a separate namespace, but it still gives me errors
Thank you for your time.
If they are in same file as main,are they above includes that uses them?
If you can't manage this, declare the globals into a header file,where your functions are and then include it into the main file..
Savage
This is where objects in C++ come in handy. Take the 4 lists and make them part of an object, either as static or local variables.
Now all of the static/local methods in that object will have access to those lists without having to explicitly pass them.
For example, the static Convert method would be alble to access List1_ and List2 without any parameter passing. -
class ProcessSomething
-
{
-
static List<Int> List1_;
-
static List<Int> List2_; // etc.
-
-
public:
-
static Convert( void);
-
};
Great idea, I'll create a class (I'll call it Main) with private members the 4 lists, and all the functions are public. But so I don't have to call each function Main.function(), should've the functions be friend functions?
I'll try it right away. Thank you
I suppose there are ways/tricks to get rid of the Main:: prefix, but I actually prefer it. I use it like you would a namespace. BTW: For static routines you will use a Main::function declaration.
To avoid name duplication, you can make the class name more descriptive and then simplify the function name. For example, instead of Main::accountBalanceReport, you could have an class Account and then the function would be balanceReport. To me, Account::balanceReport makes a lot of sense. You are not stuck with one class for all of the functions
I retried to declare them with global variables and it worked.
I has to add the #if !defined statements.
Thank you.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lawrence |
last post by:
Those of you with backgrounds with the C language will laugh at my
mistake, but those of you, like myself, who deal mostly with PHP
should be warned about passing variables as references -...
|
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: Garma |
last post by:
According to what I have learnt so far, instantiating global objects should
be the last resort. Is there any reasons why so?
Sometimes some objects or their pointers have to be shared among...
|
by: Foxy Kav |
last post by:
Hi everyone, im a first year UNI student doing a programming subject
and im stuck on how to get rid of my global variables, char
stringarray and char emptystring. I was wondering if
anyone could...
|
by: jeffg |
last post by:
The answer probably is staring me in the face and I don't see it, so
I'll just ask.
I try to limit the scope of variables and avoid global variables. But
sometimes I get stumped by the following...
|
by: hello smith |
last post by:
I have a lot of functions that add values to an array. They alos update
a global variable of type int. Currently, I use a global variable to
hold this array. All functions access this array...
|
by: zholthran |
last post by:
Hi folks,
after reading several threads on this issue (-> subject) I fear that I
got a problem that cannot easily be solved by the offered workarounds
in an acceptable way, at least not with my...
|
by: andy |
last post by:
A question about about passing a class by reference:
Say you have a class called car, and within that you have two objects
called car01 and car02.
Within the class I have an int variable...
|
by: satterfieldben |
last post by:
I have a newbie question about passing variables between functions. I am wanting to use a drop down box to select a value. Then base on which was selected, it would create a variable and I would call...
|
by: johnjsforum |
last post by:
Buddies,
I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function
////////////////////////////////////////////////////////////////////////////////////...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |