help with THIS [ Newbie ]

S

sieg1974

Hi,

the methods of the class Formater are defined like this

class Formater
{
public:
StringFormarter();
~StringFormarter();
int setLink( char * newLink );
char * getReference();
private:
char theLink[ 128 ];
};

and the method getDirectoryName is the following one

char * StringFormarter::getDirectoryName()
{
char theDirectoryName[ 128 ];
...
theHostName = this.getHostName();
...
}

Could someone tell me what is wrong with "theHostName =
this.getHostName();", and what is an aggregative type? When I compile
it, the compiler ( gcc ) shows the following error message

request for member getHostName in this, which is of non-aggregate type
Formater*

Thanks a lot,

Andre
 
J

Jonathan Turkanis

sieg1974 said:
the methods of the class Formater are defined like this

class Formater
{
public:
StringFormarter();
~StringFormarter();
int setLink( char * newLink );
char * getReference();
private:
char theLink[ 128 ];
};

The class name must match the constructors and destructor. Also, as a
beginner you should learn to use standard library containers (strings,
vectors, etc.) instear of C-style strings and built-in arrays wherever
possible.
and the method getDirectoryName is the following one

char * StringFormarter::getDirectoryName()
{
char theDirectoryName[ 128 ];
...
theHostName = this.getHostName();
...
}

The main problem is that 'this' is a pointer and you have accessed it
as if it were a reference. Try 'this->getHostName()'
 
O

osmium

sieg1974 said:
what is an aggregative type?

Something that contains an aggregation of fundamental types. Arrays,
structures and classes are the most common aggregate types.
 
H

Howard

Jonathan Turkanis said:
char * StringFormarter::getDirectoryName()
{
char theDirectoryName[ 128 ];
...
theHostName = this.getHostName();
...
}

The main problem is that 'this' is a pointer and you have accessed it
as if it were a reference. Try 'this->getHostName()'

Or, leave out the "this" specification entirely! By default, the compiler
will look in the current class scope for getHostName before it goes
elsewhere to find it. It is pretty rare that you need to use "this->"
preceding function calls or access to member data. In fact, I think the
*only* time you need that is when writing a template class, but I forget the
reason or condition where it's needed then. (Of course, some people just
like to use "this->" as a kind of documentation that it's a member of the
current class and not some global function or data. Saves looking around
for where it's defined, I guess, although my IDE will take me there via a
right-click menu item.)

-Howard
 
J

Jonathan Turkanis

Howard said:
Or, leave out the "this" specification entirely! By default, the compiler
will look in the current class scope for getHostName before it goes
elsewhere to find it. It is pretty rare that you need to use "this->"
preceding function calls or access to member data. In fact, I think the
*only* time you need that is when writing a template class, but I forget the
reason or condition where it's needed then. (Of course, some people just
like to use "this->" as a kind of documentation that it's a member of the
current class and not some global function or data. Saves looking around
for where it's defined, I guess, although my IDE will take me there via a
right-click menu item.)

-Howard

Sure, I usually leave of 'this', unless I am initializing a member
with a variable of the same name. With templates, qualifying
non-static members from dependent base classes with 'this' will force
the compiler to delay lookup until instantiation, preventing some
compilation errors.

At any rate, I just wanted to call attention to the fact that in C++
'this' is a pointer.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top