-1

I have the TestObject class:

public:
    TestObject() {}
    std::string name;

In main function I do this

TestObject *to = new TestObject();
std::string t = "r";
to->name = t;
printf("%s",t);

I need to save simple string in name file on class object, but I'm doing with the wrong way. What is the solution?

Yoh Deadfall
  • 2,711
  • 7
  • 28
  • 32
RMaster21
  • 23
  • 1
  • 8
  • These are basics covered in any C++ book - I don't think this question is appropriate for StackOverflow. – Vittorio Romeo Feb 10 '17 at 11:32
  • Don't get me wrong, but the *real* solution here is to do some more learning *first*. You see, in contrast to popular believe, this site is **not** "programming school, where people explain you the super basics" for free. Sometimes we do, but in any case: it is far more effective for you to, well, do some more research *yourself*. – GhostCat Feb 10 '17 at 11:32
  • 1
    What exactly is the problem you're having? – Colin Feb 10 '17 at 11:34

1 Answers1

1

Don't use Strings with printf C++ printf with std::string?

Here is a working code

class TestObject
{
public:
    TestObject()
    {}
    std::string name;
};
int main(int argc, char const *argv[])
{
    TestObject * to = new TestObject();
    std::string t = "r";
    to -> name = t;
    std::cout << t; // or printf("%s",t.c_str());
    delete to;
    return 0;
}
Community
  • 1
  • 1
Dhruv Sehgal
  • 1,402
  • 1
  • 13
  • 15
  • Thanks a lot. I add the c_str() and it is work. I will seach now the 'paper' of this functions. thanks ;) – RMaster21 Feb 10 '17 at 11:43