Fred
Fred

Reputation: 75

What does '->' mean when trying to forward-declare?

I am just starting to try out C++. I keep running into this error when trying to forward-declare this class from another file: request for member 'get_posx' in 'girl', which is of pointer type 'Vex*' (maybe you meant to use '->'?)

These are the two files I have included in my Qt-Creator Plain C++ Project:

main.cpp:

#include <iostream>
using namespace std;

class Vex; //Declares Vex class

class Obj //Object class
{
int x, y;
public:
void set_pos (int,int);
void move (int, int);
int get_posx() {return x;}
int get_posy() {return y;}
};

void Obj::set_pos (int pos_x, int pos_y) //Sets X and Y of Obj
{
x = pos_x;
y = pos_y;
}
void Obj::move (int pos_x, int pos_y) //Changes X and Y of Obj
{
x += pos_x;
y += pos_y;
}

int main ()
{
Obj guy; //Guy as Obj class
Vex* girl; //Girl as (imported)Vex class
guy.set_pos (0,0);
while(true == true)
{
int tempx, tempy;
cout << girl.get_posx(); //Prints x pos. of girl
cout << "\nX Position: " << guy.get_posx() <<endl; //Prints x pos. of guy
cout << "Y Position: " << guy.get_posy() <<endl; //Prints y pos. of guy
cout << "\nX Change:" << endl;
cin >> tempx; //Asks for x change in guy pos.
cout << "Y Change:" << endl;
cin >> tempy; //Asks for y change in guy pos.
guy.move (tempx, tempy);
}
return 0;
}

s.cpp:

class Vex
{
int x, y;
public:
void exp();
int get_pos() {return x;}
};

void Vex::exp()
{
x = 0;
y = 0;
}

What am I doing wrong here, and what is '->', how do I use it?

Upvotes: 1

Views: 161

Answers (2)

L&#225;szl&#243; Papp
L&#225;szl&#243; Papp

Reputation: 53145

I think you would be better off learning C++ first as you seem to lack the basics; no offense meant, just trying to help with it. You have several misconceptions in your mind as well as implementation issues. I do not even know where to start. Let us see:

  • Difference between heap and stack allocated objects (aka. pointer vs. non-pointer).

    Vex* girl; //Girl as (imported)Vex class
    ...
    cout << girl.get_posx(); //Prints x pos. of girl
    

    Pointer object members are accessed via -> in C++, so you ought to modify your code to this:

    cout << girl->get_posx(); //Prints x pos. of girl
    //          ^^
    
  • When you can use forward declaration.

    class Vex; //Declares Vex class
    

    This is not enough when you would like to access the class members like that. You will need to include the class definition through an include directive, something like this:

    #include "vex.h"
    
  • Declaring classes like that in source files (aka. cpp)

    This is usually wrong, although not ultimately for sure. If you do it like this, you will not be able to easily reuse it without including source files, which is not really recommended.

  • Strictly speaking, there is no such a thing as "import" in C++.

    Vex* girl; //Girl as (imported)Vex class
    //                    ^^^^^^^^
    

    Import may be found as a term in other programming languages such as python, but those work differently to includes.

  • Overcommenting

    Not only is that comment somewhat incomprehensive, but even pointless. Strive for self-documenting code.

  • You think it is in any way Qt related.

    It is not, even if you tag it so. It is generic (and basic) C++.

  • Not using const for methods not changing members.

    This is good practice to do for things like getting the members in your code, namely:

    int get_posx() const {return x;}
    //             ^^^^^
    int get_posy() const {return y;}
    //             ^^^^^
    

    ... and similarly:

    int get_pos() const {return x;}
    //            ^^^^^
    
  • How to write a blocking forever loop.

     while(true == true)
    

    While that works, that is nonsensical.

    `while(1)`
    

    or simply

    `forever()`
    

    in Qt would be more elegant.

  • Inconsistent coding style (e.g. space usage)

    void set_pos (int,int);
    //          ^     ^
    void move (int, int);
    //       ^     ^
    int get_posx() {return x;}
    //          ^
    
  • Not using indentation

    Your source code seems to be left aligned, but this is not a word document. While, C and C++ use parenthesis and so it will work unlike e.g. in Python, it is difficult to comprehend.

  • You do not seem to understand what constructors are.

    Obj guy; //Guy as Obj class
    ...
    guy.set_pos (0,0);
    

    In C++, you establish a constructor or at the very least "init" methods for such initialization. Strictly speaking, with C++11 on, you can even avoid that for this, but the point is that you do not initialize members through setting methods.

Upvotes: 3

Dmitry
Dmitry

Reputation: 2139

girl is declared as a pointer to an object Vex:

Vex* girl

Because of that you need to use -> operator instead of . to address the girl object members:

girl->get_posx();

also, because it is pointer, you need to initialise it with proper object:

Vex* girl = new Vex();

Other ways you will get an error.

Upvotes: 0

Related Questions