C++ :: Using Pointers Instead Of Reference Variables In Function
Mar 26, 2013
The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, and then demonstrate the function in a complete program.
int doSomething(int &x, int &y)
{
int temp =x;
x = y * 10;
y = temp * 10;
return x + y;
}
I understand how to covert the reference variables to pointers, however I am stuck on this error. Either I get the error listed in the title or (with a few changes) the error "invalid conversion from 'int' to 'int*'"
What am I doing incorrectly?
#include <iostream>
using namespace std;
int doSomething(int*, int*);
int main(){
int X, Y, result;
[Code] ....
I have multiplied both x and y by 10 and then added them together!
Here is the result " //I really didn't know how else to use the "doSomething" function in a meaningful way. So... I just stated what the function does.
<< result << ".
";
system("PAUSE");
return 0;
}
int doSomthing(int *x, int *y)
[Code] .....
View 1 Replies
ADVERTISEMENT
Sep 28, 2014
I am going to read some codes about image processing and I need to understand functions like this one below?
BOOL Trans_Geo_Affine(CImage *pImgSrc, CImage *pImgDst, BOOL bMatrix,
BOOL bHvInversed, double fScale_xx, double fFlex_xy, double fSkew_yx,
double fRotate_yy, double fTx, double fTy, int nInterpolation, BOOL bResize, BYTE cFill)
[URL]
two parameters, CImage *pImgSrc and CImage *pImgDst. I think they are class pointers and the function is passed by reference. What should I learn to understand this function and its parameters? How should I use this function? how to use the function with two parameters CImage *pImgSrc and CImage *pImgDst.
View 11 Replies
View Related
Mar 4, 2013
If f1 and f2 are two user defined functions.
main(){
int *p;
f1(&p)
}
f1(**p){
f2(&p);//
If I've to pass the pointer by reference again in another function, will I've to do something like this?
}
f2(***p){
**p = NULL;
}
View 3 Replies
View Related
Dec 4, 2014
How to how to make a function prototype that uses variables by reference. I'm making a decision based game where two running totals of two variables (ending and morality from decisions made) will decide the game outcome. I only have a few modules put in so far and most of the "story" parts cut down here to save space. I'm also getting an error saying there is more than one instance of overloaded function for the "whatToDo" module.
// ZombieGame.cpp : Defines the entry point for the console application.
//
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <iostream>
using namespace std;
//These are function prototypes to declare the functions being used
void WakeyWakey();
void TwentyMinsLater(int);
[Code] .....
View 2 Replies
View Related
Sep 10, 2014
I define an integer variable and pass it by reference to a function.
int func(int &x)
{
// do something
}
int x;
func(x);
However, I have not only one variable but an array of variables with its predefined name. How do I pass to the function by using loop? Example:
int x, y, z;
func(x);
func(y);
func(z);
How can I do this by using loop?
View 7 Replies
View Related
Nov 23, 2014
so I'm trying to rework some code that solves sets of equations by gaussian elimination and wanted to change the array elements to pointers. Below i've put my c code and the custom header file that goes with it.
Header file
#ifndef CHAPTER5_8_H
#define CHAPTER5_8_H
#define N 5
#define INPUT_FILENAME "equations.txt"
[Code].....
View 1 Replies
View Related
Mar 9, 2013
i'm still unclear between the difference between using pointer and a reference
I understood the concept of pointers in c in the class i took last year
and that was to change the actual value stored in the memory address Code:
void change_a(int a*){
a=6;
}
int main(){
int a=5;
change_a(&a);
}
but in c++ I've been using references in all my assignments because I don't know how to correctly use pointers in c++ I may have missed a class but I'm on spring break and would like to clear things up
so in c++
in my assignments I would call it like this Code:
void change_a(int &a){
a=6;
}
int main(){
int a=5;
change_a(a);
}
so does this change the value in the address or does it make another copy of a in my c++ code and stores 6 in that copy
View 2 Replies
View Related
Oct 15, 2014
I have made an application and I have basically solved everything. But the only problem is that I am using global variables because it felt like the smoothest, so my program is built on it.
But now I've read around and I understand that you should not use these(?). Do you think pointers is the best think to use instead?I have previously declared my board array and some variables as global and I want them in alot of functions.I have read and understand the procedure for the use of pointers so I can use my int's in the other functions by doing like this? Code: #include <stdio.h>
int justprint();
int main()
{
int Row = 2;
int Column = 2;
int *pRow = &Row;
int *pColumn = &Column;
[code]...
But how do I do it with an array like this one? If I declare it in the main function, and then want to use it in other functions.Or are there better, easier solutions?
Code: char game[3][3]={{0,0}};
View 13 Replies
View Related
Aug 9, 2013
I really do not see the difference between these two declarations:
int myvariable;
int * mypointer;
It is said that when you define a pointer, instead of containing actual data, it contains a pointer to the memory location where information can be found.
But doesn't the other variable declaration do the same? It obviously doesn't have data either. And it must be stored in a memory location as well. So I do not see the difference.
View 6 Replies
View Related
Apr 23, 2013
I have the following code. According to this the values of pointers p[0] and p[1] remains unchanged since the swap is made to local variables in swap function.Now my doubt is how can I swap the pointers p[0] and p[1] inside the function swap??
Code:
#include<stdio.h>int main(){char*p[2]={"hello","good morning"};
swap(p[0],p[1]);
printf("%s %s",p[0],p[1]);return0;
}void swap(char*a,char*b){char*t; t=a; a=b; b=t;
}
View 5 Replies
View Related
Feb 28, 2014
Which is more efficient in functions? Returning values or using pointers to redefine variables passed as arguments?
I mean either using:
void ptr_Func(int *x)
{
*x = *x+1
}
or
int ptr_Func(int x)
{
return x + 1;
}
In terms of speed, memory use etc.I want to know general efficiency, I know it will obviously vary with different uses and circumstances.
View 7 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
Jan 19, 2013
The error is this:
#include <iostream>
using namespace std;
void add(int s);
void subtract(int d);
void multiply(int p);
void divide(int q);
[Code] .....
View 2 Replies
View Related
Nov 23, 2013
I'm making a program that's essentially a Text-Based Fire Emblem game; it runs calculations and rolls dice and has all sorts of Goodies. However, I have hit a block to the tune of
#ifndef ITEM_H
#define ITEM_H
class Item
{
[Code]....
Up Until I called up a Sword object, it worked fine. But when I compiled it, I got an Undefined Reference to Item::Item() error in Line 8 of Weapon.cpp.
View 2 Replies
View Related
Feb 15, 2015
#include<iostream>
using namespace std;
int &fun() {
int x = 10;
return x;
}
int main() {
fun() = 30;
cout << fun();
return 0;
}
The code outputs 10.
Shouldn't it show an error because x is created locally on stack and gets destroyed on function return?
View 1 Replies
View Related
Oct 2, 2014
Everything seems to be in order and I know my code still has mistakes. I'm just trying to get it to compile and it won't allow it. I've narrowed it down to when I call the functions in main but beyond that I have no clue.
#include <iostream>
#include <cstring>
using namespace std;
void getSize(int num);
void getSpace(int num, int ptr);
void inputData();
void printData();
void destroy();
const int BIG_NUMBER = 100;
[code]....
View 4 Replies
View Related
Oct 24, 2013
So I have a really strange problem occurring...
First, here are the files I'm using:
//pa4.cpp wirtten by Syd Frederick
#include<iostream>
#include<string>
#include<fstream>
[Code].....
When compiling I'm getting a strange error that says :
/tmp/ccdt0Bf9.o: In function `main':
pa4.cpp:(.text+0x1c): undefined reference to `synopsis()'
pa4.cpp:(.text+0x1e7): undefined reference to `execute(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
View 2 Replies
View Related
Aug 29, 2013
Having a little trouble getting my function to display the results. I got my getNumber function working and I know I can't use return to return multiple values so I created a function that would display my results but I am a little lost on how I get the results from one function to another.
#include <iostream>
#include <array>
#include <iomanip>
using namespace std;
void printResult(int, int);
void getNumber(int &, int &);
void printResults (int, int){
int n, n1;
[code].....
View 13 Replies
View Related
Feb 20, 2015
Been away from c++ for a few months now (apart from answering a few questions on this site). I was trying to see if I could do a simple program using just my memory that involved unique_ptr's, but getting an error:
"attempting to reference a deleted function". I thought that by getting a reference to my pointer would not invoke a copy constructor but i'm clearly wrong.
Here's the code:
#include <iostream>
#include <memory>
#include <vector>
class Shape {
public:
virtual void Draw() = 0;
[Code] ....
View 5 Replies
View Related
Feb 22, 2015
So I was reading my book and it listed this piece of code. The first piece of code is in the book and the 2nd is just my test on the piece of code. I am curious as to why in the functions parameters there is a reference to aString. I've noticed that removing it has no affect on the outcome of the code.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool isPalindrome (string & aString) {
[Code] ....
View 2 Replies
View Related
Oct 21, 2013
I am coding a RTS game but I cant compile my dataLoader function. It gives that errors when want to call it:
//Window size
int width;
int height;
if( !dataLoader<int>( width, "settings/resolution.txt", "width" ) || !dataLoader<int>( height, "settings/resolution.txt", "height" ) )
[Code] ....
View 7 Replies
View Related
Jun 3, 2014
I dont know what kind of return reference I have to put to override the following derived member function in C++:
virtual SEntityPhysicalizeParams& GetPhysicsParams() override {return ???;}
The place where I should put the return value is marked with ???. SEntityPhysicalizeParams is a struct from another header from which I dont have access to it's source file.
I tried several things but noone seemed to work out and keep getting me either error "function must return a value" or "initial value of reference to non-const must be an lValue".
Here is SEntityPhysicalize where my function is refering to:
struct SEntityPhysicalizeParams
{///////////////////////////////////////////////////////////////////
SEntityPhysicalizeParams() : type(0),density(-1),mass(-1),nSlot(-1),nFlagsOR(0),nFlagsAND(UINT_MAX),
pAttachToEntity(NULL),nAttachToPart(-1),fStiffnessScale(0), bCopyJointVelocities(false),
pParticle(NULL),pBuoyancy(NULL),pPlayerDimensions(NULL),pPlayerDynamics(NULL),
pCar(NULL),pAreaDef(NULL),nLod(0),szPropsOverride(0) {};
[Code] ....
View 8 Replies
View Related
Nov 19, 2013
I am currently working on a code, but I keep getting a error as follows:
[Linker error] undefined reference to 'getHoursOfTheWeek(double)'
[Linker error] undefined reference to 'getGrossPay(double)'
[Linker error] undefined reference to 'getStateTax(double)'
[Linker error] undefined reference to 'getFederalTax(double)'
[Linker error] undefined reference to 'getFICA(double)'
[Linker error] undefined reference to 'getWithHoldingAmount(double)'
I have been trying to debug this error for hours and I am completely stumped....
//BEGIN
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
string getUserName();
double getPayRate();
[Code] .....
View 2 Replies
View Related
Nov 28, 2013
I have a class called Point that has functions for getting and setting x, y, and z coords., distance, and midpoint. I need to write a function that prints the class and must use ostream & displayPoint( ostream & ); as the prototype. I did some googling and came up with
std::ostream& Point::displayPoint(std::ostream& out)
{
out << "(" << out.getx() << ", " << out.gety() << ", " << out.getz();
return out;
}
in order to print (x, y, z).
My IDE (Xcode) says "No member named 'getx' in std::__1::basic_ostream<char>"
I don't understand much about ostream reference
View 1 Replies
View Related
Mar 16, 2015
I'm getting an undefined reference error to a function, and I can't figure out why. I have tried letting code blocks compile the files, I have tried the command line to compile it with the same results.
I looked up the error and found this from [URL]
undefined reference
Example
/tmp/cc2Q0kRa.o: In function `main':
/tmp/cc2Q0kRa.o(.text+0x18): undefined reference to `Print(int)'
collect2: ld returned 1 exit status
Meaning / Your code called the function Print, but the linker could not find the code for it in any .o file
Usual Causes
You forgot to link the .o file that contains the function
You misspelled the name of the function
You spelled the name of the function correctly, but the parameter list is different in someway
which seems to be the error I get. I have double checked all 4 and I see nothing.
The code that specifically gives me the error is:
Item *name = new Item(desc, id, weight, loc);
itemMap.addItem(name);
and the class looks like this:
class Item // Standard Items {
private:
std::string name;
std::string desc;
int id;
int weight;
int loc;
[code].....
I think everything matches up unless I'm just missing it.
Full codes are located here: [URL]
what I'm doing wrong?
View 5 Replies
View Related
Sep 25, 2012
I created the following code to pass the the variable 'inputVoltage' by reference to the function 'input'. It certainly works when I run the program, but I dont think it is a standard way of doing it, i.e. the use of '*' and '&' is not according to convention ? Or perhaps the way did it is acceptable ?
int input (double *inputVoltage);
int main ( {
double inputVoltage;
input(&inputVoltage);
[Code]....
View 2 Replies
View Related