Initializing a variable in the function argument is giving me an error

Feb 4, 2013 at 8:20pm
For this code below, I am getting these errors:

default argument given for parameter 1 of `void Point::print(double)'
after previous specification in `void Point::print(double)'



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<cmath>

using namespace std;

class Point{
      private:
              double x;
              double y;
      public:
             Point(void);
             Point(double xinit, double yinit);
             void print(double factor=1.0);
             void move(double dx, double dy);
             void moveto(double xnew, double ynew);
             double from_origin(void);
             double get_x(void);
             double get_y(void);
      };

Point::Point(){
               x=0;
               y=0;}
Point::Point(double xinit, double yinit){
                    x=xinit;
                    y=yinit;
                    }
void Point::print(double factor=1.0){
  //factor=1.0;
  cout << "(" << x*factor << "," << y*factor << ")";
}
void Point::move(double dx, double dy){
     x+=dx;
     y+=dy;
}
void Point::moveto(double xnew, double ynew){
     xnew=x;
     ynew=y;
}
double Point::from_origin(void){
       return sqrt(x*x+y*y);
}
double Point::get_x(void){
       return x;
}
double Point::get_y(void){
       return y;
}      




int main(){
    Point p1;
    Point p2(2,2);
    cout<<"p1 is : ";
    p1.print();
    cout<<endl<<"\np2 is : ";
    p2.print();
    p1.move(1,3);
    cout<<endl<<"\nNow p1 is : ";
    p1.print();
    p2.move(0.5,0.1);
    cout<<endl<<"\nNow p2 is : ";
    p2.print();
    cout<<"\nOf these two points : p1("<<p1.get_x()<<",";
    cout<<p1.get_y()<<") and p2("<<p2.get_x()<<",";
    cout<<p2.get_y()<<")\n";
    if(p1.from_origin()> p2.from_origin() ) cout<<"p1";
    else cout<<"p2 ";
    cout<<"is farther away from the origin (0,0)\n"<<endl;
    
    system("PAUSE");
    return 0;
    
}



When I replace the line:

" void Point::print(double factor=1.0){ "

with :

" void Point::print(double factor){
factor=1.0; "

It works. Can someone tell me why the code didn't work before? And if there is a better way of writing it.













Feb 4, 2013 at 8:23pm
You only need to include default parameters in the definition of the function, not the declaration. (At least in this example with classes).

Remove the 1.0 from
1
2
3
4
void Point::print(double factor=1.0){
  //factor=1.0;
  cout << "(" << x*factor << "," << y*factor << ")";
}
Last edited on Feb 4, 2013 at 8:24pm
Feb 4, 2013 at 8:24pm
well for one thing, if you're passing a double value to Point::print(double factor)
why would you then want to assign a literal const value (1.0) to factor?
it should be taking that argument from the callback.. otherwise it should just be:

void Point::print()
{
double factor = 1.0;
}

but I know that's not what you want either..
Last edited on Feb 4, 2013 at 8:25pm
Feb 4, 2013 at 8:37pm
Thanks! That makes more sense!
Feb 4, 2013 at 9:26pm
You're welcome!
If you're still following this.. I wonder if you should declare "factor" as a const double with yer private doubles x and y ^ in lines 8 and 9, instead of the function definition.

void Point::print(double factor=1.0)
maybe change that to this:
void Point::print()

then lines 28 - 31 could look more like this:
1
2
3
4
void Point::print(){
   //factor=1.0;
   cout << "(" << x*factor << "," << y*factor << ")";
}


Just tryn to streamline.
If I'm doing anything bad, someone please kindly let me know.
Last edited on Feb 4, 2013 at 9:31pm
Topic archived. No new replies allowed.