Thread: Classes & Collections

  1. #1
    Registered User Max_Payne's Avatar
    Join Date
    Dec 2007
    Posts
    12

    Post 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. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, and you are expecting us to fix these errors for you? Is this your code? Do you have any understanding of why these errors appear? Do you know how templates and classes work?
    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.

  3. #3
    Registered User Max_Payne's Avatar
    Join Date
    Dec 2007
    Posts
    12
    I'm not expecting anyone to fix the problems. A Hint that would help me fix this problems would be good.

    Don't worry about it. Someone from another forum gave me that hint that helped me solve these problems.

    Your statement would have been valid if i haven't showed any effort but that's not the case here.

    p.s: This doesn't have to do anything with Templates.
    Last edited by Max_Payne; 12-11-2007 at 09:40 AM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Max_Payne View Post
    Your statement would have been valid if i haven't showed any effort but that's not the case here.
    You showed zero effort here. You just dumped the code plus all the errors with nothing else added.

    Same as you did here, which I assume is the another forum you speak of: http://forums.devx.com/showthread.php?t=165498

  5. #5
    Registered User Max_Payne's Avatar
    Join Date
    Dec 2007
    Posts
    12
    So besides making the code and showing it to you. what else do you want me to do?
    Last edited by Max_Payne; 12-11-2007 at 09:22 PM.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Give us some idea of what we're looking at. If you can't find the error and you are totally familiar with the code (assuming that you wrote it all) and you have the compiler message, then why do you assume we can figure it out? To do so requires us to slosh through the compiler errors as well as your code.

    Instead, tell us what you think the problem is related to, what the lines of code are in question (because I don't feel like counting X lines from the start of file Y in something this large. For smaller code examples, this isn't as much of a concern), and what you did to try to fix it (so we already know the beaten trails).

  7. #7
    Registered User Max_Payne's Avatar
    Join Date
    Dec 2007
    Posts
    12

    Thumbs up

    Well thanks anyway. But the problems were just some mistypes that i fixed and it's working.

    I'll keep what you said in mind for future threads..

    ~Max

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Max_Payne View Post
    p.s: This doesn't have to do anything with Templates.
    This kinda just goes to show how little you know.
    90&#37; of those error messages related to template functions in the STL.
    You aren't supposed to dump them on us - you are supposed to ask if there's particular error you don't understand. Many of those errors were because you were incorrectly using the STL methods.
    Plus you have a const function (or more), and you messed up a little with those, as well.

    Take it like they say:
    Learn what these errors means by perhaps asking and then try to fix the problem yourself. It will help you in the long run.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM