Thread: need to pass an object as a pointer in function

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    5,108
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 07: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, 09: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