Newbie: How to access class members?

J

James Egan

In the example below, I'm trying to call the
GetInput::GetAddress() function in main()
which is a function in the class "GetInput".
What am I missing? I thought all I had to do
was instantiate the class with something like
"GetInput gi" then use gi.GetAddress().

-Thanks

#include <fstream>
#include <iostream>
#include <string>

using namespace std;


class GetInput {

public:
GetInput();
~GetInput();

void GetInput::GetAddress() {

//Do something here

}
};



int main()
{
GetInput gi;
//GetInput.GetAddress();

//gi.GetAddress();

return 0;
}
 
L

Lionel

James said:
In the example below, I'm trying to call the
GetInput::GetAddress() function in main()
which is a function in the class "GetInput".
What am I missing? I thought all I had to do
was instantiate the class with something like
"GetInput gi" then use gi.GetAddress().

-Thanks

#include <fstream>
#include <iostream>
#include <string>

using namespace std;


class GetInput {

public:
GetInput();
~GetInput();

void GetInput::GetAddress() {

//Do something here

}
};



int main()
{
GetInput gi;
//GetInput.GetAddress();

//gi.GetAddress();

return 0;
}

Try GetInput gi(); Note the brackets which calls the constructor.


int main()
{
GetInput gi();
//GetInput.GetAddress();

//gi.GetAddress();

return 0;
}

Lionel.
 
S

Sharad Kala

James Egan said:
In the example below, I'm trying to call the
GetInput::GetAddress() function in main()
which is a function in the class "GetInput".
What am I missing? I thought all I had to do
was instantiate the class with something like
"GetInput gi" then use gi.GetAddress().

Always paste the error you are getting.
#include <fstream>
#include <iostream>
#include <string>

using namespace std;


class GetInput {

public:
GetInput();
~GetInput();

void GetInput::GetAddress() {

//Do something here

}
};



int main()
{
GetInput gi;
//GetInput.GetAddress();

The above line is incorrect. GetInput is a type and not an object.
//gi.GetAddress();

This is correct. gi is an instance of GetInput class.
return 0;
}

You have provided no definition for the constructor and the destructor in
GetInput class. Compiler will not generate it for you since you have
declared them in the class definition. Do you get linker errors ?

Sharad
 
G

Gary Labowitz

James Egan said:
In the example below, I'm trying to call the
GetInput::GetAddress() function in main()
which is a function in the class "GetInput".
What am I missing? I thought all I had to do
was instantiate the class with something like
"GetInput gi" then use gi.GetAddress().

-Thanks

#include <fstream>
#include <iostream>
#include <string>

using namespace std;


class GetInput {

public:
GetInput();
~GetInput();

Here you have declarations of the constructor and destructor. You don't need
these. If you wish to specify them, you need to provide the definition, even
if you do nothing.
GetInput(){ };
~GetInput(){ };
But if you leave them out, the compiler will provide exactly that for you.
void GetInput::GetAddress() {

//Do something here

Well, then, do something. How about

cout << "Got an address\n";
}
};



int main()
{
GetInput gi;

This is the correct way to instantiate an object of type GetInput. Do not do
GetInput gi( ); which is a declaration of a function named gi taking no
parameters and returning a GetInput object.
//GetInput.GetAddress();
Wrong.


//gi.GetAddress();

Right! Take off the comment specifiers.
return 0;
}
Good luck.
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Lionel said:
Try GetInput gi(); Note the brackets which calls the constructor.

Wrong!
That declares gi to be a function returning a GetInput by value.

Stefan
 
J

Jan Eickmann

James said:
...

class GetInput {

public:
GetInput();
~GetInput();

void GetAddress() { //removed GetInput::

//Do something here

}
};
...

As long as you define the method inside the class body, remove the
ClassName:: stuff.
You only need it, if you define the method outside.

Jan Eickmann
 
J

javacoder187

James said:
In the example below, I'm trying to call the
GetInput::GetAddress() function in main()
which is a function in the class "GetInput".
What am I missing? I thought all I had to do
was instantiate the class with something like
"GetInput gi" then use gi.GetAddress().

-Thanks

#include <fstream>
#include <iostream>
#include <string>

using namespace std;


class GetInput {

public:
GetInput();
~GetInput();

void GetInput::GetAddress() {

//Do something here

}
};



int main()
{
GetInput gi;
//GetInput.GetAddress();

//gi.GetAddress();

return 0;
}

You shouldn't have gi.GetAddress(); commented out. It should be like
int main()
{
GetInput gi;
//GetInput.GetAddress();

gi.GetAddress();

return 0;
}
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top