Whats wrong here? object not a member of class?

I am trying to re-freshen up on C++, i been using Java and Python lately.

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

#include <iostream>
using namespace std;

class animal {
    private:
        int *speed; 
        double *position_x;
        double *position_y; 

    public:
        animal();
        animal(int speed,double time ,double y);
        ~animal();
        void print();
        int getspeed();
        int getx();
        int gety();
};

animal::animal() {
    this->speed = new int;
    this->position_x=new double;
    this->position_y= new double;

    *(this->speed) = 0;
    *(this->position_x)=0;
    *(this->position_y)= 0;

}

animal::animal(int v, double x,double y){
    this->speed = new int;
    this->position_x=new double;
    this->position_y= new double;

    *speed=v;
    *position_x=x;
    *position_y=y;
}

animal::~animal(){
    delete position_x,position_y,speed;
}


int animal::getspeed() {
    return *speed;
}

void animal::print()
{
    cout<<"speed: "<<*(this->speed)<<endl;
    cout<<"position_x: "<<*(this->position_x)<<endl;
    cout<<"position_y: "<<*(this->position_y)<<endl;
}

int animal::getx() {return *position_x;}
int animal::gety() {return *position_y;}


int main()
{
    animal fish(4,3,4);
    animal cat();
    fish.print();
    cat.print();
    
    return 0;
}


when i run it i get compile time error:

1
2
|68|error: request for member ‘print’ in ‘cat’, which is of non-class type ‘animal()’|
||=== Build finished: 1 errors, 0 warnings ===|


I am not so sure whats the problem with this, any help?
i realised that it should be


1
2
3
4
5
animal cat;

// i still don't get why this won't work

animal cat();
This happens because animal cat(); parses as a function declaration of a function cat that returns an animal object and takes no parameters.

Also, dynamic allocation is highly inappropriate here (to avoid the word "wrong"). You shouldn't use new unless necessary.

And this: delete position_x,position_y,speed; won't do what you expect, it just deletes position_x.
Look up the comma operator for more information.
Last edited on
Athar gave you a good explanation, but I will elaborate on the pointers and delete

Don't use pointers in this case. You really, really don't need them; just using the primitive types themselves will suffice. By using pointers, the compiler-give constructor will just copy the pointers and not what they point to. What do you think will happen for cat = fish; ?

As for the delete, in C++ the comma operator evaluates each opearand and then returns the last:
int x = SomeFunc(), SomeOtherFunc, 0, 42;
SomeFunc() is called first, then SomeOtherFunc(), and x receives a value of 42.

For such a small class, you don't need to externally define the functions; you can define them inline like in Java and Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Animal
{
    double x, y, speed;
public:
    Animal() : x(0.0), y(0.0), speed(0.0) {}
    Animal(double X, double Y, double Speed) : x(X), y(Y), speed(Speed) {}

    void Print()
    {
        cout << "X=" << x << endl;
        cout << "Y=" << y << endl;
        cout << "Speed=" << speed << endl;
    }
    //etc...
};


Though for large classes, the class definition is usually in a header (*.h, *.hpp) and the implementation in a source file (*.cc, *.cpp, *.cxx). And even then, you don't have to explicitly say this->SomeVar, as SomeVar will already be in scope.
Last edited on
is there a way to get the current name of the class and print it.

like

1
2
3
4
5
6

animal::print() {
   cout <<" the animals name is: "<< //how can i access its name? (assuming i don't have  a private field which asks for the
             //name   when  the object is created
}
Well, you can get a name with typeid(*this).name(). But what you get as a result depends on the compiler.
also why can't this work for copy constructor

1
2
3
4
5
6
7
8

animal::animal(const animal &g){
    this->speed=g.getspeed();
    this->position_x=g.getx();
    this->position_y=g.gety();

}


the program thinks i am trying to modify g, even though i am just trying to use a public function on it.

anyone know whats the problem with this?
It is because getspeed() is not marked as a const member function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class MyClass
{
    int var;
public:
    MyClass() : var(1) {}
    int variable() const //note the const: guaranteed not to change the state of this object
    {
        return var;
    }
    void variable(const int &v) //note the lack of const: may or may not change the sate
    {
        if(v > 0)
        {
            var = v;
        }
    }
};

19
20
21
22
23
24
MyClass a;
const MyClass b;
a.variable(7); //legal: a is not const
cout << a.variable(); //legal: will always be legal whether a is const or not
b.variable(-4); //illegal: b is const and variable(const int &) is not
cout << b.variable(); //legal: b is const and variable() is too 
Last edited on
thank you so much, i understand now.

I am learning better now than when i was taught in class
Topic archived. No new replies allowed.