0

I have a homework assignment that asks for me to make a class named Coord containing two double variables named xval and yval. In the class, there should be construtor and display methods and a friend function named conPol(). convPol() should accept two doubles, r and theta to convert polar coordinates into polar coordinates. I have everything coded, except now I've run into some errors. Here is what I have so far

#include <iostream>
#include <cmath>

using namespace std;

class Coord
{

    friend double covPol(Coord&, Coord&);
private:

    double xval, yval;

public:

     Coord(double, double);
    void  display();


};

Coord::Coord(double X, double Y)
 {
    xval = X;
    yval = Y;
}

void Coord::display()
{
    cout << "The x-cordinate is: " << endl;
    cout << "The y- coordinate is: " << endl;
}

double convPol(Coord &r, Coord &theta)
{
    xval = r* cos (theta);
    yval = r* sin (theta); 
}


int main()
{
    Coord One(2 , 4); 

    One.convPol(2,4);
    One.display(); 

    return 0; 
}

Here are the errors:

Error   1   error C2065: 'xval' : undeclared identifier c:\users\thomas\documents\visual studio 2013\projects\conpol().cpp\conpol().cpp\source.cpp  36  1   conPol().cpp

Error   2   error C2665: 'cos' : none of the 3 overloads could convert all the argument types   c:\users\thomas\documents\visual studio 2013\projects\conpol().cpp\conpol().cpp\source.cpp  36  1   conPol().cpp

Error   3   error C2065: 'yval' : undeclared identifier c:\users\thomas\documents\visual studio 2013\projects\conpol().cpp\conpol().cpp\source.cpp  37  1   conPol().cpp

Error   4   error C2665: 'sin' : none of the 3 overloads could convert all the argument types   c:\users\thomas\documents\visual studio 2013\projects\conpol().cpp\conpol().cpp\source.cpp  37  1   conPol().cpp

Error   5   error C2039: 'convPol' : is not a member of 'Coord' c:\users\thomas\documents\visual studio 2013\projects\conpol().cpp\conpol().cpp\source.cpp  45  1   conPol().cpp

6   IntelliSense: identifier "xval" is undefined    c:\Users\Thomas\Documents\Visual Studio 2013\Projects\conPol().cpp\conPol().cpp\Source.cpp  36  2   conPol().cpp

7   IntelliSense: no instance of overloaded function "cos" matches the argument list
        argument types are: (Coord) c:\Users\Thomas\Documents\Visual Studio 2013\Projects\conPol().cpp\conPol().cpp\Source.cpp  36  12  conPol().cpp

8   IntelliSense: identifier "yval" is undefined    c:\Users\Thomas\Documents\Visual Studio 2013\Projects\conPol().cpp\conPol().cpp\Source.cpp  37  2   conPol().cpp

9   IntelliSense: no instance of overloaded function "sin" matches the argument list

        argument types are: (Coord) c:\Users\Thomas\Documents\Visual Studio 2013\Projects\conPol().cpp\conPol().cpp\Source.cpp  37  12  conPol().cpp

From what I understand from this, is that my friend function is not able to get the private variables from my class. Also, my sin and cos functions are not working. I've included so I'm not sure why it doesn't work. As for the friend function not being able to get the variables, I've no idea.

TPA
  • 7
  • 2
  • You misspelled `covPol` as `convPol`. Also, `friend` functions are not member functions, they're non-member functions. Make `convPol` a member function since you need to access data members. – template boy Mar 23 '15 at 20:37
  • @templateboy it was to my understanding that friend functions were able to access data members. – TPA Mar 23 '15 at 20:45
  • Yes, but `convPol` isn't referring to any one instance of `Coord` when you say `xval = r* cos (theta);`. It's not a member function; It doesn't know who `xval` belongs to. So either take the `Coord` as a parameter and so `c.xval = ...` or make it a member function instead. – template boy Mar 23 '15 at 20:48

3 Answers3

0

From your code you are trying to accomplish the conversion of polar coordinated to rectangular ones.

Since you already have a constructor in hand, you dont even need a friend function. Also the arguments should be of type float or double not Coord.

The function signature is thus:

Coord convPol(double r, double theta)
{
    double xval = r* cos (theta), 
           yval = r* sin (theta); 
    return Coord(xval, yval);
}

This function is sufficient for the purpose you are trying to achieve.

a_pradhan
  • 3,285
  • 1
  • 18
  • 23
0

The only thing friend does is relax permissions.

convPol, as you've defined it, is a global function, not a member function of Coord. Thus, it makes no sense to call it as a member function or to implement it as if it were a member function. But if it were to manipulate any actual objects of type Coord, you could access their xval and yval members inside of this function.

Regarding sin and cos not working, the error messages tell you why:

no instance of overloaded function "cos" matches the argument list
    argument types are: (Coord)

This should prompt you to check the type of the variables you're passing in: e.g. when you call cos(theta), you can see that theta has type Coord. That is surely not what you meant!

  • Hmm.. I'm confused on how I defined convPol as a global function. I gave it friend in the class. – TPA Mar 24 '15 at 05:16
0

You've mentioned several friend functions: conPol() and convPol() in your text, and then covPol(Coord&, Coord&) and convPol(Coord &r, Coord &theta) in your code. I'd start by figuring out what you want your function to be called.

aldo
  • 2,927
  • 21
  • 36