2

I'm new with C++ and I'm currently studying for exams, messing around with C++ in VisualStudio and experimenting a bit. Usuall I work with Java.

I wrote a simple class to see how and if things work:

class Point
{
private:
    int x;
    int y;

public:
    Point(int arg1, int arg2)
    {
        x = arg1;
        y = arg2;
    }
};

I tried 2 simple member functions for x and y to just double the value stored in the x and y variables.

First I tried this:

void doubleX()
{
    x *= 2;
};

void doubleY()
{
    y *= 2;
};

Then I tried this:

void doubleX()
{
    Point::x = 2 * Point::x;
};

void doubleY()
{
    Point::y = 2 * Point2::y;
};

Both are put inside the class definition.

While building through VisualStudio it alwas gives me this error warning: "Error C3867 'Point::doubleX': non-standard syntax; use '&' to create a pointer to member"

Tried to mess around with adress pointers as well but... I don't really have a clue. I think I know how pointers basically work, but I have no idea how to use it for my case here.

Any quick solution and explanation to this problem?

Thanks in advance!

EDIT: here's my whole code, problem is in the main now

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

using namespace std;

class Point
{
public:
    int x;
    int y;

    Point(int arg1, int arg2)
    {
        x = arg1;
        y = arg2;
    }

    void doubleX()
    {
        x *= 2;
    };

    void doubleY()
    {
        y *= 2;
    };
};

int main()
{
    Point p(1,1);

    int &x = p.x;
    int &y = p.y;

    cout << x << "|" << y;

    p.doubleX; p.doubleY; //error message here

    cout << x << "|" << y;

    cin.get();
}
GenerationLost
  • 179
  • 1
  • 1
  • 15

4 Answers4

4

Maybe you didn't declare the member functions inside the class definition? Here is a full working example based on your class:

#include <iostream>


class Point
{
private:
    int x;
    int y;

public:
    Point(int arg1, int arg2)
    {
        x = arg1;
        y = arg2;
    }

    void doubleX()
    {
        x *= 2; /* or this->x *= 2; */
    }

    void doubleY()
    {
        y *= 2;
    }

    int getX()
    {
        return x;
    }

    int getY()
    {
        return y;
    }
};


int main()
{
    Point p(2, 3);

    std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl;

    p.doubleX();
    p.doubleY();

    std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl;

    return 0;
}

You can put this in a main.cpp file, compile and run it. I tested it with the g++ compiler and it works fine.

Valy
  • 573
  • 2
  • 12
  • Oh, now I see that the actual problem was in my main method, sorry guys, should've posted the whole code. – GenerationLost Jul 18 '17 at 15:09
  • `int main() { Point p(1,1); cout << p.x << "|" << p.y; p.doubleX; p.doubleY; cout << p.x << "|" << p.y; cin.get(); }` This is my main, members are public, so why can't I access them with "p.x"? – GenerationLost Jul 18 '17 at 15:10
  • That's pretty easy to spot :). The problem is not from accessing `p.x` and `p.y`. `doubleX` and `doubleY` are **functions**! So, you must call them like `p.doubleX(); p.doubleY();`. – Valy Jul 18 '17 at 15:35
  • And again referring to @CJ_macar who already gave me the hint. Thanks tho :) – GenerationLost Jul 18 '17 at 15:57
2

The answer given by Valy is correct. But I would like remind you that C++ offers you another choice of declaring and defining methods, that is declaring method inside the class declaration and defining them outside the class declaration. This enables you to easily separate interface and implementation into .h and .cpp files, respectively, as shown below:

Point.h

class Point
{
private:
    int x;
    int y;

public:
    Point(int arg1, int arg2);
    void doubleX();
    void doubleY();
    int getX();
    int getY();
};

Point.cpp

#include "Point.h"

Point::Point(int arg1, int arg2)
{
    x = arg1;
    y = arg2;
}

void Point::doubleX()
{
    x *= 2;
}

void Point::doubleY()
{
    y *= 2;
}

int Point::getX()
{
    return x;
}

int Point::getY()
{
    return y;
}

// PointTest.cpp

#include "Point.h"

int main()
{
    // Do something with Point here
    Point pt(1, 2);

    std::cout << "Original: (" << pt.getX() << ", " << pt.getY() << ")" << std::endl;

    pt.doubleX();
    pt.doubleY();

    std::cout << "After being doubled: (" << pt.getX() << ", " << pt.getY() << ")" << std::endl;

    return 0;
}

And, how to compile:

g++ -o PointTest PointTest.cpp Point.cpp
duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
  • Thanks for the advice! Getting into header usage and inline/makros will be the next topic I'll go through – GenerationLost Jul 18 '17 at 15:31
  • 1
    Yep, this is of course the better solution and I thought about writing it like this myself. I just wanted to give him a quick fix and not introduce too many new concepts. – Valy Jul 18 '17 at 15:41
1

Can't comment due to reputation but it seems vc++ outputs the error message you stated if you try to call

Point::doubleX

Here's a live example of the output: http://rextester.com/ZLCEW66682

You should create an instance of the class and call the function using parens

CJ_macar
  • 45
  • 8
-2

In your second set of functions

void doubleX()
{
    Point2::x = 2 * Point2::x;
};

void doubleY()
{
    Point2::y = 2 * Point2::y;
};

If you want them to be member functions of the class Point, Point::y ... this is not how you should access the member data. Only static member variables can be accessed like that. The correct way is

void doubleX()
{
    this->x = 2 * this->x;
};

void doubleY()
{
    this->y = 2 * this->y;
};

That is using this pointer.

AdityaG
  • 428
  • 1
  • 3
  • 17