C++ cannot call member function w/o object

Hi, I am getting a compilation error saying "cannot call member function ‘void Point::PointType(float, float)’ without object." I've tried adjusting the constructor a few times. But to no avail. I am also stuck on what should I add into the member function of PointType. Would appreciate anyone's help on this. Thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

struct PointType
{
	float x;
	float y;
	PointType(const float x1, const float y1) :x(x1),y(y1){}
};
class Object
{
	private:
	float d;
	PointType * pt;
	public:
	Object(float n) : d(n){}
	 
     float depth() const
	{
		return d;
	}
};
class Point :public Object
{
	private:
	PointType mpoint;
	 
	public:
	
	Point(const PointType& pt, float& y1) : mpoint(pt), Object(y1) {}
	
 	virtual ~Point();

        // PointType PointType(float x, float y)
	// {
		 // return mpoint;
	// }
 };

const float EPSILON = 1e-5f;

bool is_near(float x, float y)
{
	return std::abs(x - y) < EPSILON;
}
float frand()
{
	return 10.0f * float(rand()) / float(RAND_MAX);
}
int main()
{

	srand(unsigned(time(0)));
	int count = 0;
	int max_count = 0;

	float x = frand();
	float y = frand();
	float depth = frand();

	Point point(Point::PointType(x, y), depth);
        if (is_near(point.depth(), depth))
	{
		++count;
	}
	else
	{
		std::cout << "  - Point::depth test failed" << std::endl;
	}
	++max_count;


}

	
Last edited on
The PointType() method is non-static which means you can't just call it in isolation - you have to call it on an object of type Point.

Also, that method doesn't return anything. Your attempt to pass its return value into the constructor on line 43 makes no sense, because it doesn't have a return value.
Last edited on
At line 43 you have:
Point point(Point::PointType(x, y), depth);
So you're calling Point::PointType(x,y) That function is declared at line 24 as a member of class Point that returns void. Since it's a member, it must be called with an object. After all, if you could call it with no object, then what would this point it?

Also, Point::PointType returns void, so line 43 is trying to call:
Point::Point(void, float)
and there is no such constructor.

What is the code supposed to do? I think you may have the classes setup wrong.
you had a few problems -- including calling point constructor with an extra parameter
this compiles and should serve as a new starting point to get it working from here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

struct PointType
{
	float x;
	float y;
	PointType(const float x1, const float y1) :x(x1),y(y1){}
};

class Point 
{
	private:
	PointType mpoint;
	 
	public:
	
	Point(const PointType& pt) : mpoint(pt) {}
	
 //	virtual ~Point();
	

 };
 
float frand()
{
	return 10.0f * float(rand()) / float(RAND_MAX);
}

int main()
{

	srand(unsigned(time(0)));
	int count = 0;
	int max_count = 0;

	float x = frand();
	float y = frand();
	float depth = frand();
 
	Point pt(PointType(x, y));
}

	
Last edited on
Thanks for you help guys. But it still does not work. I'm still facing the same issue of "cannot call member function ‘void Point::PointType(float, float)’ without object." Below is the updated changed I made. Need more guidance on this please. And @jonnin, I'm not suppose to tweak anything in the main. Sorry for not letting you know earlier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Point 
{
	private:
	PointType mpoint;
 	float y;
	
	public:
	
	Point(const PointType& pt, float& y1) : mpoint(pt), y(y1) {}
	
 	virtual ~Point();
	
	PointType PointType(float x, float y)
	{
		 return mpoint;
	}

	
};
OK, so that's the redefinition of your PointType() method to actually return a value.

Have you addressed the following issue?

MikeyBoy wrote:
The PointType() method is non-static which means you can't just call it in isolation - you have to call it on an object of type Point.


