error here can anyone help

This is a discussion on error here can anyone help within the C++ Programming forums, part of the General Programming Boards category; the question is Construct a class named Coord that contain two floating –point data member named xval and yval , ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    61

    error here can anyone help

    the question is
    Construct a class named Coord that contain two floating –point data member named xval and yval , which will be used to store the x and y values of a point in rectangular coordinates. The function member should include appropriate constructor and display functions and friend function named conv_pol (). The conv_pol() function should accept two floating point member that represent a point in polar coordinates and convert them into rectangular coordinates .
    and here's my coding..
    Code:
    #include<iostream>
    #include<cmath>
    using namespace std;
     
    
    
    class Coord
    
    {
    	friend Coord conv_pol( float ,float);
    private:
    float xval;
    float yval;
    
    public:
    	void print();
    
    	Coord(float,float);
    
    
    };
    
    Coord::Coord(float x,float y)
    {
    	xval = x;
    	yval = y;
    }
    
    
    void Coord::print()
    {
    	cout<<'('<<xval<<','<<yval<<')'<<endl;
    }
    
    Coord conv_pol(float x,float y)
    { 
       Coord temp;
       temp.xval = temp.yval*cos(temp.yval*3.142 /180);
       temp.yval = temp.yval*sin(temp.yval*3.142 /180);
       return temp;
     
    }
    int main()
    {
    	Coord a;
    	a=conv_pol(10,210);
    	a.print();
    
    
     return 0;
    }
    the error says no appropriate default constructor not initialized but then.. have already done Coord(float,float) which is the default constructor..
    i am confused on how to edit the constructor here..
    so what do i have to do??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you only have a constructor that takes two float parameters. You need to either give those parameters default values, or have a second constructor with no parameters.

    Edit: Or you can pass two values to the constructor, e.g.:
    Code:
    Coord conv_pol(float x,float y)
    { 
       Coord temp(x, y);
       temp.xval = temp.yval*cos(temp.yval*3.142 /180);
       temp.yval = temp.yval*sin(temp.yval*3.142 /180);
       return temp;
     
    }
    Note that your current code is broken - you are using xval and yval, but they have not been assigned any value. With the above fix, it should do the right thing.

    Surely your 3.142 should be a constant named PI or pi?


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    As a variation of matsp's theme, I propose that you make conv_pol() a non-friend function, and then just write:
    Code:
    Coord conv_pol(float x, float y)
    {
        return Coord(y * cos(y * 3.142 / 180), y * sin(y * 3.142 / 180));
    }
    Incidentally, I notice that x is not used... is that intended or a bug?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21