Need Help Calling a Function Twice

A

AJ

AJ

Hey I need help calling a constructor of a class twice, giving it
different parameters on the second call. The reason is because I want
to rotate a line and my convertpoint class find the new point to
rotate it to. With a circle i only need to call it once, but with a
line i need to call it twice for each endpoint. The problem is that i
keep getting this error:

csci135p6spinline.cpp:49: error: redeclaration of `ConvertPoint
convertpoint'
csci135p6spinline.cpp:48: error: `ConvertPoint convertpoint'
previously declared here

here is the csci135p6spinline.cpp file
#############################################################################################
#include "csci135canvas.h"
#include "csci135p6spinline.h"
#include "csci135p6spinconvertpoint.h"
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
using std::vector;
using std::ifstream;
using std::string;
using std::stringstream;

Line::Line()
{
ifstream image2;
string specs;
image2.open("image.canvas.txt");

while(image2.good())
{
string type;
getline(image2,specs);
stringstream paint(specs);
paint>>type;

if(type == "line")
{
stringstream art(specs);

paint>>Xposition_1>>Yposition_1>>Xposition_2>>Yposition_2;

linex1.push_back(Xposition_1);
liney1.push_back(Yposition_1);
linex2.push_back(Xposition_2);
liney2.push_back(Yposition_2);
}
}
}
void Line::draw(Canvas &canvas, double angle)
{
double theta = angle;
for(unsigned int i = 0; i < linex1.size(); i++)
{
double x_one = linex1;
double y_one = liney1;
double x_two = linex2;
double y_two = liney2;

ConvertPoint convertpoint(x_one,y_one,theta);
ConvertPoint convertpoint(x_two,y_two,theta);

canvas.DrawLine(x_one,y_one,x_two,y_two);
}
}
#############################################################################################

Thank You in advance for your help and time.

AJ,
 
I

Ian Collins

AJ said:
AJ

Hey I need help calling a constructor of a class twice, giving it
different parameters on the second call. The reason is because I want
to rotate a line and my convertpoint class find the new point to
rotate it to. With a circle i only need to call it once, but with a
line i need to call it twice for each endpoint. The problem is that i
keep getting this error:

csci135p6spinline.cpp:49: error: redeclaration of `ConvertPoint
convertpoint'

You can't create the same object twice! But you can have more than one
constructor for a class.
 
J

joseph cook

void Line::draw(Canvas &canvas, double angle)
{
     double theta = angle;
     for(unsigned int i = 0; i < linex1.size(); i++)
     {
          double x_one = linex1;
          double y_one = liney1;
          double x_two = linex2;
          double y_two = liney2;

          ConvertPoint convertpoint(x_one,y_one,theta);
          ConvertPoint convertpoint(x_two,y_two,theta);

          canvas.DrawLine(x_one,y_one,x_two,y_two);
     }}


I'm not sure I understand. Does creating the instances of
convertPoint have some sort of hidden side-effect just by creating
them? Because you create them, but don't use them subsequently.
Having such a side effect in a constructor is not very clear code in
this instance. If you are merely trying to enact this side-effect,
you could just create two objects:
ConvertPoint convertPoint(...);
ConvertPoint convertPoint2(...);

Joe Cook
 
A

AJ

void Line::draw(Canvas &canvas, double angle)
{
     double theta = angle;
     for(unsigned int i = 0; i < linex1.size(); i++)
     {
          double x_one = linex1;
          double y_one = liney1;
          double x_two = linex2;
          double y_two = liney2;

          ConvertPoint convertpoint(x_one,y_one,theta);
          ConvertPoint convertpoint(x_two,y_two,theta);
          canvas.DrawLine(x_one,y_one,x_two,y_two);
     }}

I'm not sure I understand.  Does creating the instances of
convertPoint have some sort of hidden side-effect just by creating
them?   Because you create them, but don't use them subsequently.
Having such a side effect in a constructor is not very clear code in
this instance.    If you are merely trying to enact this side-effect,
you could just create two objects:
ConvertPoint convertPoint(...);
ConvertPoint convertPoint2(...);

Joe Cook


AJ

Ok i did what you said but now i am getting a new error that is even
more confusing:

/tmp/cc6nkERP.o(.text+0xa2a): In function `Circle::draw(Canvas&,
double)':
: undefined reference to `ConvertPoint::ConvertPoint(double&, double&,
double)'
/tmp/ccqtgNmU.o(.text+0xb9e): In function `Line::draw(Canvas&,
double)':
: undefined reference to `ConvertPoint::ConvertPoint(double&, double&,
double)'
/tmp/ccqtgNmU.o(.text+0xbbb): In function `Line::draw(Canvas&,
double)':
: undefined reference to `ConvertPoint::ConvertPoint(double&, double&,
double)'
collect2: ld returned 1 exit status

here is circle::draw
##########################################################################################
void Circle::draw(Canvas &canvas, double angle)
{
double theta = angle;

for(unsigned int i = 0; i < circlex.size(); i++)
{
double x = circlex;
double y = circley;
double r = circleradius;

ConvertPoint convertpoint(x,y,theta);
canvas.DrawCircle(x,y,r);
}
}
##########################################################################################

Here is line::draw
##########################################################################################
void Line::draw(Canvas &canvas, double angle)
{
double theta = angle;
for(unsigned int i = 0; i < linex1.size(); i++)
{
double x_one = linex1;
double y_one = liney1;
double x_two = linex2;
double y_two = liney2;

ConvertPoint convertpoint(x_one,y_one,theta);
ConvertPoint convertpoint2(x_two,y_two,theta);

canvas.DrawLine(x_one,y_one,x_two,y_two);
}
}
##########################################################################################

Thank You in advance for you time and help

AJ,
 
J

Jonathan Lee

Ok i did what you said but now i am getting a new error that is even
more confusing:

/tmp/cc6nkERP.o(.text+0xa2a): In function `Circle::draw(Canvas&,
double)':
: undefined reference to `ConvertPoint::ConvertPoint(double&, double&,
double)'
/tmp/ccqtgNmU.o(.text+0xb9e): In function `Line::draw(Canvas&,
double)':
: undefined reference to `ConvertPoint::ConvertPoint(double&, double&,
double)'
/tmp/ccqtgNmU.o(.text+0xbbb): In function `Line::draw(Canvas&,
double)':
: undefined reference to `ConvertPoint::ConvertPoint(double&, double&,
double)'
collect2: ld returned 1 exit status

You've called Circle::draw() and Line::draw() somewhere, but haven't
provided the source to the linker to stitch the separate parts of the
program together.

Presumably you have a separate "circle.cpp", "line.cpp" and
"main.cpp".
Make sure you're passing all of these to your compiler (likely g++
from the error codes). The easiest way would be to simply pump every
cpp file into g++.

g++ main.cpp circle.cpp line.cpp -o yourprogname

If you build the files separately, something like:

g++ main.cpp circle.o line.o -o yourprogname

--Jonathan
 
A

AJ

You've called Circle::draw() and Line::draw() somewhere, but haven't
provided the source to the linker to stitch the separate parts of the
program together.

Presumably you have a separate "circle.cpp", "line.cpp" and
"main.cpp".
Make sure you're passing all of these to your compiler (likely g++
from the error codes). The easiest way would be to simply pump every
cpp file into g++.

   g++ main.cpp circle.cpp line.cpp -o yourprogname

If you build the files separately, something like:

   g++ main.cpp circle.o line.o -o yourprogname

--Jonathan- Hide quoted text -

- Show quoted text -

AJ,

Thank you for the help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top