dhayden wrote:
At line 43 you have:
Point point(Point::PointType(x, y), depth);
So you're calling Point::PointType(x,y) That function is declared at line 24 as a member of class Point that returns void. Since it's a member, it must be called with an object. After all, if you could call it with no object, then what would this point it?
Last edited on
Hi @MikeyBoy. Thanks for the response. I have actually been trying to fix my constructor as well the past hour. But somehow, I am not able to resolve it. This is a little complex for my level I think. I tried adding a float variable inside the parameter to match the code in the main. But it still doesn't work. I've ran out of ways. Can I know what is wrong now?
This has nothing to do with your constructor. This has to do with how you're calling your Point::PointType() method. You can't just call it as if it were a standalone function. It's a method of the Point class, so ypou have to call it on an existing Point object.

If you don't understand how to call methods of a class, the following tutorial should be of help:

http://www.cplusplus.com/doc/tutorial/classes/
Mikey, should he not be able to call it via a temporary, hidden object, as he is doing?
Point::PointType(x, y) should construct an object...

if you can't change main, you have to add a float do that constructor so it can accept it. its missing.
Last edited on
@jonnin You can't call a non-static member function like that! It doesn't matter what the function returns. Calling Point::PointType() doesn't magically create an unnamed temporary Point object. Note that Point and PointType are different types - line 43 is not invoking the PointType constructor.

@playpro10, can you clarify what your intention is with line 43? Is the first argument you pass into the Point constructor supposed to be a temporary PointType object returned by the PointType constructor?

Since you apparently have constraints on how you write this code, it would be helpful if you just posted the entire text of the assignment. Otherwise, people are just wasting time suggesting solutions that you then reveal aren't any good to you.
Last edited on
Hi, I have updated the necessary code as above. Hope this fills in more information. I'm sorry for wasting you guys time. Will post better the next time. Thanks!
Right, the direct call is wrong. but cleaning up the syntax (which I did above as well), this would work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class p
{public:
  int x;
  p(int i){x = i;}
};

class q
{public:
  q(p tp){cout<<tp.x<<endl;}
};

int main()
{
q jj(p(3));  //prints 3.  I thought this is what he was trying to do.
//q qq(p::p(5)); //fails, cannot call constructor this way. this is what he did sort of. 
}
Last edited on
You still haven't explained what this code is trying to do. It's very hard to give good advice without knowing. It's like asking for for instructions on driving when we don't know if you're driving a car, a motorcycle or an 18-wheeled truck.

I'm not suppose to tweak anything in the main.

One interpretation of the code that compiles is below. The key difference is that I've made PointType and struct within class Point, so
Point point(Point::PointType(x,y), depth)
is calling the constructor for Point::PointType instead of calling a member function.

But this is code is still a mess:
- class object serves no purpose. It's just a tortuous way of wrapping float d;
- Class PointType doesn't serve a purpose, at least not for this code. You could more easily put members x and y into class Point.

So once again: what are you trying to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

class Object
{
private:
    float d;			// depth
  public:
    Object(float n):d(n)
    {
    }

    float depth() const
    {
	return d;
    }
};

class Point:public Object
{
public:
    struct PointType
    {
	float x;
	float y;
	PointType(const float x1, const float y1):x(x1), y(y1)
	{}
    };

    Point(const PointType & pt, float &y1):Object(y1), mpoint(pt)
    {
    }

    virtual ~ Point() {};
private:
    PointType mpoint;

};

const float EPSILON = 1e-5f;

bool
is_near(float x, float y)
{
    return std::abs(x - y) < EPSILON;
}

float
frand()
{
    return 10.0f * float (rand()) / float (RAND_MAX);
}

int
main()
{

    srand(unsigned (time(0)));
    int count = 0;
    int max_count = 0;

    float x = frand();
    float y = frand();
    float depth = frand();

    Point point(Point::PointType(x, y), depth);
    if (is_near(point.depth(), depth)) {
	++count;
    } else {
	std::cout << "  - Point::depth test failed" << std::endl;
    }
    ++max_count;

}
Topic archived. No new replies allowed.