GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Dec-2007, 23:59
Max_Payne Max_Payne is offline
New Member
 
Join Date: Oct 2007
Posts: 21
Max_Payne has a little shameless behaviour in the past
Question

Classes & Collections


***Header Files:

Point.h
Code:
#pragma once #include <iostream> using namespace std; class Point { private: int x; int y; public: Point(void); Point( int x, int y ); Point( const Point &xPoint ); ~Point(void); void setX( int x ); int getX() const; void setY( int y ); int getY() const; Point & operator = ( const Point &xPoint ); bool includes( int cord_X, int cord_Y ) const; void draw() const; };


PointCollection.h
Code:
#pragma once #include "Point.h" const int MAX = 100; class PointCollection { private: Point pts[ MAX ]; int quantity; public: PointCollection( void ); PointCollection( const PointCollection &xPointCollection ); ~PointCollection( void ); // To know how many Points are // ..in the collection int size() const; PointCollection operator = ( const PointCollection &xPointCollection ); Point &operator[]( int index ); const Point &operator[]( int index ) const; bool isFull() const; bool isEmpty() const; bool includes( const Point &xPoint ) const; int indexOf( const Point &xPoint ) const; };

////////////////////////////////////////////////////////////////////////

***Source Files:

Point.cpp
Code:
#include "Point.h" Point::Point( void ) { this->x = 0; this->y = 0; } Point::Point( int x, int y ) { this->x = x; this->y = y; } Point::Point( const Point &xPoint ) { this->x = xPoint.x; this->y = xPoint.x; } Point::~Point( void ){} // Destructor //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx void Point::setX( int x ) // set X { this->x = x; } void Point::setY( int y ) // set Y { this->y = y; } int Point::getX() const // get X { return ( this->x ); } int Point::getY() const // get Y { return ( this->y ); } //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Point &Point::operator = ( const Point &xPoint ) { this->x = xPoint.x; this->y = xPoint.y; return ( *this ); } bool Point::includes( int cord_X, int cord_Y ) const { return ( this->x == cord_X && this->y == cord_Y ); } void Point::draw() const { cout << '.'; }


PointCollection.cpp
Code:
#include "PointCollection.h" PointCollection::PointCollection( void ) { this->quantity = 0; } PointCollection::PointCollection( const PointCollection &xPointCollection ) { this->quantity = xPointCollection.quantity; for ( int i = 0; i < this->quantity; i++ ) ( this->pts )[ i ] = ( xPointCollection.pts )[ i ]; } PointCollection::~PointCollection( void ) {} //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx int PointCollection::size() const { return ( this->quantity ); } //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Point &PointCollection::operator []( int index ) { return ( ( this->pts )[ index ] ); } const Point &PointCollection::operator []( int index ) const { return ( ( this->pts )[ index ] ); } //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx PointCollection PointCollection::operator =( const PointCollection &xPointCollection ) { this->quantity = xPointCollection.quantity; for ( int i = 0; i < this->quantity; i++ ) ( this->pts )[ i ] = ( xPointCollection.pts )[ i ]; return ( *this ); } //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx bool PointCollection::isFull() const { return ( this->quantity == MAX ); } bool PointCollection::isEmpty() const { return ( this->quantity == 0 ); } //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx bool Point::includes( const Point &xPoint ) const { bool thisOne = false; for ( int i = 0; i < this->quantity && ! thisOne; i++ ) { if ( ( *this )[ i ] == xPoint ) thisOne = true; } return ( thisOne ); } int PointCollection::indexOf( const Point &xPoint ) const { int index = -1; for ( int i = 0; i < this->quantity && index == -1; i++ ) { if ( ( *this )[ i ] == xPoint ) index = i; } return ( index ); }


Main.cpp
Code:
#include "PointCollection.h" int main () { Point p1( 1, 1 ), p2( p1 ); p1.draw(); cout << "\n\n"; if ( p1.includes( 1, 1 ) == true ) cout << "Point 1 & 2 are Equal" << "\n\n"; else cout << "Point 1 & 2 are NOT Equal" << "\n\n"; system("PAUSE"); return 0; } // end main




This is the Error I'm Getting:
------ Build started: Project: Project_Dibujo, Configuration: Debug Win32 ------
Compiling...
Point.cpp
PointCollection.cpp
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\pointc ollection.cpp(85) : error C2511: 'bool Point::includes(const Point &) const' : overloaded member function not found in 'Point'
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\point. h(7) : see declaration of 'Point'
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\pointc ollection.cpp(103) : error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'const Point'
c:\program files\microsoft visual studio 8\vc\include\xmemory(174) : see declaration of 'std::operator =='
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\pointc ollection.cpp(103) : error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'const Point'
c:\program files\microsoft visual studio 8\vc\include\xutility(2143) : see declaration of 'std::operator =='
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\pointc ollection.cpp(103) : error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const Point'
c:\program files\microsoft visual studio 8\vc\include\xutility(1826) : see declaration of 'std::operator =='
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\pointc ollection.cpp(103) : error C2784: 'bool std::operator ==(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const Point'
c:\program files\microsoft visual studio 8\vc\include\utility(60) : see declaration of 'std::operator =='
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\pointc ollection.cpp(103) : error C2676: binary '==' : 'const Point' does not define this operator or a conversion to a type acceptable to the predefined operator
Generating Code...
Compiling...
Main_.cpp
Generating Code...
Build log was saved at "file://c:\Documents and Settings\MPayne007\My Documents\Visual Studio 2005\Projects\Project_Dibujo\Project_Dibujo\Debug\ BuildLog.htm"
Project_Dibujo - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  #2  
Old 11-Dec-2007, 05:28
davis
 
