CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2013
    Posts
    3

    C## Class Problems

    I need some help on this ASAP as the program is due at 11:00pm.

    I have one error left, but I have a feeling once it's fixed I'm going to have several more. That would be my luck.

    The error is the title for this problem: declaration of 'OPairType::OPairType(int, int)' outside of class is not definition -fpermissive

    Here is my full code:

    Header File:

    class OPairType{
    private:
    int x;
    int y;
    public:
    OPairType (int=0, int=0);
    int getX() const;
    int getY() const;
    void setX(int);
    void setY(int);
    void setValues(int, int);
    friend OPairType operator + (OPairType, OPairType);
    friend OPairType operator - (OPairType, OPairType);
    friend bool operator == (OPairType, OPairType);
    friend bool operator != (OPairType, OPairType);
    friend std:stream& operator << (std:stream&, OPairType);
    And here is the .cpp code:

    #include "OPairType.h"
    #include <iostream>

    OPairType::OPairType (int x, int y); //error occurs here

    int OPairType::getX() const {
    return x;
    }

    int OPairType::getY() const {
    return y;
    }

    void OPairType::setX(int new_x) {
    x = new_x;
    }

    void OPairType::setY(int new_y) {
    y = new_y;
    }

    void OPairType::setValues (int new_x, int new_y){
    x = new_x;
    y = new_y;
    }

    OPairType operator + (OPairType lh, OPairType rh){
    OPairType answer;

    answer.x = lh.x + rh.x;
    answer.y = lh.y + rh.y;

    return answer;
    }

    OPairType operator - (OPairType lh, OPairType rh){
    OPairType answer;

    answer.x = lh.x - rh.x;
    answer.y = lh.y - rh.y;

    return answer;
    }

    bool operator == (OPairType lh, OPairType rh){
    return lh.x == rh.x && lh.y == rh.y;
    }

    bool operator != (OPairType lh, OPairType rh){
    return !(lh.x == rh.x && lh.y == rh.y);
    }

    std:stream& operator << (std:stream& out, OPairType c){
    out << "(" << c.x << ", " << c.y << ")";
    return out;
    }
    Any responses at all will be appreciated. Just be fast is all I ask

  2. #2
    Join Date
    Nov 2013
    Posts
    3

    Re: C## Class Problems

    sorry about the smileys, first time posting. They are supposed to be double colons "::"

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C## Class Problems

    It's just what the compiler says. I guess you meant to implement the constructor, but the way it's written, with the ; and no body, it looks more like a definition, and it's outside of the class definition. You have it declared inside the class. You need to provide the implementation outside.

    OPairType::OPairType (int x, int y);

  4. #4
    Join Date
    Nov 2013
    Posts
    3

    Re: C## Class Problems

    would this work?

    OPairType::OPairType (int x, int y) {
    x=y=0;
    }

    Edit: I've got it to compile now using the above code. Now my output is coming out wrong. I'm leaving c's ordered pair as default(0, 0) but when I use cout<<c<<endl it outputs (-2, 1991577954).

    Any Ideas??
    Last edited by ldicus34; November 7th, 2013 at 10:02 PM.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: C## Class Problems

    Quote Originally Posted by ldicus34 View Post
    sorry about the smileys, first time posting. They are supposed to be double colons "::"
    It is because you didn't use Code tags. Besides the "smileys" problem your code is very hard to read/understand.
    Please, read the Announcement: section Information on posting
    Victor Nijegorodov

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

    Re: C## Class Problems

    Before posting, please format your code properly and use code tags. Go Advanced, select the code and click '#'.

    Code:
    OPairType::OPairType (int x, int y); //error occurs here
    You are trying to define a constructor with two parameters. This is a forward declaration and not a definiton. Also it isn't good practice to use paramater variables which have the same names as class variables.

    I suspect the required constructor definition is
    Code:
    OPairType::OPairType (int px, int py) : x(px), y(py) {}
    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++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C## Class Problems

    Quote Originally Posted by ldicus34 View Post
    would this work?

    OPairType::OPairType (int x, int y) {
    x=y=0;
    }
    That line sets temporary variables x and y to 0, and does nothing else. So effectively, that constructor accomplishes nothing, even though you were able to get it to compile.
    Any Ideas??
    If you posted your entire program, including the main() function, then maybe we have "ideas". However you're the one that should be responsible for debugging your own programs -- leaving it to a forum full of experienced and professional programmers to do this work would be considered cheating.

    One thing:
    Code:
    bool operator == (OPairType lh, OPairType rh){
    return lh.x == rh.x && lh.y == rh.y;
    }
    
    bool operator != (OPairType lh, OPairType rh){
    return !(lh.x == rh.x && lh.y == rh.y);
    }
    First, those parameters should be const references. Second, the operator != could have been simply coded like this:
    Code:
    bool operator != (const OPairType& lh, const OPairType& rh)
    {
        return !(lh == rh);
    }
    This is logically what operator != is supposed to mean, and that is "not equal". Since "equal" is already coded, reuse it by just sticking a "not" in front of it.

    The reason to do things this way is that operator == is already coded. If operator == is later changed (made more complex for example), then operator != automatically works without any changes.

    Regards,

    Paul McKenzie

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
  •  





Click Here to Expand Forum to Full Width

Featured