Help with Cartesian class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Help with Cartesian class

  1. #1
    Join Date
    Apr 2014
    Posts
    5

    Help with Cartesian class

    I am trying to make a program with a Cartesian class that allows the user to enter 2 coordinates and displays these coordinates. When I try to compile it, I get a message saying the x and y in x=c and y=d don't name a type. How can I fix this? Also, how would I go about inserting an assignment function that assigns the values of coord1 to coord2?
    Code:
    #include <iostream>
    #include <istream>
    #include <ostream>
    
    using namespace std;
    
    class Cartesian
    {
    private:
    double x;
    double y;
    public:
    Cartesian( double= 0, double= 0);
    friend istream& operator>>(istream&, Cartesian&);
    friend ostream& operator<<(ostream&, const Cartesian&);
    double c;
    double d;
    x=c;
    y=d;
    
    };
    
    Cartesian::Cartesian(double a, double b)
    {
        x=a;
        y=b;
    }
    
    istream& operator>>( istream& in, Cartesian& num)
    {
    	in >> num.x;
    	in >> num.y;
        
    
    return in;
    }
    
    ostream& operator<<( ostream& out, const Cartesian& num)
    {
    	cout << "(" << num.x << ", " << num.y << ")" << endl;
    
    return out;
    }
    
    int main()
    {
    	Cartesian coord1, coord2;
        cout << "Please enter the first x-coordinate: ";
    	cin >> coord1.c;
    	cout << "Please enter the first y-coordinate: ";
    	cin >> coord1.d;
    	 cout << "Please enter the second x-coordinate: ";
    	cin >> coord2.c;
    	cout << "Please enter the second y-coordinate: ";
    	cin >> coord2.d;
    	
    
    	cout << coord1;
    	cout << coord2;
    
    	
    	
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: Help with Cartesian class

    Code:
    double c;
    double d;
    x=c;
    y=d;
    Just remove these lines as c and d aren't used. You can't do an assignment when the compiler expects a definition/declaration.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  3. #3
    Join Date
    Apr 2014
    Posts
    5

    Re: Help with Cartesian class

    Okay so I did that, but now I get these errors
    prog.cpp: In function ‘int main()’:
    prog.cpp:45:16: error: ‘class Cartesian’ has no member named ‘c’
    cin >> coord1.c;
    ^
    prog.cpp:47:16: error: ‘class Cartesian’ has no member named ‘d’
    cin >> coord1.d;
    ^
    prog.cpp:49:16: error: ‘class Cartesian’ has no member named ‘c’
    cin >> coord2.c;
    ^
    prog.cpp:51:16: error: ‘class Cartesian’ has no member named ‘d’
    cin >> coord2.d;
    ^

    Here is my current code
    Code:
    #include <iostream>
    #include <istream>
    #include <ostream>
    
    using namespace std;
    
    class Cartesian
    {
    private:
    double x;
    double y;
    public:
    Cartesian( double= 0, double= 0);
    friend istream& operator>>(istream&, Cartesian&);
    friend ostream& operator<<(ostream&, const Cartesian&);
    
    };
    
    Cartesian::Cartesian(double a, double b)
    {
        x=a;
        y=b;
    }
    
    istream& operator>>( istream& in, Cartesian& num)
    {
    	in >> num.x;
    	in >> num.y;
        
    
    return in;
    }
    
    ostream& operator<<( ostream& out, const Cartesian& num)
    {
    	cout << "(" << num.x << ", " << num.y << ")" << endl;
    
    return out;
    }
    
    int main()
    {
    	Cartesian coord1, coord2;
        cout << "Please enter the first x-coordinate: ";
    	cin >> coord1.c;
    	cout << "Please enter the first y-coordinate: ";
    	cin >> coord1.d;
    	 cout << "Please enter the second x-coordinate: ";
    	cin >> coord2.c;
    	cout << "Please enter the second y-coordinate: ";
    	cin >> coord2.d;
    	
    
    	cout << coord1;
    	cout << coord2;
    
    	
    	
    	return 0;
    }

  4. #4
    Join Date
    Apr 2014
    Posts
    5

    Re: Help with Cartesian class

    Okay so I made some more changes to my code and it runs now but it doesn't allow the user to enter coordinates. What am I doing wrong?
    Code:
    #include <iostream>
    #include <istream>
    #include <ostream>
    
    using namespace std;
    
    class Cartesian
    {
    private:
    double x;
    double y;
    public:
    Cartesian( double= 0, double= 0);
    friend istream& operator>>(istream&, Cartesian&);
    friend ostream& operator<<(ostream&, const Cartesian&);
    }
    ;
    
    Cartesian::Cartesian(double a, double b)
    {
        x=a;
        y=b;
    }
    
    istream& operator>>( istream& in, Cartesian& num)
    {
    	in >> num.x;
    	in >> num.y;
        
    
    return in;
    }
    
    ostream& operator<<( ostream& out, const Cartesian& num)
    {
    	cout << "(" << num.x << ", " << num.y << ")" << endl;
    
    return out;
    }
    
    int main()
    {
    	double x1, x2, y1, y2;
    	Cartesian coord1(x1, y1), coord2(x2, y2);
    	cout << "Please enter the first coordinates: ";
    	cin >> coord1;
    	cout << "Please enter the second coordinates: ";
    	cin >> coord2;
    	cout << coord1;
    	cout << coord2;
    
    	
    	
    	return 0;
    }

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,998

    Re: Help with Cartesian class

    Well it's time to learn the debugger and use it to know what where and why in your code goes wrong.
    Victor Nijegorodov

  6. #6
    Join Date
    Apr 2014
    Posts
    5

    Re: Help with Cartesian class

    What type of debugger are we talking about?
    Last edited by cootoncandyliz; April 6th, 2014 at 04:45 PM.

  7. #7
    Join Date
    Apr 2014
    Posts
    5

    Re: Help with Cartesian class

    Also, what is the syntax for writing a memberwise assignment function within a class?

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: Help with Cartesian class

    You are trying to make things more complicated than they are.

    Code:
    #include <iostream>
    using namespace std;
    
    class Cartesian
    {
    private:
    	double x;
    	double y;
    
    public:
    	Cartesian( double a = 0, double b = 0) : x(a), y(b){}
    
    	friend istream& operator>>(istream&, Cartesian&);
    	friend ostream& operator<<(ostream&, const Cartesian&);
    };
    
    istream& operator>>(istream& in, Cartesian& num)
    {
    	cin >> num.x >> num.y;
    	return in;
    }
    
    ostream& operator<<( ostream& out, const Cartesian& num)
    {
    	cout << "(" << num.x << ", " << num.y << ")" << endl;
    	return out;
    }
    
    int main()
    {
    Cartesian	coord1,
    		coord2,
    		coord3;
    
    	cout << "Please enter the first coordinates: ";
    	cin >> coord1;
    
    	cout << "Please enter the second coordinates: ";
    	cin >> coord2;
    
    	coord3 = coord1;
    
    	cout << coord1;
    	cout << coord2;
    	cout << coord3;
    	
    	return 0;
    }
    Last edited by 2kaud; April 6th, 2014 at 05:19 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: Help with Cartesian class

    The declaration for an addition function for the class would be
    Code:
    Cartesian operator+(const Cartesian& rh);
    This would allow two classes of type Cartesian to be added. ie
    Code:
         coord3 = coord1 + coord2;
    - although the body of the definition will need to be written!

    This overloads the operator + to be defined for the class Cartesian. Similary, you could overload the operators +=, -=, -, * etc to perform these operations for the Cartesian class.
    Last edited by 2kaud; April 6th, 2014 at 05:23 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: Help with Cartesian class

    Quote Originally Posted by cootoncandyliz View Post
    Also, what is the syntax for writing a memberwise assignment function within a class?
    With this class as it currently is, you don't need to provide either an assignment or a copy constructor as the default ones provided by the compiler are sufficient because no dynamic storage is involved.

    See
    http://www.learncpp.com/cpp-tutorial...r-overloading/
    http://www.cplusplus.com/doc/tutorial/templates/
    http://www.tutorialspoint.com/cplusp...verloading.htm
    Last edited by 2kaud; April 6th, 2014 at 05:40 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)