C++ :: Transfer Ownerships Between Objects (passing Arguments) Using Raw Pointers?
Sep 4, 2012
Code:
void Class1::Func(shared_ptr<type1> parameter)
{
}
or
void Class1::Func(const shared_ptr<type1>& parameter)
{
}
or
Should I ever pass arguments/parameters to other objects using shared_ptr's or raw pointers?
View 3 Replies
ADVERTISEMENT
Aug 23, 2013
I'm having some problems with a function. The function is supposed to find the two largest values in an array.
Code:
void find_two_largest( const int *a, int n, int *largest, int *second_largest){
largest = a;
int temp;
second_largest = a;
for ( int i = 1; i < n; i++){
if (*(a + i) > *largest){
temp = *largest;
[Code]....
I don't see any mistake with the code of the function, but when I try to call it inside my program it only returns 0 for both largest and second_largest.
Code:
int *find_middle( int *a, int n);
void find_two_largest(const int *a, int n, int *largest, int *second_largest);
int main()
{
int n;
[Code]...
Do I have to declare the variables largest and second_largest as normal integer variables and then pass their addresses as arguments to find_largest or is that incorrect?
View 10 Replies
View Related
Feb 4, 2013
Having a little trouble passing arguments to functions.
I wrote this simple program to get the hang of it but I'm quite stuck. I'm sure you will be able to get at what i want the program to do...
Code:
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <windows.h>
void menu(int HP,int Gold,int Armour);
[Code] ....
View 3 Replies
View Related
Feb 10, 2015
I have a 1wire program from maxim running in visual studio. There is this argument in the main function that requires the com port to be specified the command line. If I do pass it as "COM1" the program works as expected.
I don't want to depend on having to pass "COM1" in the command line and into main. I've tried creating a string for COM1 and passing it right into the if function but it doesn't work.
Code:
int main(int argc, char **argv) {
int len, addr, page, answer, i;
int done = FALSE;
SMALLINT bank = 1;
uchar data[552];
[Code] .....
View 1 Replies
View Related
Oct 7, 2014
I know that passing arguments by const instead of value is more efficient and allows us to avoid allocating a temporary local variable of the argument type. But is this always true? Or are there some cases when calling functions with constant arguments should be avoided? If so, is passing by pointer the most efficient way?
View 6 Replies
View Related
May 5, 2014
Say I have a function pointer with this definition:
void ( *pressFunc ) ( void*, void* );
And i did this function:
void functionWithOneArg ( void* testPtr );
And i did this
pressFunc = &functionWithOneArg;
One. Would C actually let me do this? ( Assigning a function with one argument to a function with two )
Two. If so, what would happen to the second argument that is passed the function when its called? Does it just get 'cut off' and only the first argument is passed?
View 2 Replies
View Related
Jan 29, 2015
I always have confusions while using pointers with functions both as arguments and as return type.
View 1 Replies
View Related
Mar 12, 2014
I have a program that is working very well when I pass C++ vectors as arguments to my functions by reference, but I get some compilation errors when try to make a modification. I am also posting the entire program and its output below. so that you can see what is going on. I have commented out the line that causes an error.(Some of the indentation that got corrupted when I copied the code to the browser.)
This program basically calculates the coefficients of a least square polynomial and then evaluates this polynomial at artificial data points and verifies that this actually reproduces the original data within reasonable floating point error.
The function that computes the coefficients of the least square polynomial is Code: vector<double> LSPVecValued_GSL( const int, const vector<float> &, const vector<float> &); and as you can see it returns a vector by value, and this vector contains the coefficients of the least square polynomial.
There is also a function that evaluates this polynomial by accepting a vector argument by reference : Code: float evaluate_polynomial(double, vector<double>& ) ; I have also created another version of the evaluation function which accepts the same vector argument by value: Code: float evaluate_polynomial_ByValue(double t, vector<double> vec_a) ; In the program I call the first evaluation function (whose vector argument is passed by reference) by first using an intermediate vector variable containing the coefficients, and then I pass this vector as an argument to the evaluation function, as follows:
Code:
vec_a = LSPVecValued_GSL( deg, vec_x , vec_y);
for(int j=0; j< n ; j=j+20 ) {
cout<<"x["<<j<<"] = " << vec_x[j] << " ,y["<<j<<"] = " << vec_y[j] <<" , p(x["<<j<<"]) ( EVALUATED FROM REFERENCE) = "
<< evaluate_polynomial( vec_x[j], vec_a) << endl; // This version works without error
[Code] .....
As you can see above, I am also able to call the second evaluation function (the one whose vector argument is passed by value) directly by plugging in the function LSPVecValued_GSL"(...)" and this works without error, and this is a one step process, only one line of code is involved.
However, I get a compilation error (line number 12 that I have commented out above) if I try to plug in the function "LSPVecValued_GSL(...)" into the first evaluation function that expects a vector argument by reference. I tried to put a "&" in front ofLSPVecValued_GSL but this did not fix the bug.
What syntax is appropriate to use the first evaluation function (which accepts a vector argument by reference) if I want to plug in the vector-valued function LeastSquarePolynomial_GSL directly in the the first version of the evaluation function which expects a vector argument by reference?
View 14 Replies
View Related
Dec 9, 2013
I am trying to make quicksort and binary search and I get error when I am passing dynamic array to argument. It also says error during initialization of the dynamic array.
//.h file
#ifndef SortableArray_h
#define SortableArray_h
#include <iostream>
#include <string>
[Code] ....
View 2 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
Feb 14, 2013
I am using visual studio 2012 and i pass three command line arguments as 10 20 30 and when i m compile the program get error.....
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main(int n,char **p) {
int sum=0,i;
if(n>=2)
[Code] .....
View 2 Replies
View Related
Jun 12, 2014
Suppose if i write a test program like
Code:
void function1(unsigned int var1);
int main(void) {
function1(-3);
}
void function1(unsigned int var1) {
printf("%d", var1);
}
The output is -3. how it happens the argument is unsigned but iam passing signed but still prints the signed value. My bigger question is how the arguments are handled if the passing parameters are different types compared to declaration.
View 2 Replies
View Related
Jun 6, 2013
I am using OpenCV to read and manipulate a set of images, which I need to store in an array to work with them. Here is a snippet of the code:
#define MAX_IMAGES 8
typedef Mat* MatPtr;
int main(int argc, char** argv) {
char imageName[] = "./res/imageX.tiff";
MatPtr datacube[MAX_IMAGES];
[code].....
I have an array of pointers to Mat objects (an OpenCV class used to hold info and data about an image), which I will use to store the images. The function imread reads an image and returns a Mat object loaded with the relevant data about the image.However, this gives me a nice segfault when the assignment takes place. Of course, I can swap it with the following code, but since I'm working with big images (2048x2048 and upwards), it's really inefficient:
for(unsigned int i = 0; i < MAX_IMAGES; i++) {
imageName[11] = 49 + i;
datacube[i] = new Mat(imread(imageName, -1));
}
Is there any way to do this elegantly and without much hassle?Again, excuse my rustiness and anything completely stupid I might have said. It's been a long time since I worked with C++. Managed to circumvent the problem by using a STD vector instead of an array. I'd still like to know the answer to this riddle...
View 6 Replies
View Related
Sep 27, 2013
I've created an Array of pointers to objects using:
Person ** A = new person * [arraysize];
When I intend to access a specific person do I have to do this? :
something = A->[i];
and when I want a specific object within my struct do I have to do this? :
something_else = A->[i]->random_int;
View 10 Replies
View Related
Dec 5, 2013
my code:
int OKCount=0;
int WaitingCount=0;
int ReservationCount=0;
Flight::Flight(int capacity, int waitingMax) {
seats=capacity;
[code].....
reservations is a data member in the class flight as:
Reservation **reservations;
OKReservation is a derived class and its abstract base class is Reservation.
My problem is that the reservations array loses its value in other function
View 8 Replies
View Related
Apr 14, 2014
I have a data structure defined up here called this:
typedef list <classSpec*> ClassSpecList;
I'm trying to add stuff into the list here based on functions that return certain values of that match the same data type. In one function, I have a list pointer object defined here and I have another statement that calls a function.
ClassSpecList *answer = 0;
classSpec *thisanswer = parseClass(br);
Basically I'm trying to add the results of what thisanswer returns into my main ClassSpecList. Problem is, when I try
answer->push_back(new classSpec (*thisanswer));
It compiles but I get a seg fault
When I try somethign else like:
answer->insert(ClassSpecList.begin(), *thisanswer);
I keep getting primary expression errors and I do not know why. I even tried it with other list made without typedef and I still get those.
View 6 Replies
View Related
Nov 13, 2013
The game is going to be an RPG where you take turns attacking enemies. Your character will be able to attack/defend/use item/run/use magic spell. For now, we just want the bare basics - for a character to be able to do a generic attack and to be able to fight a generic enemy (so we're not worried about items and such things yet).
Our cpp files are: hero.cpp (which is actually the main file... and should be renamed to something better, admittedly), enemyStats.cpp (contains a class for enemies that lets you declare its HP/attack/etc), playerStats.cpp (see previous; but for your character) and attack.cpp, which contains the following:
int attackFunction(player myPlayer, enemy myEnemy)
{
print("Your player attacked for %d damage, bringing the wolf down to %d HP.", myPlayer.getAttack(), myEnemy.getCurrentHealth-myPlayer.getAttack());
}
We barely started, but we're already stuck on that part. I managed to make the program run just fine in terms of giving the player and enemy stats and reading it, but whenever I try to run this code in my hero.cpp (inside of main):
attackFunction(warrior, wolf);
it tells me that "enemy" has not been declared.
If I run the same code, but without any references to the wolf/enemy (so just warrior/myPlayer), it runs perfectly fine. Likewise, the program runs perfectly fine if I don't use attack.cpp and put the function directly inside of the main hero.cpp folder.
View 11 Replies
View Related
Nov 5, 2014
Im creating a program for a race. The Race class has a vector of results and each element of that vector is a pointer to a result. The Result class has a Time and a pointer to a Participant. So in each race there are various results and it is a result for each participant.The Time is a class that has hours, minutes and seconds. How can I sort the vector of results from the result of the participant with the fastest time to the result of the participant with the slowest time?My code is like this:
//.h file:
class Time {
unsigned int hours;
unsigned int minutes;
unsigned int seconds;
[code]....
What am I missing to get my code to work?
View 9 Replies
View Related
Nov 5, 2014
Im creating a program for a race. The Race class has a vector of results and each element of that vector is a pointer to a result. The Result class has a Time and a pointer to a Participant. So in each race there are various results and it is a result for each participant. The Time is a class that has hours, minutes and seconds. How can I sort the vector of results from the result of the participant with the fastest time to the result of the participant with the slowest time?
Im getting some errors in my code. I put the error as comments in the code. Each error is after the line where it occurs. My code is like this:
//.h file:
class Time
{
unsigned int hours;
[Code]....
View 8 Replies
View Related
Jul 27, 2013
So I'm trying to fill a vector with pointers to account objects.
std::vector<account*> fill_vector() {
std::vector<account*> temp;
std::ifstream s;
std::string str;
s.open("H://account2.dat");
[Code] ....
The accounts have four different types and I am supposed to skip over ones that have an invalid account type and throw an exception:
account* factory(std::string account_code, std::string first_name,
std::string last_name, char type, double balance) {
try {
if(type == 'A') {
simple_account *a = new simple_account(account_code, first_name, last_name, balance);
[Code] .....
My problem is they program will not skip over my rejected accounts.. still adds them to the vector but I cant figure out why!
View 8 Replies
View Related
Dec 3, 2013
I want to scan numbers in from within a function, but have access to them in main, so I tried using pointers to do so:
Code:
// Path Of Exile socket colours simulation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
[Code].....
View 9 Replies
View Related
Feb 28, 2015
I have been struggling with pointers. I am trying to write a program that first asks a user to input a filename. It then checks if the file exists and if it does it passes a pointer to the next function. The next function then asks the user for a specific word to look for and the function will search a text file for the word and do some other operations. My problem is that I do not understand how to use the pointer returned by my first function as an input to another function.
The following code has the first function file_check() and the second function word_search() which I think the way I am declaring it is the problem.
Code:
FILE *file_check();
void word_search(FILE *);
int
main(void) {
FILE* check= file_check();
// word_search(check);
[Code] ......
View 6 Replies
View Related
Feb 10, 2015
Using SFML, I had a Board class which held multiple vectors of all of my object types in the game, and then it also held a vector of pointers to the memory addresses of these object instances, like this
class Board{
//...
std::vector<AbstractObject*> GetAllLevelObjects(){ return allLevelObjects; }
//so these are used to hold my object instances for each level
[Code]....
When looping through this vector and drawing the sprites of the objects, I get the runtime error 0xC0000005: Access violation reading location 0x00277000. I solved this error by storing the vector of pointers in the class that holds my Board instance, but I'm wondering why only this solution worked? Why couldn't I just have my vector of pointers in the same class that the instances of those objects were in?
View 2 Replies
View Related
Jun 4, 2013
#include <stdio.h>
#include <stdlib.h>
int size_b_row1;
int size_b_col1;
int main(void) {
double C[4][4] = {{1,3,5,2},{7,6,2,2},{1,2,7,3},{2,3,5,3}};
double TC[4][4];
transpose(C, TC);
[Code] ......
View 2 Replies
View Related
May 7, 2013
I am looking for an example of when passing arrays to other data that has pointers,
I understand the terms I would just like to see a small example of code that actually demonstrates this process.
View 4 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