[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

String Question

I would like to append a pointer to a string. This is my function:

Good::Good(GoodDescription* desc, string name, int val)
{
      string int_state;
      Caretaker caretaker;

      Memento* memento;
      memento = Good::createMemento();      
      caretaker.push_back(memento);
      Good::setMemento(caretaker.front());

      description = desc;
      goodName = name;
      value = val;

      int_state += "State: " + memento + "\n";
      cout << int_state;

      caretaker.pop_front();
}

I get an error that I can't add two pointers.

Thanks,
Igor
0
igor92128
Asked:
igor92128
  • 5
  • 4
  • 2
2 Solutions
 
AxterCommented:
You can't append a pointer to a string.

You can append a string to a string, or a pointer to a string to a string, but not a generic non-string pointer.

0
 
AxterCommented:
If int_state is an std::string, and you're trying to add two C-style strings to int_state, then what you can do is either wrap them in an std::string, or you can append each string one at a time.

example:
string int_state;
char * s1 = "State: ";
char * s2 = "Some memento";

int_state += s1;
int_state += s2;
int_state += "\n";

or

int_state = std::string("State:  ") + std::string(s2) + std::string("\n");

Regardless of what method you use, you need to be able to conver the Memento type to either a string or a char* pointer.
0
 
igor92128Author Commented:
Axter,

I'm not sure what you mean by the example.
char * s2 = "Some memento";
This is not Memento* memento.

I can:
cout << memento;
so there must be a way to append it to a string, or do something to put it in some sort of log for reference.

Igor
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AxterCommented:
>>so there must be a way to append it to a string, or do something to put it in some sort of
>> log for reference.

That is not what that means.
A cout can take many different types that can not be added to a string.
cout can take int, float, double.  Neither of these types can be directly added to a string.
More over, if memento is a pointer, then the cout << memento code is just going to send the address of memento to the cout, and it's not going to send the contents of memento to cout.

What is Memento?
Does it have a char* interface?
Please post it's class declaration, and so we can give you a more complete answer.

0
 
jkrCommented:
>>I can:
>>cout << memento;

Then you also can use a stringstream as a helper, e.g.

#include <sstream>
using namespace std;

Good::Good(GoodDescription* desc, string name, int val)
{
    stringstream tmp;
    string int_state;
    Caretaker caretaker;

    Memento* memento;
    memento = Good::createMemento();    
     caretaker.push_back(memento);
    Good::setMemento(caretaker.front());

    description = desc;
    goodName = name;
    value = val;

    ss << memento;
    int_state += "State: " + tmp.str() + "\n";
    cout << int_state;

    caretaker.pop_front();
}
0
 
jkrCommented:
Ooos, correction, that should have been

Good::Good(GoodDescription* desc, string name, int val)
{
   stringstream tmp;
   string int_state;
   Caretaker caretaker;

   Memento* memento;
   memento = Good::createMemento();    
     caretaker.push_back(memento);
   Good::setMemento(caretaker.front());

   description = desc;
   goodName = name;
   value = val;

   tmp << memento;
   int_state += "State: " + tmp.str() + "\n";
   cout << int_state;

   caretaker.pop_front();
}
0
 
igor92128Author Commented:
Yeah jkr that works.
Here is my memento class anyways, if you are interested. It's a design pattern:

#ifndef _memento_h
#define _memento_h

#include <deque>

using namespace std;

/**
 * Responsible for the memento's safekeeping. Never operates
 * on or examines the contents of a memento.
 */
typedef deque<Memento*> Caretaker;

class State
{
      //Empty Class
};

class Memento
{

private:
      State* state;

public:
      Memento(): state(0){};

      Memento(State* state)
      {
            this->state = state;
      }

      State* getState()
      {
            return state;
      };

      void setState(State* state)
      {
            this->state = state;
      };

};
#endif

Igor
0
 
AxterCommented:
The code that jkr posted, may compile, but it's not going to actually work.

At best, you'll get the output of the address for your memento.

You're not going to get the actual contents of memento.
0
 
igor92128Author Commented:
But the address is what I was expecting, isn't it?
What would you do to print the contents?

Igor
0
 
igor92128Author Commented:
Hold on, I will start a new thread for this question.
0
 
AxterCommented:
>>But the address is what I was expecting, isn't it?

No.

What you should be trying to get is what your memento type represents.

It's memory address is of little value for display or output purposes.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now