Thread: im trying to pass an object to an inherited class

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

    im trying to pass an object to an inherited class

    im trying to pass an object to an inherited clas by a construcotr however im facing some probelms can someone tell me what im doing wrong. im HAving Cboard my board and i need to pass it to the object Cpawn here under is my code:
    main
    Code:
    #include<iostream>#include "Cboard.h"
    #include "Cpawn.h"
    #include "Cpiece.h"
    
    
    
    
    
    
    void main(){
    	char location[50];
    
    
    	char piece;
    	int oldrow=0,oldcolumn=0,newrow=0,newcolumn=0,check =0,end=0;
    	Cboard myboard;
    	Cpawn mypawn(&myboard);
    	
    
    
    	Cpiece *piecer = &mypawn;
    	do{
    	std::cout<<std::endl;
    	
    		(myboard).printboard();
    		
    
    
    		std::cout<<std::endl;
    	std::cout<<"please enter location";
    	std::cin>>location;
    	check =(myboard).validateuserinput(location,&newcolumn,&newrow,&oldrow,&oldcolumn);
    	//piece = (*myboard).board[]
    	if(check==0){
    		std::cout<<"correct";
    		
    		if((*piecer).movepiece(&newcolumn, &newrow,&oldrow,&oldcolumn)==1){
    			(myboard).moveer(&newrow,&newcolumn,&oldrow,&oldcolumn);
    			(myboard).move();
    	}
    
    
    		
    	}
    	else{
    		std::cout<<"error";
    	}
    	end++;
    	}while(end!=8);
    }
    Cpawn.h
    Code:
    #pragma once#include "Cpiece.h"
    
    
    class Cpawn:public Cpiece
    {
    public:
    	virtual char getcolor();
    	 virtual char setcolor(char colour);
    	virtual char getpiece();
    	virtual int movepiece(int *newcolumn, int *newrow, int *oldrow,int *oldcolumn);
    	 char col;
    	 Cboard *mboard;
    	Cpawn(Cboard *board);
    };
    Cpawn.cpp
    Code:
    #include <iostream>#include "Cpawn.h"
    #include "Cpiece.h"
    #include"Cboard.h"
    
    
     char Cpawn::getpiece(){
    	   
    		return 'p';
    	}
     Cpawn::Cpawn(Cboard *board){
    	 mboard = board; 
         }
     char Cpawn::setcolor(char colour){
    	 col = colour;
      return col;
     }
     char Cpawn::getcolor(){
    	 return col;
     }
     int Cpawn::movepiece(int *newcolumn, int *newrow,int *oldcolumn,int *oldrow){
    	 int correct =0;
    	 int old ,old1;
    	char piece;
    	 old =*oldrow;
    	 old1 = *oldcolumn;
    	 piece= mboard->board[1][1]->getcolor;
    	 if(*oldrow == *newrow && *oldcolumn-1 == * newcolumn||*oldrow == *newrow && *oldcolumn+1 == * newcolumn){
    		correct =1;
    	}
    
    
    	return correct ;
     }
    Cpiece.h
    Code:
    #pragma once
    
    class Cpiece
    {
    protected:
    
    
    	
    public:
    	virtual char getcolor() =0;
    	virtual char setcolor(char colour)=0;
    	virtual char getpiece()=0;
    	virtual int movepiece(int *newcolumn, int *newrow, int *oldrow,int*oldcolumn)=0;
    	char getcolumn();
    	char getrow();
    	//Cboard bord;
    	void setvalue(int oldcolumn,int oldrow);
    	//Cpiece (void);
    	//Cpiece(Cboard board);
    	int row,column;
    	
    
    
    	//~Cpiece(void);
    };
    cpiece.cpp
    Code:
    #include "Cpiece.h"
    
    
    
    /*Cpiece::Cpiece(void)
    {
    }
    
    
    /*Cpiece::Cpiece(Cboard myboard){
    	bord = myboard;
    }*/
    
    
    /*Cpiece::~Cpiece(void)
    {
    }*/
    void Cpiece::setvalue(int oldcolumn,int oldrow){
    	
    	 row =oldrow ;
    	 column = oldcolumn;
    }
     char Cpiece::getcolumn(){
    
    
     return column;
    }
    
    
    char Cpiece::getrow(){
    	return row;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Saying you've got a problem is insufficient. In future, try being specific about what the problem is. For example, if your compiler is emitting error messages, what are those messages? Are there particular lines of code that are causing the problem


    However, you got lucky - I happened to spot the problem. CPawn.h needs to either forward declare CBoard, or #include "cboard.h". The reason is that the declaration of CPawn's constructor does not also magically declare CBoard.

    Other problems

    1) main() returns int, not void. If your compiler documentation (or, worse, a book) tells you that main() returns void, it is wrong. Even if your compiler accepts it.

    2) Don't use #pragma once. It is not guaranteed to work. Use #include guards. If you don't know what #nclude guards are, look them up.

    3) Cboard.cpp has screwed up comments, so more code is being commented out than you might think.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    these are erros that im facing and could not correct
    Attached Images Attached Images im trying to pass an object to an inherited class-untitled-jpg 

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Refer to the line in my previous post that started "You got lucky ...".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-22-2009, 11:12 PM
  2. Detecting Inherited Class Type
    By leeor_net in forum C++ Programming
    Replies: 9
    Last Post: 03-01-2009, 03:15 PM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. inherited variables in template class not inheriting
    By fimion in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2005, 08:10 AM
  5. Replies: 3
    Last Post: 02-12-2002, 10:09 PM