Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,378 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,031 people online right now. Registration is fast and FREE... Join Now!




Need some help/advise for Public/Private classes

 
Reply to this topicStart new topic

Need some help/advise for Public/Private classes

nirali35
24 Sep, 2006 - 08:57 AM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2006
Posts: 3


My Contributions
Hello!

I am considerably new in C++ and need some of your advises!

Actually I am doing my assignment and getting some troubles in the way.
I am using Microsoft Visual C++ (from Visual Studio .Net 2005)

While I try to compile my code, I get the following erros:

QUOTE
error C3861: 'getX': identifier not found
error C3861: 'getY': identifier not found
error C3861: 'resMag': identifier not found
error C3861: 'resDir': identifier not found


It seems to me that I have to make the classes getX, getY, resMag and resDir PUBLIC. But I do not see the proper reason for that.

I am providig the code to help you out understand the problem better:

CODE
/*
   Developer  : Nirali Patel
   Course     : CSCI 201
   Project    : Assignment #1 - VECTORS
   Date       : 09/23/2006

   File             : vector.cpp
   Description: vector class.
*/

#include <iostream>
#include <stdlib.h>
#include <cmath>

using namespace std;

class vector
{
private:
    double dir; // Vector direction
    double mag; // vector magnitude

    double getX(double, double); // Get horizontal components of the vectors
                double getY(double, double); // Get vertical components if the vectors
    
                double resDir(double, double); // Resultant Directions
                double resMag(double, double); // Resultant Magnitudes

public:
    vector(); // Constructor to make the Vector NULL
                vector(vector &temp);  // Constructor to copy the Vector
    
    friend vector operator + (const vector& lhs, const vector& rhs);
    friend vector operator - (const vector& lhs, const int rhs);
    friend vector operator * (const vector& lhs, const vector& rhs);

    friend int operator == (const vector& lhs, const vector& rhs);
    friend int operator != (const vector& lhs, const vector& rhs);

    friend ostream& operator << (ostream& out, const vector& rhs);
    friend istream& operator >> (istream& out, vector& rhs);
};

/*
    Default Constructor
*/
vector::vector()
{
    dir = 0.0; // Set vector direction to NULL
    mag = 0.0; // Set vector magnitude to NULL
}

/*
    Copy Constructor
*/
vector::vector(vector &temp)
{
    dir = temp.dir; // Set vector direction to Temperory vector direction
    mag = temp.mag; // Set vector mahnitude to Temperory vector magnitude
}

/*
    Get horizontal component
*/
double vector::getX(double ang, double dist)
{
    double ans;

    ans = sin[ang*(M_PI/180)]*dist; // Horizontal (X) component

    return ans;
}

/*
    Get vertical component
*/
double vector::getY(double ang, double dist)
{
    double ans;

    ans = cos[ang*(M_PI/180)]*dist; // Vertical (Y) component

    return ans;
}

/*
    Get the resultant magnitude of 2 vectors
*/
double vector::resMag(double x, double y)
{
    double ans;

    ans = sqrt(pow(x,2)+pow(y,2));

    return ans;
}

/*
    Get the resultant direction of 2 vectors
*/
double vector::resDir(double x, double y)
{
    double ans;

    ans = atan(y/x);

    return ans;
}

/*
    Operator +
*/
vector operator + (const vector& lhs, const vector& rhs)
{
    vector ans;

    double lhsDir, rhsDir;
    double lhsMag, rhsMag;

    double lhsX, rhsX;
    double lhsY, rhsY;

    double X, Y;

    lhsDir = lhs.dir;
    rhsDir = rhs.dir;

    lhsMag = lhs.mag;
    rhsMag = rhs.mag;

    lhsX = getX(lhsDir, lhsMag); // Get horizontal component of LHS vector
    rhsX = getX(rhsDir, rhsMag); // Get horizontal component of RHS vector

    lhsX = getY(lhsDir, lhsMag); // Get vertical component of LHS vector
    rhsX = getY(rhsDir, rhsMag); // Get vertical component of RHS vector

    X = lhsX + rhsX; // Total of all horizontal components
    Y = lhsY + rhsY; // Total od all vertical components

    ans.mag = resMag(X, Y);
    ans.dir = resDir(X, Y);

    return ans;
}