Posts: n/a

Re: Classes & Collections


Since there wasn't any actual question posted, I'll go with the implied question that asks "WTF am I doing wrong?!"

Rather than go through it all, I'll just post the changes that were required to fix it.

Point.h

CPP / C++ / C Code:
#pragma once

#include <iostream>
using namespace std;

class Point
{
private:
    int x;
    int y;

public:
    Point(void);
    Point( int x, int y );
    Point( const Point &xPoint );
    ~Point(void);

    void setX( int x );
    int getX() const;

    void setY( int y );
    int getY() const;

    Point & operator = ( const Point &xPoint );
    bool operator == ( const Point &xPoint ) const;

    bool includes( int cord_X, int cord_Y ) const;

    void draw() const;
};

PointCollection.h

CPP / C++ / C Code:
#pragma once

#include "Point.h"

const int MAX = 100;

class PointCollection
{
private:
    Point pts[ MAX ];
    int quantity;

public:
    PointCollection( void );
    PointCollection( const PointCollection &xPointCollection );
    ~PointCollection( void );

    // To know how many Points are
    // ..in the collection
    int size() const; 

    PointCollection operator = ( const PointCollection &xPointCollection );
    
    Point &operator[]( int index );
    //const Point &operator[]( int index ) const;

    bool isFull() const;
    bool isEmpty() const;

    bool includes( const Point &xPoint ) const;
    int indexOf( const Point &xPoint ) const;
};

Point.cpp

CPP / C++ / C Code:
#include "Point.h"

Point::Point( void )
{
    this->x = 0;
    this->y = 0;
}

Point::Point( int x, int y )
{
    this->x = x;
    this->y = y;
}

Point::Point( const Point &xPoint )
{
    this->x = xPoint.x;
    this->y = xPoint.x;
}

Point::~Point( void ){} // Destructor

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

void Point::setX( int x ) // set X
{
    this->x = x;
}

void Point::setY( int y ) // set Y
{
    this->y = y;
}


int Point::getX() const // get X
{
    return ( this->x );
}

