C/C++ :: Pass Values By Reference?
Nov 7, 2014
I built a program that finds the average amount of days missed per employee. I am now attempting to modify my program to pass values by reference rather than passing by value. I created 3 functions:
int numOfEmployees(); int numOfDays(int); double avgDays(int, int);
in the prototype I use the ampersand sign int numOfDays(int&);
in the actual function I use numOfDays(int& employees)
I am just not able to get the call to the function to work the program will crash when I modify it with the ampersand signs. I know I have to re-work my program, but I am having a hard time understanding how.
This is the original code I am trying to modify:
#include <iostream>
using namespace std;
int numOfEmployees(); //Prototype for numOfEmployees
int numOfDays(int); //Prototype for numOfDays
double avgDays(int, int); //Prototype for avgDays
[Code]......
View 1 Replies
ADVERTISEMENT
Nov 7, 2014
I built a program that finds the average amount of days missed per employee. I am now attempting to modify my program to pass values by reference rather than passing by value. I created 3 functions: int numOfEmployees(); int numOfDays(int); double avgDays(int, int);
in the prototype I use the ampersand sign int numOfDays(int&);
in the actual function I use numOfDays(int& employees)
I am just not able to get the call to the function to work the program will crash when I modify it with the ampersand signs.This is the original code I am trying to modify:
#include <iostream>
using namespace std;
int numOfEmployees(); //Prototype for numOfEmployees
int numOfDays(int); //Prototype for numOfDays
double avgDays(int, int); //Prototype for avgDays
[code]....
View 1 Replies
View Related
Jan 23, 2013
I've been given an assignment with the below questions.
1. What is the difference between pass by reference & pass by pointers?
2. What is the use of the initialization list in the constructor?
3. What is meant by a reference & its advantage?
4. Class has a reference, pointer and a const. Is it possible to write the copy constructor & assignment operator overloading funciton? how? ( Since reference is there, I'm not sure on how to write for it)
5. Example for a variable decleration and definition? (I know for function but for variable don kw how)
6. static and const static what is the difference??
View 1 Replies
View Related
Jul 2, 2014
// explain difference between pass by value and pass by reference
void addOne(int i)
{
i++;
}
void addOne(int& i)
{
i++;
}
View 2 Replies
View Related
Apr 17, 2013
i think i understand both concept. i know that pass by value is that the function receiving those values makes actually a copy of those parameters. Passing by reference makes the variable in the main function to actually see those changes in the other function, instead of a copy, and apply them to the variable in the main function. my question is, why would i pass it by reference if i just can make a return type function.
View 5 Replies
View Related
Nov 27, 2014
I need to put my Dice() function as pass by reference and something as a pass by value, it can be in the same function.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
[Code].....
View 6 Replies
View Related
Nov 25, 2013
(Pass-by-Value vs. Pass-by-Reference)
Write a complete C++ program with the two alternate functions specified below, each of which simply triples the variable count defined in main. Then compare and contrast the two approaches. These two functions are
a) function tripleByValue that passes a copy of count by value, triples the copy and returns the new value and
b) function tripleByReference that passes count by reference via a reference parameter and triples the original value of count through its alias (i.e., the reference parameter).
View 7 Replies
View Related
Apr 10, 2014
I need to pass an array of 10 instances of a custom class to a function. The snippets of code are posted below. How would I do this right?
The prototype:
Code:
int Search(Vertex vertex[], ofstream &outfile);
The implementation in the main function.
Code:
Search(vertex[10], outfile);
View 2 Replies
View Related
Mar 15, 2013
How do you pass 2D arrays by reference??
View 2 Replies
View Related
Feb 9, 2015
//program to form a header file
/* using pass by reference in pointer */
#include <iostream>
#include<math.h>
[Code].....
View 14 Replies
View Related
May 13, 2014
I've written some code that I am currently threading but I am unsure how to pass by reference, or rather why my pass by reference is failing.
I am passing an array of floats by reference, this works fine when not threaded but I am given the error that float*&field does not match std::reference_wrapper<float*>.
Anyways. Here's the code.
The method:
diffuse(int b, float*& field, float*& pField, float diffFactor, float dt, int iteration)
The thread:
std::thread diffuseThread(diffuse, 1, std::ref(u), std::ref(prevU), visc, dt, iteration);
View 14 Replies
View Related
Jun 25, 2013
I am having trouble working with third party dll's, libs and header files. I am trying to call a function.here is the function that is suppose to be called.
bool COAuthSDK::GetRequestToken(CClientDetails &objClientDetails)
it has this info of what it needs :
Name IN/OUT Description
m_environment IN Optional. Possible values are SANDBOX (default) and LIVE.
m_strConsumerKey IN OAuth consumer key provided by E*TRADE
m_strConsumerSecret IN OAuth consumer secret provided by E*TRADE
m_strToken OUT Returned by the function if successful
m_strTokenSecret OUT Returned by the function if successful
m_strCallback IN Optional; default value is "oob"
here is the COAuthSDK header
#ifndef _OAUTHSDK_H_INCLUDED_
#define _OAUTHSDK_H_INCLUDED_
#include "ETCOMMONCommonDefs.h"
#include "ETCOMMONOAuthHelper.h"
using namespace std;
[code].....
when I try to build the function it says that its in the dll's so I know I have to call it.here is the link to the build site if needed URL....
View 1 Replies
View Related
Jan 24, 2013
What is the difference in pass by pointer and pass by reference? As per my understanding, there is no difference much.If a function accepts pointer, then NULL check can be performed.other than this i'm not able to see any big difference..
View 3 Replies
View Related
Feb 3, 2014
I cant get my insert function to insert to the head of the list I dont think I'm passing the pointers correctly.
#include <iostream>
using namespace std ;
struct list1 {
int data ;
list1 * next = NULL;
[Code] .....
View 4 Replies
View Related
Mar 17, 2014
I'm trying to pass a class object by reference.
total = mathfunction(i);
}
double mathfunction(retirement& j)
{
double R = 0.00, m = 0.00, r = 0.00, t = 0.00, totaled = 0.00,
numerator = 0.00, denom = 0.00, temp = 0.00;
[Code]....
I only tried to pass by reference because I figured passing by value would be a larger pain in the neck.
View 1 Replies
View Related
Mar 14, 2013
I have a struct which has an array inside of it:
struct someStruct{
int structArray[999];}
Now when I want to access that array, I have the following:
ptrSomeStruct->structArray[someIndex];
But now I want to pass structArray to this function by reference so that it can adjust the array as needed and I can continue to use the array back in my caller function:
void adjustArray(void *& someArray){}
How do I accomplish this?
View 2 Replies
View Related
Jul 30, 2014
I just want to know if there is any real difference between the two below, if yes, when would i use one over the other? I would thought the "&" is pointless in below function, as far as the data is concerned.., the only things is with "&", if the pointer address value is changed in Test function, it will affect the caller's copy of data. Both function should behave the same if data is changed.
Code:
Between
void Test(QSharedPointer<Data> data)
{
}
and
void Test(QSharedPointer<Data> & data)
{
}
View 6 Replies
View Related
Sep 19, 2014
Due to the nature of this requirement, I've made a very minimal example, which would adequately solve my issue, without resorting to use of pointers or copy constructors.
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
[Code] ....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
View 1 Replies
View Related
Oct 4, 2013
I don't understand how my code not run.
#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;
[Code] ....
View 3 Replies
View Related
Apr 21, 2014
I am trying use a print function to print out data in a struct. My questions are:
1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?
2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct Inventory {
[Code] .....
View 9 Replies
View Related
Jan 30, 2013
I'm trying to pass an reference of the object ifstream to my constructor as such:
// myClass.cpp
int main(){
ifstream file;
myClass classobj = myClass(file);}
I am defining the constructor as follows, in myClass.cpp:
myClass::myClass(ifstream &file){
// do stuff
}
I get the error of "No overloaded function of myClass::myClass matches the specified type."
Also, when I build the program, it tells me "syntax error: identifier 'ifstream'"
Also, I made sure I included this:
#include <iostream>
#include <fstream>
#include <sstream>
What am I doing wrong? How can I pass an ifstream reference to my constructor?
View 3 Replies
View Related
Jan 19, 2012
I'm using mingw with gcc 4.4.1. I'm trying to write a function that operates only on unique objects that have been explicitly moved.
Code:
#include <iostream>
#include <memory>
typedef std::unique_ptr<int> unique;
void eat(unique&& i)
{
std::cout << *i << std::endl;
[Code] ....
This is confusing to me because it compiles, yet my unique i is passed by R-value reference, without ever being explicitly moved. I thought named objects could NEVER be considered R-value references... Am I wrong to think that the above is extremely dangerous? After the call to "eat", my "i" has been destroyed internally, I could save by passing by value:
Code:
#include <iostream>
#include <memory>
typedef std::unique_ptr<int> unique;
void eat(unique i) {
std::cout << *i << std::endl;
[Code] ....
But at this point, I'm paying for a (theoretically cheap) move copy, when I could be paying nothing at all... A third solution would be to overload with a const ref version, but at this point, I get an obscure linker error, when I'm really looking for a compiler error...
Code:
#include <iostream>
#include <memory>
typedef std::unique_ptr<int> unique;
void eat(const unique&);
void eat(unique&& i)
[Code] ....
I'm actually trying to write a container that manages unique objects, and was trying to write a function that accepts only moved objects.
View 4 Replies
View Related
Nov 27, 2014
i have a project where i create a dice game, the user rolls 2 dice and the computer roles 2 dice. the player that wins 3 out of 5 rolls wins the game. I have completed the requirements, but i wanted to create a pass by value function for "void Dice()", I'm not too sure how that works?
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//creating my variables and their values
int compinput;
[Code] .....
View 5 Replies
View Related
Jan 15, 2015
Well I know how to pass values between forms using get and set properties. But the problem with it is everytime I want to pass values from form2 to form1 it doesn't appear. To make it appear I have to type form1.Show(); which open form1 a second time and then show the value. Is there any way I could make it appear without using form1.Show();?
View 9 Replies
View Related
Mar 4, 2015
I need to pass multiple values from one function to another and how to do this. Here is my code so far.
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
//Function Prototypes//
int charString(string, string, string);
string reverseString(string, string, string);
[Code]...
I basically need to take line1, line2, and line3 and return them to the reverseString function. Also, I am not allowed to do anything like make my own classes. I have to stick to the basics and no higher level programming techniques since we have not learned them yet.
View 5 Replies
View Related
Dec 29, 2012
Code:
struct NewPlayer {
int level;
int intelligence;
int damage;
};
int CharacterInfo(NewPlayer MageWizard, int Clevel,int Cint, int Cdam)
[Code] .....
View 1 Replies
View Related