Thank you smile.gif

This post has been edited by Dark_Nexus: 26 Sep, 2006 - 10:59 PM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Need Some Help/advise For Public/Private Classes
24 Sep, 2006 - 02:15 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,013



Thanked: 18 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
If the methods will be accessed by other classes or non-friend classes, then the methods must be public, as private methods are not accessable. Try doing some reading on protected methods smile.gif
User is offlineProfile CardPM
+Quote Post

Xing
RE: Need Some Help/advise For Public/Private Classes
24 Sep, 2006 - 05:32 PM
Post #3

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
@nirali35

Where is your main function?
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Need Some Help/advise For Public/Private Classes
24 Sep, 2006 - 06:48 PM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,013



Thanked: 18 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
the main function of this code, is likely in another class file, thus the issue, if it was contained within this same class, i doubt the declaration of private/public would be much of an issue, pending it's placement.
User is offlineProfile CardPM
+Quote Post

nirali35
RE: Need Some Help/advise For Public/Private Classes
24 Sep, 2006 - 06:55 PM
Post #5

New D.I.C Head
*

Joined: 24 Sep, 2006
Posts: 3


My Contributions
Well, all problems solved for the above question! Thanks everyone!

Now I am having some un-understandable errors:

QUOTE
~/projects/1_VECTORS> gcc main.cpp
Undefined first referenced
symbol in file
std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::_Setprecision)/var/tmp//ccOqVw6x.o
__gxx_personality_v0 /var/tmp//ccOqVw6x.o
atan /var/tmp//ccOqVw6x.o
std::basic_ostream<char, std::char_traits<char> >::operator<<(std::ios_base& (*)(std::ios_base&))/var/tmp//ccOqVw6x.o
std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)/var/tmp//ccOqVw6x.o
std::basic_ostream<char, std::char_traits<char> >::operator<<(double)/var/tmp//ccOqVw6x.o
std::cin /var/tmp//ccOqVw6x.o
sin /var/tmp//ccOqVw6x.o
cos /var/tmp//ccOqVw6x.o
sqrt /var/tmp//ccOqVw6x.o
std::ios_base::Init::~Init [in-charge]()/var/tmp//ccOqVw6x.o
std::ios_base::Init::Init[in-charge]()/var/tmp//ccOqVw6x.o
std::basic_istream<char, std::char_traits<char> >::operator>>(double&)/var/tmp//ccOqVw6x.o
std::cout /var/tmp//ccOqVw6x.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
~/projects/1_VECTORS>


File: vector.cpp:
CODE
/*
   Developer  : Munjal Patel
   Course     : CSCI 201
   Project    : Assignment #1 - VECTORS

   File       : vector.cpp
   Description: vector class.
*/

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <iomanip>

#define PI = 3.14;

using namespace std;

class vector
{
private:
    double dir; // Vector direction
    double mag; // vector magnitude

    friend double getX(double, double); // Get horizontal components of the vectors
    friend double getY(double, double); // Get vertical components if the vectors
    
    friend double resDir(double, double); // Resultant Directions
    friend double resMag(double, double); // Resultant Magnitudes

public:
    vector(); // Constructor to make the Vector NULL
    vector(vector &temp);  // Constructor to copy the Vector
    
    friend vector operator + (const vector& lhs, const vector& rhs);
    friend vector operator - (const vector& lhs, const vector& rhs);
    friend vector operator * (const vector& lhs, const int rhs);
    friend vector operator * (const int lhs, const vector& rhs);

    friend int operator == (const vector& lhs, const vector& rhs);
    friend int operator != (const vector& lhs, const vector& rhs);

    friend ostream& operator << (ostream& out, const vector& rhs);
    friend istream& operator >> (istream& out, vector& rhs);
};

/*
    Default Constructor
*/
vector::vector()
{
    dir = 0.0; // Set vector direction to NULL
    mag = 0.0; // Set vector magnitude to NULL
}

