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 01-Oct-2004, 13:48
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

First time using classes


Just wanted to run this past someone in the know. This is my first time designing/using classes in C++ and I wanted to make sure I sorta know what I'm doing before I go further. So, if someone would be kind enough to look at my code and let me know if I'm jacked up or not, that would be great.

I'm not sure about the changePosition() method. What I want to do is move a point from one location to another. (Just thought it would be a nice thing to have available.) I just don't know if what I have in mind is what I coded. I haven't completed the main program yet, just this header file, since I want to make sure I'm on the right path before I get lost.

Thanks for the look.

CPP / C++ / C Code:
/*************************
* CartesianPoint.h
*Purpose: Declares the class CartesianPoint
************************/
#ifndef CARTESIANPOINT //compile-once wrapper
#define CARTESIANPOINT

#include <iostream>
using namespace std;

class CartesianPoint
{
public:

CartesianPoint (); //default-value constructor
CartesianPoint (int xinput, int yinput); //explicit-value constructor

CartesianPoint getPosition () const; //find present location
CartesianPoint changePosition (int xinput, int yinput); //change point's location 

private:

double x, y;
};

//-----------Default-value constructor-----------
inline
CartesianPoint::CartesianPoint ()
{
x = 0.0;
y = 0.0;
}

//-----------Explicit-value constructor----------
inline
CartesianPoint::CartesianPoint (double xinput, double yinput)
{
x = xinput;
y = yinput;
}

//---------Get location-------------------
inline CartesianPoint
CartesianPoint::getPosition () const
{
return CartesianPoint (x, y);
}

//-------Change location---------------
inline CartesianPoint CartesianPoint::changePosition(int xinput, int yinput)
{
CartesianPoint cur_loc = getPosition();
cur_loc = CartesianPoint(xinput, yinput);
return cur_loc;
}

#endif
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #2  
Old 10-Oct-2004, 01:08
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough
seems all good to me
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #3  
Old 12-Oct-2004, 18:25
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Angry

Error messages on compile


I've finally completed all the code for this problem, but now I get the inevitable compile errors. The following are examples of the errors.

Code:
cannot resolve overloaded function 'getSlope' based on conversion to type 'double' request for member 'getY' in line1.Line::getPoint() const', which is of non-aggregate type '<unkown type>' 'CartesianPoint LineSegment::pt1' is private within this context no match for 'std::istream&>>Line&' operator; candidates are ...

I think I'm being told that I'm trying to access objects and methods incorrectly, but I don't see how to fix it. Any help? My headers and source are attached in a zip file.

Thanks.
Attached Files
File Type: zip Lines.zip (5.1 KB, 14 views)
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #4  
Old 12-Oct-2004, 19:49
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello crystalattice. Using g++ & Linux, I got a ton of errors, mostly related to my compiler. However, I did notice a few things:

First, in this sample you are calling functions, but you aren't using (). This:
CPP / C++ / C Code:
void
Line::pointSlope (Line line)
{
	CartesianPoint point_line = getPoint;
	double slope_line = getSlope;
	double point_x = point_line.getX;
	double point_y = point_line.getY;

Should be more like:
CPP / C++ / C Code:
void
Line::pointSlope (Line line)
{
	CartesianPoint point_line = getPoint();
	double slope_line = getSlope();
	double point_x = point_line.getX();
	double point_y = point_line.getY();

Also, why are you passing in a Line? I am missing something there, I think...

Also, this is really confusing to me here:
CPP / C++ / C Code:
void
Line::slopeInterecept (Line line)
{
	cout << "The slope-intercept equation of a line is "
		<< "y=" << pointSlope.slope_line << "(x-" << pointSlope.point_x
		<< ")+" << pointSlope.point_y << endl;
}

pointSlope is a member function of Line, but it appears that you are trying to use it as a defined variable of a class? Also, I can not figure out where slope_line, point_y & point_x are defined local to Line.

My use/understanding of classes are pretty basic and maybe you are trying to do something that is way over my head here, so I apologize if that is the case...
  #5  
Old 12-Oct-2004, 22:03
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Oh, trust me, I'm not trying to do tricky things. If you don't understand it, then I probably "made it up" because I don't understand my textbook all that well.

I'll look again at what you found. I didn't even realize I'd missed the "()" on the end of the functions. Thanks.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #6  
Old 13-Oct-2004, 07:20
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by crystalattice
Oh, trust me, I'm not trying to do tricky things. If you don't understand it, then I probably "made it up" because I don't understand my textbook all that well.

Okay. When you work with classes, your member functions are meant to work on the member data that you have. Classes are not that much different from a structure except that you have member functions. So there is no need to pass member data to a member function
CPP / C++ / C Code:
void
Line::pointSlope() //Instead of Line::pointSlope (Line line)

Also member functions should really be limited to working on member data only. If you have a function that finds where two lines intersect, this should be defined external as you will need to pass two seperate line classes into it.

All in all, I don't think that you are too far off on this. You may just want to step back and re-evaluate your member functions.

HTH.
  #7  
Old 13-Oct-2004, 08:21
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 893
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by dsmith
First, in this sample you are calling functions, but you aren't using ()

As this is true on most compilers, I just want to add that in case of MS VC, it works either way when creating a new instance of a class.

CPP / C++ / C Code:
	CBase * testing = new CChild() ;
//	CBase * testing = new CChild; // same thing
//	That is however wrong when used like this
	testing->TestPureVitual;

Kind regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

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
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56
time Problem zuzupus MySQL / PHP Forum 9 24-Jul-2003 07:02

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

All times are GMT -6. The time now is 21:12.


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