i have here a code that uses pointers to manipulate data in a class...i have a problem of how to show them on screen...i think im using an erroneous syntax...lol...

#include <iostream>
#include <string>
using namespace std;

class MyClass{
        public:
                string first_name, last_name, id_no;
                int age;
};

int main(){

        MyClass s;
        MyClass* pS;

        pS = new MyClass;

        pS->first_name = "John Paul ";
        pS->last_name = "Dingle ";
        pS->id_no = "0321183 ";
        pS->age = 20;



        cout << s.first_name; << s.last_name << s.id_no << s.age << endl;

        delete pS;
        return 0;
}

you think im doing the right thing?? lol... help

Recommended Answers

All 6 Replies

delete line 13, it has no purpose.

change line 25 to use pointer pS

should i include the * before pS? after i changed 's' to 'pS' i still got an error:

ptr-struct.cpp:24: error: request for member `first_name' in `pS', which is of non-class type `MyClass*'
ptr-struct.cpp:24: error: request for member `last_name' in `pS', which is of non-class type `MyClass*'
ptr-struct.cpp:24: error: request for member `id_no' in `pS', which is of non-class type `MyClass*'
ptr-struct.cpp:24: error: request for member `age' in `pS', which is of non-class type `MyClass*'

cout << ps->first_name << ps->last_name << ps->id_no << ps->age << endl;

thank you very much...rep points to the both of you...uhmmm, i have a question...

pS->first_name

is synonymous to...

(*ps).first_name

right??uhmmm..what about this one?

*(ps).first_name

thank you again sirs...

ps->first_name == (*ps).firstname ; // ok
MyClass& ref = *ps ; ref.first_name ; // ok
*(ps).first_name ; // error, equivalent to
*(ps.first_name) ; // error, ps is a pointer
// reason: operator precedence

Thank you for that explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.