1
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class newclass
{
public:
    string *Date;
    void show(){ cout << this->Date;}

};

int main()
{
    newclass *n = new newclass;
    n->Date = new string ;
    n->Date.assign("hello");   // get an error on this line
    n->show();
    return 0;
}

Can Somebody please explain me how this works?. I am new to this.

Tom
  • 11
  • 1
  • Which part are you having trouble with, and what is the error that you get? – EkcenierK Aug 23 '11 at 19:31
  • I get an error "error C2228: left of '.assign' must have class/struct/union" at the line n->Date.assign("hello"); – Tom Aug 23 '11 at 19:32
  • 1
    Hmm, what exactly do you need to know? You'll get an error on the suggested line because n->Date is a pointer and you should use n->Date->assign. – Daniel Băluţă Aug 23 '11 at 19:33
  • 5
    Why are you using `new` and `this` and pointers? C++ isn't anything like Java. If you're new, you should get a [good beginner book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – GManNickG Aug 23 '11 at 19:34
  • 2
    The correct version of your code would look like this: http://ideone.com/WvByd Now isn't that much simpler? – Benjamin Lindley Aug 23 '11 at 19:44

5 Answers5

7

n->Date->assign("hello") because n->Date is a pointer, so you must dereference it before calling a function on it.

Please look at this SO Question for a detailed explanation of the syntax.

Community
  • 1
  • 1
Bob Fincheimer
  • 17,978
  • 1
  • 29
  • 54
  • Can you tell me how can I do that – Tom Aug 23 '11 at 19:34
  • its a syntax thing. `.` means access a member function/field of a class. But if you have a pointer you must dereference it first. To dereference you can use a `*` on the pointer (e.g: `*pointer`). Then you can access the fields. (e.g: `(*pointer).Date`). This syntax has a simpler abbreviation: `(*pointer).Date` is the same as `pointer->Date). – Bob Fincheimer Aug 23 '11 at 19:37
  • @Tom Please look at the link in the question for a better explanation. – Bob Fincheimer Aug 23 '11 at 19:39
  • @Tom: You need to get a book. You aren't going to learn C++ from online resources only, it's too complicated. – GManNickG Aug 23 '11 at 20:16
2

You've got a few problems here, let's go over them one at a time:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class newclass
{
public:
   // This is a pointer to a std::string, that doesn't appear to 
   // be allocated.  From this small example, you don't really need
   // a pointer at all:
   // string *Date;

   // now that this object isn't a pointer, you can use dot syntax
   // to access its member functions
   string Date;

   void show()
   {
      // accessing date through this-> is not necessary 
      // here.  You can simply use Date.  However since this
      // doesn't cause any specific problems I mention it only 
      // as informational
      cout << this->Date;
   }

};

int main()
{
    newclass *n = new newclass;

    // This is bad practice, you generally shouldn't be allocating
    // objects within a class outside of that class's implementation.
    // this would better be done in the newclass constructor.
    //  n->Date = new string ;

    // Since we replaced the pointer to string object Date in newclass
    // and replaced it with an automatic string object, this line will
    // now work as written. 
    n->Date.assign("hello");   // get an error on this line
    n->show();

    // At this point your program will exit, and newclass is shown as
    // a memory leak.  While the OS will reclaim this memory at application
    // exit, its good to get used to managing the lifetime of your objects
    // appropriate.  This has do be deleted, or better yet wrapped in a smart 
    // pointer
    delete n;

    return 0;
}

To provide an example that is similar to your original question, let's take the following code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <memory>
using namespace std;


class newclass
{
public:
   // auto_ptr is a smart pointer class that 
   // wraps a dynamically allocated object and
   // provides cleanup for it when it goes out
   // of scope.  So when our class goes out of 
   // scope the Date object will be cleaned up
   // for us
   auto_ptr<string> Date;

   // this is the constructor of the newclass object
   // its called anytime a newclass object is instantiated
   // we use the initializer list to allocate the Date object
   newclass() : Date(new string)
   {
   }

   // a mutator for setting the Date object (promotes encapsulation)
   void set_date(const std::string& val)
   {
      // necessary to dereference to get at the date object
      (*Date) = val;
   }

   void show() {  cout << this->Date; }
};

int main()
{
    // note the use of an auto_ptr here for automatic cleanup
    auto_ptr<newclass> n(new newclass);

    // use our mutator method to set the date
    n->set_date("hello");
    n->show();

    return 0;
}
Chad
  • 18,706
  • 4
  • 46
  • 63
2

It looks like you're applying idioms from another language. In C++ you should avoid pointers whenever possible, replaced by member or local variables. Your code in that case would look like this:

class newclass
{
public:
    string Date;
    void show(){ cout << this->Date;}
};

int main() {
    newclass n;
    n.Date.assign("hello");
    n.show();
    return 0;
} 
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

Date is itself a pointer to string, you will need to dereference it as well:

n->Date->assign("hello");

TeaWolf
  • 714
  • 5
  • 10
0
    newclass *n = new newclass;
    n->Date = new string ;
    n->Date->assign("hello");   // use -> when accessing a member of a pointer
    n->show();
André Puel
  • 8,741
  • 9
  • 52
  • 83