int Point::getY() const // get Y
{
    return ( this->y );
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Point &Point::operator = ( const Point &xPoint )
{
    if( this != &xPoint )
    {
	this->x = xPoint.x;
	this->y = xPoint.y;
    }

    return ( *this );
}

bool Point::operator == ( const Point &xPoint ) const
{
    bool isEquality = false;
    if( this->x == xPoint.x &&
        this->y == xPoint.y )
    {
	isEquality = true;
    }
    return isEquality;
}

bool Point::includes( int cord_X, int cord_Y ) const
{
    return ( this->x == cord_X && this->y == cord_Y );
}

void Point::draw() const
{
    cout << '.';
}

PointCollection.cpp

CPP / C++ / C Code:
#include "PointCollection.h"

PointCollection::PointCollection( void )
{
    this->quantity = 0;
}

PointCollection::PointCollection( const PointCollection &xPointCollection )
{
    this->quantity = xPointCollection.quantity;

    for ( int i = 0; i < this->quantity; i++ )
	( this->pts )[ i ] = ( xPointCollection.pts )[ i ];
}

PointCollection::~PointCollection( void ) {}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

int PointCollection::size() const
{
    return ( this->quantity );
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Point &PointCollection::operator []( int index )
{
    return ( ( this->pts )[ index ] );
}

/*
const Point &PointCollection::operator []( int index ) const
{
    return ( ( this->pts )[ index ] );
}
*/

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

PointCollection PointCollection::operator =( const PointCollection &xPointCollection )
{
    this->quantity = xPointCollection.quantity;

    for ( int i = 0; i < this->quantity; i++ )
	( this->pts )[ i ] = ( xPointCollection.pts )[ i ];

    return ( *this );
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

bool PointCollection::isFull() const
{
    return ( this->quantity == MAX );
}

bool PointCollection::isEmpty() const
{
    return ( this->quantity == 0 );
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

bool PointCollection::includes( const Point &xPoint ) const
{
    bool thisOne = false;

    for ( int i = 0; i < this->quantity && ! thisOne; i++ )
    {
	//if ( ( *this )[ i ] == xPoint )
	if( pts[ i ] == xPoint )
	    thisOne = true;
    }

    return ( thisOne );
}

int PointCollection::indexOf( const Point &xPoint ) const
{
    int index = -1;

    for ( int i = 0; i < this->quantity && index == -1; i++ )
    {
	//if ( ( *this )[ i ] == xPoint )
	if( pts[ i ] == xPoint )
	    index = i; // I'd add a break here to end "early" if found
    }

    return ( index );
}


Everything compiled without errors or warnings on my Linux system, but I didn't execute or otherwise test or review the code for anything other than successful compilation.

You may note that I followed your coding style as closely as possible.


:davis:
  #3  
Old 11-Dec-2007, 08:41
Max_Payne Max_Payne is offline
New Member
 
Join Date: Oct 2007
Posts: 21
Max_Payne has a little shameless behaviour in the past
Post

Re: Classes & Collections


Thank You very much Davis.

I have added a new class to the previous program but I'm getting an error with the draw() function:

Here's the Header of the new class Page.h:
CPP / C++ / C Code:
#pragma once

#include "PointCollection.h"

class Page
{
private:
	int heigh;
	int width;

public:
	Page( void );
	Page( int width, int heigh );
	Page( const Page &xPage );
	~Page( void );

	void setWidth( int width );
	int getWidth() const;

	void setHeigh( int heigh );
	int getHeigh() const;

	void draw() const;
};



Here's the Source Code of the new class Page.cpp:
CPP / C++ / C Code:
#include "Page.h"

Page::Page( void )
{
	this->width = 0;
	this->heigh = 0;
}

Page::Page( int width, int heigh )
{
	this->width = width;
	this->heigh = heigh;
}

Page::Page( const Page &xPage )
{
	this->width = xPage.width;
	this->heigh = xPage.heigh;
}

Page::~Page( void ) {}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

void Page::setHeigh( int heigh )
{
	this->heigh = heigh;
}

void Page::setWidth( int width )
{
	this->width = width;
}

int Page::getHeigh() const
{
	return ( this->heigh );
}

int Page::getWidth() const
{
	return ( this->width );
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

void Page::draw() const
{
	bool included;

	for( int y = 0 ; y < this->heigh ; y++ )
	{
		for ( int x = 0; x < this->width ; x++ )
		{
			for ( int p = 0 ; p < PointCollection.size()  && !included ; p++ )
			{

				if ( PointCollection[ p ].includes( x, y ) )
				{
					PointCollection[ p ].draw();
					included = true;
				}

				if ( included )
					cout << ' ';
			}
		}
		cout << endl;
	}
}

------ Build started: Project: Project_Dibujo, Configuration: Debug Win32 ------
Compiling...
Page.cpp
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\page.c pp(75) : warning C4832: token '.' is illegal after UDT 'PointCollection'
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\pointc ollection.h(8) : see declaration of 'PointCollection'
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\page.c pp(75) : error C2275: 'PointCollection' : illegal use of this type as an expression
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\pointc ollection.h(8) : see declaration of 'PointCollection'
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\page.c pp(78) : error C2059: syntax error : '['
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\page.c pp(79) : error C2143: syntax error : missing ';' before '{'
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\page.c pp(80) : error C2143: syntax error : missing ';' before '['
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\page.c pp(80) : error C2337: 'p' : attribute not found
c:\documents and settings\mpayne007\my documents\visual studio 2005\projects\project_dibujo\project_dibujo\page.c pp(80) : error C2143: syntax error : missing ';' before '.'
Build log was saved at "file://c:\Documents and Settings\MPayne007\My Documents\Visual Studio 2005\Projects\Project_Dibujo\Project_Dibujo\Debug\ BuildLog.htm"
Project_Dibujo - 6 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


A simple hint that would help me solve this problems would be good.

Max.
  #4  
Old 11-Dec-2007, 08:59
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 509
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Classes & Collections


Do you have an object/array named PointCollection somewhere? I believe that PointCollection is the name of your class so you can't make a statement like

PointCollection[0].size();

You could however create an instance of that class and then use it:

PointCollection pc;
pc.size();
  #5  
Old 11-Dec-2007, 09:05
Max_Payne Max_Payne is offline
New Member
 
Join Date: Oct 2007
Posts: 21
Max_Payne has a little shameless behaviour in the past
Thumbs up

Re: Classes & Collections


Something so stupid can be torture to newbies.

Thank you very much for the Help...
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with Classes Within Classes jdbrine C++ Forum 2 29-Jun-2006 15:26
Classes initiation kdsXchris C++ Forum 3 05-Jun-2006 03:07
How do you make your own collections objects accept multiple types? robynkartinian C++ Forum 19 06-Mar-2006 10:23
Assistance with classes... Bravebird C++ Forum 7 27-Apr-2005 13:17
Fairly simple classes help please sammacs C++ Forum 0 30-Nov-2004 09:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:42.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.