CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Posts
    8

    I can't split my code for makefile

    I must say that I have NO IDEA how to split my code. Actually I think that I don't understand how makefile works confused.gif

    Can anyone show me how my code should be split? And how makefile would look like??

    MY CODE:
    Code:
    #include <iostream>
    #include <cstring>
    #include <fstream>
    
    
    
    using namespace std;
    
    
    struct element;
    template <typename Typ, Typ *neutral> class zbior;
    template <typename Typ,Typ *neutral>   zbior<Typ,neutral> operator+(  const zbior<Typ,neutral>&, const  zbior<Typ,neutral>&);
    template <typename Typ,Typ *neutral>   zbior<Typ,neutral> operator-(  const zbior<Typ,neutral>&, const  zbior<Typ,neutral>&);
    template<typename Typ, Typ *neutral> ostream &operator << (ostream &, const zbior<Typ,neutral> &);
    
    
    
    int i=0;
    char c='_';
    template <typename Typ, Typ *neutral> class zbior{
    	int _rozmiar;
    	Typ *ti;
    	static const int DomysRoz=5;
    public:
    	explicit zbior(int rzm=0);
    	zbior(Typ *tablica, int rzm);
    	zbior(const zbior &nps );
    	~zbior() {delete [] ti;};	
    	bool operator==(const zbior&) const;
    	bool operator !=(const zbior&) ;
    	void operator+=(const zbior<Typ,neutral>&);
    	void operator-=(const zbior<Typ,neutral>&);
    	friend  zbior<Typ,neutral> operator+<Typ,neutral>( const zbior<Typ,neutral>&, const zbior<Typ,neutral>&);
    	friend  zbior<Typ,neutral> operator-<Typ,neutral>( const zbior<Typ,neutral>&, const zbior<Typ,neutral>&);
    	friend ostream &operator << <>(ostream &os, const zbior<Typ,neutral> &k); 
    	void operator=(const zbior&); 
    	int rozmiar()  {return _rozmiar;}
    	Typ min();
    	Typ maks();
    	void operator+=(Typ ww);
    	void operator-=(Typ ww);
    	void sortuj();
    	void wypisz();
    	int LiczEl();
    	int przewi(int tt);
    };
    template <typename Typ, Typ *neutral> zbior<Typ,neutral>::zbior(int rzm){
    	_rozmiar=rzm;
    	ti=new Typ[_rozmiar];
    	for (int ix=0; ix<_rozmiar; ++ix){
    		ti[ix]=*neutral;
    		
    	}
    	
    }
     struct element {
    	int t,w;
    	element(int a=0, int b=0) {t=a; w=b;};
    	bool operator==(const element);
    	bool operator<(const element);
    	bool operator>(const element);
    	void operator=(const element j) {t=j.t; w=j.w;}
    	bool operator!=(const element j) {return (t!=j.t || w!=j.w);}
    };
    element k;
    class Test{
    public:
    	Test(){;};
    	virtual void przygotuj()=0;
    	virtual void testuj()=0;
    	virtual void drukuj_wynik()=0;
    };
    
    class ProstyTest: public Test {
    
    	zbior<int,&i> ki;
    	zbior<int,&i> ke;
    	zbior<int,&i> wynik;
    	zbior<int,&i> dod;
    	zbior<int,&i> odj;
    
    	int ile;
    public:
    	ProstyTest(){ile=0;;};
    	void przygotuj();
    	void testuj();
    	void drukuj_wynik();
    };
    
    class DuzyZestaw: public Test {
    	
    	zbior<char,&c> pi;
    	zbior<char,&c> pe;
    	zbior<char,&c> wynik;
    	zbior<char,&c> dod1;
    	zbior<char,&c> odj1;
    	zbior<element,&k> wi;
    	zbior<element,&k> we;
    	zbior<element,&k> wy;
    	zbior<element,&k> dod2;
    	zbior<element,&k> odj2;
    public:
    	DuzyZestaw(){;};
    	void przygotuj();
    	void testuj();
    	void drukuj_wynik();
    };
    
    
    
    	
    	
    
    
    
    
    
    bool element::operator==(const element p){
    return((*this).t==p.t & (*this).w==p.w);
    }
    
    bool element::operator<(const element p){
    if ((*this).w==p.w) {return (*this).t<p.t;}
    else return (*this).w<p.w;
    }
    
    bool element::operator>(const element p){
    if ((*this).w==p.w) {return (*this).t>p.t;}
    else return (*this).w>p.w;
    }
    
    std::ostream &operator << (std::ostream &os, const element &k)
    {
       return os<<"t:"<<k.t<<" w:"<<k.w;
    } 
    
    
    
    
    
    template <typename Typ, Typ *neutral> zbior<Typ,neutral>::zbior(const zbior &nps ){
    int ix;
    	_rozmiar=nps._rozmiar;
    	if( ! nps.ti)
    		ti=0;
    	else{
    		ti=new Typ [_rozmiar+1];
    		for (int ix=0; ix<_rozmiar; ++ix){
    		ti[ix]=nps.ti[ix];
    		
    	}
    	}
    }
    
    template <typename Typ, Typ *neutral>  int zbior<Typ,neutral>::LiczEl(){
    int n=0;
    for(int i=0; i<_rozmiar; i++)
    {
    	if(ti[i] != *neutral) {++n;};
    }
    return n;
    }
    
    template <typename Typ, Typ *neutral> void zbior<Typ,neutral>::operator+=(Typ ww){
    int i=0;
    int ix=0;
    if(_rozmiar<=0) {
    			zbior<Typ,neutral> tymcz (*this);
    						_rozmiar=(_rozmiar+DomysRoz);
    						delete [] ti;
    						ti= new Typ[_rozmiar];
    						for(ix=0; ix<(tymcz._rozmiar); ++ix) {ti[ix]=tymcz.ti[ix];}
    						ti[ix]=ww;
    						for(i=(ix+1); i<(_rozmiar); ++i) {ti[i]=*neutral;}
    		}
    else{
    			
    while (ww!=ti[ix] & ix<_rozmiar) {ix++;}
    
    if (ti[ix]!=ww) {
    		if ((LiczEl())==_rozmiar) {
    						zbior<Typ,neutral> tymcz (*this);
    						_rozmiar=(_rozmiar+DomysRoz);
    						delete [] ti;
    						ti= new Typ[_rozmiar];
    						for(ix=0; ix<(tymcz._rozmiar); ++ix) {ti[ix]=tymcz.ti[ix];}
    						ti[ix]=ww;
    						for(i=(ix+1); i<(_rozmiar); ++i) {ti[i]=*neutral;}
    					}
    		else { ix=0; while (ti[ix]!=*neutral & ix<_rozmiar) {ix++;} ti[ix]=ww;}
    		}
    else {; }
    }
    }
    
    template <typename Typ, Typ *neutral> void zbior<Typ,neutral>::wypisz(){
    
    for (int ix=0; ix<_rozmiar; ++ix){
    		cout << ti[ix];}
    }
    
    template <typename Typ, Typ *neutral> void zbior<Typ,neutral>::operator-=(Typ ww){
    int ix=0;
    while (ww!=ti[ix] & ix<_rozmiar) {ix++;}
    if (ti[ix]==ww) {ti[ix]=*neutral;}
    }
    
    template <typename Typ, Typ *neutral> bool zbior<Typ,neutral>::operator !=(const zbior &k) {
    bool wy=false;
    int p, ix=0;
    while(wy==false & ix<_rozmiar) {
    p=0;
    cout<<"- - - - - ";
    cout<<"this: ";
    cout<<ti[ix];
    cout<<" "<<k.ti[p];
    cout<<"\n";
    while (ti[ix]!=k.ti[p] & p<k._rozmiar){p++; cout<<"that: ";cout<<k.ti[p];  cout<<"\n"; }
    if (ti[ix]!=k.ti[p] &ti[ix]!=*neutral) {wy=true; cout<<"lala";}
    ix++;
    }
    	
    
    return wy;
    ;}
    template <typename Typ, Typ *neutral> Typ zbior<Typ,neutral>::maks(){
    Typ maks;
    if (_rozmiar>0)
    {	maks=ti[0];
    	for(int i=0; i<_rozmiar; i++)
    	{
    		if( ti[i]>maks & ti[i]!=*neutral) {maks=ti[i];};
    	}
    }
    else {throw "ta tablica jest pusta!" ;}
    return maks;
    }
    
    template <typename Typ, Typ *neutral> Typ zbior<Typ,neutral>::min(){
    Typ min;
    if (_rozmiar>0)
    {	min=ti[0];
    	for(int i=0; i<_rozmiar; i++)
    	{
    		if( ti[i]<min & ti[i]!=*neutral) {min=ti[i];};
    	}
    }
    else {throw"ta tablica jest pusta! ";}
    return min;
    }
    template <typename Typ, Typ *neutral> void zbior<Typ,neutral>::operator+=(const zbior<Typ,neutral> &ps){
    
    for (int i=0; i<(ps._rozmiar); i++) {if (ps.ti[i]!=*neutral) (*this)+=(ps.ti[i]);};
    
    }
    template <typename Typ, Typ *neutral> void zbior<Typ,neutral>::operator-=(const zbior<Typ,neutral> &ps){
    
    for (int i=0; i<(ps._rozmiar); i++) {if (ps.ti[i]!=*neutral) (*this)-=(ps.ti[i]);};
    
    }
    template <typename Typ,Typ *neutral>  zbior<Typ,neutral> operator+(  const zbior<Typ,neutral>&pp, const  zbior<Typ,neutral>&kk){ 
    zbior<Typ,neutral> tymcz (kk);
    tymcz+=pp;
    return tymcz;
    }
    
    template <typename Typ,Typ *neutral>  zbior<Typ,neutral> operator-(  const zbior<Typ,neutral>&pp, const  zbior<Typ,neutral>&kk){ 
    zbior<Typ,neutral> tymcz (pp);
    tymcz-=kk;
    return tymcz;
    }
    						
    
    template <typename Typ, Typ *neutral> void zbior<Typ,neutral>::operator=(const zbior &ps)
    {int ix;
    	_rozmiar=ps._rozmiar;
    	if( ! ps.ti)
    		ti=0;
    	else{
    		delete [] ti;
    		ti=new Typ [_rozmiar+1];
    		for (int ix=0; ix<_rozmiar; ++ix){
    		ti[ix]=ps.ti[ix];
    		
    	}
    	}
    }		
    
    
    template<typename Typ, Typ *neutral> 
    ostream &operator << (ostream &os, const zbior<Typ,neutral> &k)
    {
        for (int ix=0; ix<k._rozmiar; ++ix){
    		 os << k.ti[ix]<<" ";}
    return os;
    } 
    
    void ProstyTest::przygotuj(){
    ki+=(2);ki+=(9);ki+=(4);ki+=(5);ki+=(8);
    ke+=(1);ke+=(3);ke+=(7);ke+=(8);ke+=(9);ke+=(4);
    dod+=(1);dod+=(3);dod+=(7);dod+=(8);dod+=(9);dod+=(4);dod+=(2);dod+=(5);
    odj+=(2);odj+=(5);
    ile=5;
    }
    
    void ProstyTest::testuj(){
    ofstream bledy("bledyprosty.txt");
    bledy.close();
    ofstream mojplik("plik.txt");
    mojplik<<"To sa wyniki Prostego Testu:";
    mojplik.put('\n');
    mojplik<<"obiekt 1: ";
    mojplik<<ki;
    mojplik.put('\n');
    mojplik<<"obiekt 2: ";
    mojplik<<ke;
    mojplik.put('\n');
    mojplik<<"ob.1 + ob.2 = ";
    wynik=ki+ke;
    if (wynik!=dod) { 
    	ofstream bledy("bledyprosty.txt");
    	bledy<<"\n"<<"Zla wartosc dla: ob.1 + ob.2!";
    	bledy.close();}
    mojplik<<wynik;
    mojplik.put('\n');
    mojplik<<"ob.1 - ob.2 = "; 
    wynik=ki-ke;
    if (wynik!=odj) { 
    	ofstream bledy("bledyprosty.txt", ios_base::app);
    	bledy<<"\n"<<"Zla wartosc dla: ob.1 - ob.2!";
    	bledy.close();}
    mojplik<<wynik;
    mojplik.put('\n');
    mojplik<<"minimum obiektu 1: ";
    try{
    	mojplik<<ki.min();
    	}
    catch(const char* w)
    { ofstream bledy("bledyprosty.txt", ios_base::app);
    	bledy<<"\n"<<"minimum obiektu 1: "<<w;
    	bledy.close();}
     mojplik.put('\n');
    mojplik<<"maksimum obiektu 1: ";
    try{
    	mojplik<<ki.maks(); 
    	}
    catch(const char* w)
    { ofstream bledy("bledyprosty.txt", ios_base::app);
    	bledy<<"\n"<<"maksimum obiektu 1: "<<w;
    	bledy.close();}
    mojplik.put('\n');
    mojplik<<"liczba elementow obiektu 1: ";
    mojplik<<ki.LiczEl(); 
    if (ki.LiczEl()!=ile) { 
    	ofstream bledy("bledyprosty.txt", ios_base::app);
    	bledy<<"\n"<<"Zla wartosc dla: liczba elementow obiektu 1: ";
    	bledy.close();}
    mojplik.close();
    ;}
    
    void ProstyTest::drukuj_wynik(){
    char zn;
    ifstream mojplik("plik.txt");
    while (mojplik.get(zn)) cout<<zn;
    mojplik.close();
    
    ;}
    
    void DuzyZestaw::przygotuj(){
    element g;
    pi+=('c');pi+=('a');pi+=('A');pi+=('f');pi+=('y');
    pe+=('a');pe+=('c');pe+=('B');pe+=('A');pe+=('p');pe+=('m');
    dod1+=('A');dod1+=('c');dod1+=('B');dod1+=('a');dod1+=('m');dod1+=('p');dod1+=('y');dod1+=('f');
    odj1+=('y');odj1+=('f');
    
    g.t=1; g.w=1; wi+=(g); dod2+=(g);odj2+=(g);
    g.t=1; g.w=3; wi+=(g);dod2+=(g);odj2+=(g);
    g.t=4; g.w=5; wi+=(g);dod2+=(g);odj2+=(g);
    g.t=2; g.w=2; wi+=(g);dod2+=(g);
    g.t=7; g.w=9; wi+=(g);dod2+=(g);
    g.t=2; g.w=2; we+=(g);
    g.t=7; g.w=9; we+=(g);
    g.t=1; g.w=4; we+=(g);dod2+=(g);
    g.t=6; g.w=6; we+=(g);dod2+=(g);
    g.t=7; g.w=5; we+=(g);dod2+=(g);
    g.t=1; g.w=8; we+=(g);dod2+=(g);
    
    
    ;}
    void DuzyZestaw::testuj(){
    ofstream bledy("bledyduzy.txt");
    bledy.close();
    ofstream mojplik("duzytest.txt");
    mojplik<<"To sa wyniki Duzego Testu:";
    mojplik.put('\n');
    mojplik<<"obiekt 1: ";
    mojplik<<pi;
    mojplik.put('\n');
    mojplik<<"obiekt 2: ";
    mojplik<<pe;
    mojplik.put('\n');
    mojplik<<"ob.1 + ob.2 = ";
    wynik=pi+pe;
    if (wynik!=dod1) { 
    	ofstream bledy("bledyduzy.txt", ios_base::app);
    	bledy<<"\n"<<"Zla wartosc dla: ob.1 + ob.2!";
    	bledy.close();}
    mojplik<<wynik;
    mojplik.put('\n');
    mojplik<<"ob.1 - ob.2 = "; 
    wynik=pi-pe;
    cout<<wynik; cout<<odj1;
    if (wynik!=odj1) { 
    	ofstream bledy("bledyduzy.txt", ios_base::app);
    	bledy<<"\n"<<"Zla wartosc dla: ob.1 - ob.2!";
    	bledy.close();}
    mojplik<<wynik;
    mojplik.put('\n');
    mojplik<<"minimum obiektu 1: ";
    try{
    	mojplik<<pi.min();
    	}
    catch(const char* w)
    { ofstream bledy("bledyduzy.txt", ios_base::app);
    	bledy<<"\n"<<"minimum obiektu 1: "<<w;
    	bledy.close();}
    mojplik.put('\n');
    mojplik<<"maksimum obiektu 1: ";
    try {
    	mojplik<<pi.maks();
    	}
    catch(const char* w)
    { ofstream bledy("bledyduzy.txt", ios_base::app);
    	bledy<<"\n"<<"maksimum obiektu 1: "<<w;
    	bledy.close();}
    mojplik.put('\n');
    mojplik<<"liczba elementow obiektu 1: ";
    mojplik<<pi.LiczEl(); 
    
    
    mojplik.put('\n');
    mojplik<<"obiekt 3: ";
    mojplik<<wi;
    mojplik.put('\n');
    mojplik<<"obiekt 4: ";
    mojplik<<we;
    mojplik.put('\n');
    mojplik<<"ob.3 + ob.4 = ";
    wy=wi+we;
    if (wy!=dod2) { 
    	ofstream bledy("bledyduzy.txt", ios_base::app);
    	bledy<<"\n"<<"Zla wartosc dla: ob.3 + ob.4!";
    	bledy.close();}
    mojplik<<wy;
    mojplik.put('\n');
    mojplik<<"ob.3 - ob.4 = "; 
    wy=wi-we;
    if (wy!=odj2) { 
    	ofstream bledy("bledyduzy.txt", ios_base::app);
    	bledy<<"\n"<<"Zla wartosc dla: ob.3 - ob.4!";
    	bledy.close();}
    mojplik<<wy;
    mojplik.put('\n');
    mojplik<<"minimum obiektu 3: ";
    try {
    	mojplik<<wi.min(); 
    	}
    catch(const char* w)
    { ofstream bledy("bledyduzy.txt", ios_base::app);
    	bledy<<"\n"<<"minimum obiektu 3: "<<w;
    	bledy.close();}
    mojplik.put('\n');
    mojplik<<"maksimum obiektu 3: ";
    try {
    	mojplik<<wi.maks(); 
    	}
    catch(const char* w)
    { ofstream bledy("bledyduzy.txt", ios_base::app);
    	bledy<<"\n"<<"maksimum obiektu 3: "<<w;
    	bledy.close();}
    mojplik.put('\n');
    mojplik<<"liczba elementow obiektu 4: ";
    mojplik<<wi.LiczEl();
    mojplik.close();
    ;}
    void DuzyZestaw::drukuj_wynik(){
    char zn;
    cout<<"\n";
    ifstream mojplik("duzytest.txt");
    while (mojplik.get(zn)) cout<<zn;
    mojplik.close();;}
    string p="kot";
    
    void testuj(Test &_test){
    _test.przygotuj();
    _test.testuj();
    /*_test.drukuj_wynik();*/
    }
    main(){
    ProstyTest p;
    DuzyZestaw k;
    testuj(p);
    testuj(k);
    
    
    
    
    
    
    
    
    
    
    
    getchar();
    ;}

  2. #2
    Join Date
    Aug 2009
    Posts
    8

    Re: I can't split my code for makefile

    Cut out all your template definitions and implementations "Zhibor" and save it in a file called Zhibor.cpp

    Next cut out all your class definitions and implementations such as ProstyTest, DuzyZestaw and Test and save them in cpp files with those names. Everything else into a file called Run.cpp

    Now your makefile will look like

    Code:
    all: ProstyTest.cpp DuzyZestaw.cpp Test.cpp Run.cpp
        g++ ProstyTest.cpp DuzyZestaw.cpp Test.cpp Run.cpp
    I havent bothered to break the files up into definitions and implementations. But, remember that template defs and implementations should be in the one file.
    Java is your wife; comfortable, stable and easy to live with. C++ is your mistress; exciting, dangerous and you just don't know when all hell will break loose.

    http://codediaries.blogspot.com

  3. #3
    Join Date
    Dec 2009
    Posts
    89

    Re: I can't split my code for makefile

    Quote Originally Posted by biesczadka View Post
    I must say that I have NO IDEA how to split my code.
    Do you mind if I ask why? If you wanted to split the code, why you coded it that long already?
    Quote Originally Posted by biesczadka View Post
    Actually I think that I don't understand how makefile works confused.gif

    Can anyone show me how my code should be split? And how makefile would look like??
    I just learned about Makefile, and I follow below guide, which contains a very good information how the program is spitted.
    http://mrbook.org/tutorials/make/

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: I can't split my code for makefile

    you already seem to have had separate files: http://www.codeguru.com/forum/showthread.php?t=489403
    and now you put all in one file. Is it only the makefile you are having troubles with? Then post what you have already and what errors you get (or which problems you have with that makefile of yours).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured