need to pass an object as a pointer in function

This is a discussion on need to pass an object as a pointer in function within the C++ Programming forums, part of the General Programming Boards category; im new to c++ and i need to pass myboard.board (board is in the class Cboard and it is an ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    need to pass an object as a pointer in function

    im new to c++ and i need to pass myboard.board (board is in the class Cboard and it is an array of int) to a function in a class called piece however this is troubling . i need to pass it as pointer os that i could change its value here under is my code. please help
    main.cpp
    Code:
    #include<iostream>
    #include"board.h"
    #include "pieces.h"
    
    
    void main(){
    	char location[5];
    	int ov1=0,ov2=0,nv1=0,nv2=0;
    	int player=0;
    	int repeat=0;
    	int correct =0 ;
    	int boards;
    	int x=0;
    	Cboard myboard; // creating object
    	Cpiece mypiece;
    	do{
    	//myboard.getvalues(&new1,&new2);
    	if(repeat ==0){
    	myboard.printboard();
    	}
    	std::cout<<std::endl;
    	std::cout<<"please enter location";
    	std::cin>>location;
    	 myboard.board;// object array which i need to pass
    	repeat=myboard.setvalues(location,&nv1,&nv2,&ov1,&ov2);
    	//mypiece.piecechoice(&ov1,&ov2,boards);
    	std::cout<<player;
    	mypiece.piecechoice(&ov1,&ov2,&myboard.board); // the function when called 
    
    
        x=x++;
    	}while (x !=4);
    
    
    
    
    }
    piece.cpp
    Code:
    #include "pieces.h"
    #include"board.h"
    #include<iostream>
    
    
    Cboard myboard;
    void Cpiece::piecechoice(int *ov1,int *ov2,int *boards[8][8]){
    	int piece = 0;
    	int a,b;
    	a=*ov1;
    	b=*ov2;
    myboard.board;
        switch(piece){
    		case 1:
    			std::cout<<"ponn";
    			break;
    		case 2:
    			std::cout<<"ponn";
    			break;
    			
    	}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    Quote Originally Posted by kdun
    i need to pass myboard.board (board is in the class Cboard and it is an array of int) to a function in a class called piece however this is troubling . i need to pass it as pointer os that i could change its value here under is my code.
    When an array is passed as an argument, it is converted to a pointer to its first element. That is, given a function with a pointer parameter:
    Code:
    void foo(int x[]);
    And given an array:
    Code:
    int numbers[10];
    You can call:
    Code:
    foo(numbers);
    and the changes to x[i] in foo will affect numbers[i] in the caller, where i is a valid index of numbers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    I recently wrote this fascinating subject on the dangers of C arrays: SourceForge.net: Safer arrays in Cpp - cpwiki
    (Title is misleading.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    Quote Originally Posted by Elysia View Post
    I recently wrote this fascinating subject on the dangers of C arrays: SourceForge.net: Safer arrays in Cpp - cpwiki
    (Title is misleading.)
    easy to read and informative, just as I like it. and it allowed me to procrastinate a little longer while I look at 6 lines where there should be a couple of pages on particle physics.

    you wouldn't happen to have anything written on the danger of linear congruent generators when applied to monte carlo generators? at some point I should write a small paragraph justifying the decision to use a mersenne-twister other than "a guy I know told me it would be better" (that particular piece of text I have been procrastinating for a couple of months now).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Quote Originally Posted by killme View Post
    you wouldn't happen to have anything written on the danger of linear congruent generators when applied to monte carlo generators? at some point I should write a small paragraph justifying the decision to use a mersenne-twister other than "a guy I know told me it would be better" (that particular piece of text I have been procrastinating for a couple of months now).
    Nope. Statistics just isn't my area of forte.

    Oh, and btw,
    @kdun
    You shouldn't use void main: http://cpwiki.sourceforge.net/void%20main
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,336
    I recently wrote this fascinating subject on the dangers of C arrays
    O_o

    The way that looks, it sounds unbelievably arrogant. If you still have time to edit you may want to move a few of those words around so it reads as a treatment on a fascinating subject.

    Just so as you know, `typeid(???).name()' isn't guaranteed to return anything meaningful to humans. I'd strongly encourage a footnote explaining the output as a sample run where the user may see something different.

    Soma
    Last edited by phantomotap; 04-12-2013 at 03:14 PM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    I can't edit the post, but I did update the disclaimer in the wiki under type information to indicate that it is a sample run and output may vary between compilers and typeid(???).name() is not guaranteed to return anything human readable.
    I guess I should figure out how to make foot notes, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    The way that looks, it sounds unbelievably arrogant
    you could always make a quote to mess with Elysia.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-30-2012, 06:18 AM
  2. Pass object to function
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2008, 07:57 AM
  3. how to pass an ifstream object to a function
    By chintugavali in forum C++ Programming
    Replies: 14
    Last Post: 12-18-2007, 08:58 PM
  4. How to pass member functions into a function object...
    By TeenWolf in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 01:01 PM
  5. How can I define and pass a pointer to a vector object?
    By asmileguo in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2006, 11:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21