Sign in to follow this  

Calling functions from Classes

This topic is 3662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a class with both a header and implementation. In my main() function in a separate main.cpp file, I am attempting to call a function from a class. I know I have all of the #includes all set, and I can set my private variables. Ex)
 class ShapeDrawer
{
  public:
	ShapeDrawer();
	void clear();
	void drawLine(Line);
	void drawTriangle(Triangle);
	void writeFile(string fileName);
	const static int N_ROWS = 100;
	const static int N_COLS = 100;
	float triArea(int xa, int ya, int xb, int yb, int xref, int yref);

  private:
	void initArray();
	int pixeldata[3 * N_ROWS * N_COLS];
	void setPixel(int X, int Y, char color, int data);
	void setOnePixel(int X, int Y, int data);
	int evalFunc(int x, int y, int a, int b, int c);
	
};

void ShapeDrawer::drawLine(Line l) {
	int xa = l.getStart().getX();
	int ya = l.getStart().getY();
	int xb = l.getEnd().getX();
	int yb = l.getEnd().getY();
	int red = l.getColor().getRed();
	int blue = l.getColor().getBlue();
	int green = l.getColor().getGreen();

I'm sure its my newbie understanding of classes, but I can't call drawLine in my main *.cpp file. Can someone tell me how to call drawLine?? thanks

Share this post


Link to post
Share on other sites
This should work:

ShapeDrawer shape;

shape.drawLine (line);
or for pointers...

ShapeDrawer* shape=new ShapeDrawer;

shape->drawLine (line);

// when you are done with the object...
delete shape;

I dont know your line object, so you will need to fill out the parameters.

Share this post


Link to post
Share on other sites
The shapeDrawer shape doesn't work because I don't define that type. I get the error message:

error C3861: 'drawLine': identifier not found

As if I never created the drawLine function. I tried

Point p1(11,11); // test of default constructor
Point p2(3, 9);// test of the other constructor
Color m(255,0,0);
Line x(p1,p2,m);

drawLine(x);

which is all I should need to call drawLine, except I just don't seem to have it set up so I can call that from my shapeDrawer.cpp.

Any other ideas?

Share this post


Link to post
Share on other sites
Quote:
Original post by UMichGOBLUE
The shapeDrawer shape doesn't work because I don't define that type. I get the error message:

error C3861: 'drawLine': identifier not found

As if I never created the drawLine function. I tried

Point p1(11,11); // test of default constructor
Point p2(3, 9);// test of the other constructor
Color m(255,0,0);
Line x(p1,p2,m);

drawLine(x);

which is all I should need to call drawLine, except I just don't seem to have it set up so I can call that from my shapeDrawer.cpp.


Assuming that is from your main.cpp file, the problem you have is that you're calling drawLine(x) as if it were a static function. That line of code will only work if the function "drawLine()" is static, then it would look like...

ShapeDrawer::drawLine(x);

Share this post


Link to post
Share on other sites
You can't call drawline directly. It's not a general function, but a member of your shapedrawer class. You access that member function through an instance of the shapedrawer class, as outlined by Crypter above.

Share this post


Link to post
Share on other sites

This topic is 3662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this