/*
    Copy Constructor
*/
vector::vector(vector &temp)
{
    dir = temp.dir; // Set vector direction to Temperory vector direction
    mag = temp.mag; // Set vector mahnitude to Temperory vector magnitude
}

/*
    Get horizontal component
*/
double getX(double ang, double dist)
{
    double ans;

    ans = sin(ang*(M_PI/180))*dist; // Horizontal (X) component

    return ans;
}

/*
    Get vertical component
*/
double getY(double ang, double dist)
{
    double ans;

    ans = cos(ang*(M_PI/180))*dist; // Vertical (Y) component

    return ans;
}

/*
    Get the resultant magnitude of 2 vectors
*/
double resMag(double x, double y)
{
    double ans;

    ans = sqrt(pow(x,2)+pow(y,2));

    return ans;
}

/*
    Get the resultant direction of 2 vectors
*/
double resDir(double x, double y)
{
    double ans;

    ans = atan(y/x);

    return ans;
}

/*
    Operator +
*/
vector operator + (const vector& lhs, const vector& rhs)
{
    vector ans;

    double lhsDir, rhsDir;
    double lhsMag, rhsMag;

    double lhsX, rhsX;
    double lhsY, rhsY;

    double X, Y;

    lhsDir = lhs.dir;
    rhsDir = rhs.dir;

    lhsMag = lhs.mag;
    rhsMag = rhs.mag;

    lhsX = getX(lhsDir, lhsMag); // Get horizontal component of LHS vector
    rhsX = getX(rhsDir, rhsMag); // Get horizontal component of RHS vector

    lhsX = getY(lhsDir, lhsMag); // Get vertical component of LHS vector
    rhsX = getY(rhsDir, rhsMag); // Get vertical component of RHS vector

    X = lhsX + rhsX; // Total of all horizontal components
    Y = lhsY + rhsY; // Total od all vertical components

    ans.mag = resMag(X, Y);
    ans.dir = resDir(X, Y);

    return ans;
}

/*
    Operator -
*/
vector operator - (const vector& lhs, const vector& rhs)
{
    vector ans;

    double lhsDir, rhsDir;
    double lhsMag, rhsMag;

    double lhsX, rhsX;
    double lhsY, rhsY;

    double X, Y;

    lhsDir = lhs.dir;
    rhsDir = rhs.dir;

    lhsMag = lhs.mag;
    rhsMag = rhs.mag;

    lhsX = getX(lhsDir, lhsMag); // Get horizontal component of LHS vector
    rhsX = getX(rhsDir, rhsMag); // Get horizontal component of RHS vector

    lhsX = getY(lhsDir, lhsMag); // Get vertical component of LHS vector
    rhsX = getY(rhsDir, rhsMag); // Get vertical component of RHS vector

    X = lhsX - rhsX; // Subtract 2nd component from the 1st
    Y = lhsY - rhsY; // Subtract 2nd component from the 1st

    ans.mag = resMag(X, Y);
    ans.dir = resDir(X, Y);

    return ans;
}

/*
    Operator * (vector*int)
*/
vector operator * (const vector& lhs, const int rhs)
{
    vector ans;

    double lhsDir, lhsMag;

    lhsDir = lhs.dir;
    lhsMag = lhs.mag;

    lhsMag *= rhs;

    if (rhs < 0.0)
    {
        lhsDir -= 180.0;
    }

    ans.dir = lhsDir;
    ans.mag = lhsMag;

    return ans;
}

/*
    Operator * (int*vector)
*/
vector operator * (const int lhs, const vector& rhs)
{
    vector ans;

    double rhsDir, rhsMag;

    rhsDir = rhs.dir;
    rhsMag = rhs.mag;

    rhsMag *= lhs;

    if (lhs < 0.0)
    {
        rhsDir -= 180.0;
    }

    ans.dir = rhsDir;
    ans.mag = rhsMag;

    return ans;
}

/*
    Operator ==
*/
int operator == (const vector& lhs, const vector& rhs)
{
    if (lhs.dir != rhs.dir)
    {
        return 0;
    }
    if (lhs.mag != rhs.mag)
    {
        return 0;
    }

    return 1;
}

/*
    Operator !=
*/
int operator != (const vector& lhs, const vector& rhs)
{
    if (lhs.dir == rhs.dir)
    {
        return 0;
    }
    if (lhs.mag == rhs.mag)
    {
        return 0;
    }

    return 1;
}

/*
    Operator <<
*/
ostream& operator << (ostream& out, const vector& rhs)
{
    cout << setprecision(2) << fixed;
    out << rhs.mag << " " << rhs.dir;

    return out;
}

/*
    Operator >>
*/
istream& operator >> (istream& in, vector& rhs)
{
    double mag, dir;

    cout << "\n";
    cout << "Enter the Magnitude and Direction of the Vector seperated by a space. \n";
    cin >> mag >> dir;

    mag *= 1.00;
    dir *= 1.00;

    if (mag == 0.0)
    {
        dir = 0.0;
    }

    rhs.dir = dir;
    rhs.mag = mag;

    in >> mag >> dir;
    return in; // Just fooling compiler
}


File: main.cpp:

CODE
#include "vector.cpp"

int main()
{
    vector a, b;
    vector c, d, e, f, g;

    cout << "\n";
    cout << "Vector A:\n";
    cin >> a;
    cout << "\nVector B:\n";
    cin >> b;

    c = a+b;
    d = a-b;
    e = b-a;
    f = a*2;
    g = b*2;

    cout << "\n A+B: \n" << c;
    cout << "\n A-B: \n" << d;
    cout << "\n B-A: \n" << e;
    cout << "\n A x 2: \n" << f;
    cout << "\n B x 2: \n" << g;

    if (a == b)
    {
        cout << "\n A = B";
    }

    if (a != b)
    {
        cout << "\n A != B";
    }

    a = b;

    if (a == b)
    {
        cout << "\n A = B";
    }
}


Any ideas??

This post has been edited by Dark_Nexus: 26 Sep, 2006 - 11:00 PM
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Need Some Help/advise For Public/Private Classes
25 Sep, 2006 - 05:42 AM
Post #6

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Works fine for me.
I just got a few "Variable not Used" Warnings and One Error in which you haven't declared the fixed variable.
CODE

cout << setprecision(2) << fixed;

Try changing this and compile it again.
User is offlineProfile CardPM
+Quote Post

mk_2302
RE: Need Some Help/advise For Public/Private Classes
27 Sep, 2006 - 07:33 AM
Post #7

New D.I.C Head
*

Joined: 27 Sep, 2006
Posts: 1


My Contributions
QUOTE(Xing @ 24 Sep, 2006 - 06:32 PM) *

@nirali35

Where is your main function?





Welcome to the 2D Distance Program!!!

Where is your first point? 3.4 12.2
Where is your second point? 13.4 12.2

Thank you!! Calculating... Done.

(3.4, 12.2) is 10 units away from (13.4, 12.2).

Thank you for using the TDP!!

Endeavor to have a wonderous day!



hey do u know how to make this program if u do plz help
my email is mk_2302@yahoo.co.in

thanks
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Need Some Help/advise For Public/Private Classes
27 Sep, 2006 - 07:54 AM
Post #8

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
QUOTE(mk_2302 @ 27 Sep, 2006 - 09:03 PM) *

QUOTE(Xing @ 24 Sep, 2006 - 06:32 PM) *

@nirali35

Where is your main function?





Welcome to the 2D Distance Program!!!

Where is your first point? 3.4 12.2
Where is your second point? 13.4 12.2

Thank you!! Calculating... Done.

(3.4, 12.2) is 10 units away from (13.4, 12.2).

Thank you for using the TDP!!

Endeavor to have a wonderous day!



hey do u know how to make this program if u do plz help
my email is mk_2302@yahoo.co.in

thanks

The program you want isn't very difficult to code. Try it yourself. Show us your code. We'll help you if you have a problem.
If you don't know the formula is, check here.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 01:42AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month