Pointer assignment Error

Mar 14, 2013 at 2:58pm
Hi,

Suppose this code snippet:
1
2
3
4
5
6
7
8
class myClass
{ public:
    std::string* field1;	
    std::string* field2;	
    LONG64* id;	
    std::string* field3;	
    std::string* field4;
};


How to make an assignment of each field?
(constructor call already made ​​previously)
1
2
3
4
5
myClass->field1="TEST";
myClass->field2="TEST";
myClass->id=  ??; //
myClass->field3="TEST";
myClass->field4="TEST";


Error: cannot convert const char [5]' to std::string* in assignment.

Thanks in advance!
Mar 14, 2013 at 3:02pm
These fields are all pointers to strings, put but your passing in a string to them. You're missing one indirection here. You dereference the myClass pointer to get the field, now you need to dereference that field also. Or just change them to strings and not pointers to strings.
Mar 14, 2013 at 3:02pm
closed account (3qX21hU5)
std::string* field1; Is just creating a pointer that can point to a string. It is not creating a pointer to a string and a string to hold the text.

So the reason you are getting this error is because you are trying to assign "TEST" to a string pointer that is pointing to nothing.
Mar 14, 2013 at 3:13pm
Thanks a lot.
But What to do to solve it?

1
2
  std::string myString = "TEST";
  myClass->field1 = myString; // This dont work 


Thanks in advance
Mar 14, 2013 at 3:16pm
We just told you, field1 (and the others) are just pointers to strings. They aren't strings themselves, so you can't assign a string to it. Either change the type of the fields to std::string, or derefence the field before assigning to it.
Mar 14, 2013 at 4:14pm
closed account (D80DSL3A)
ie. myClass->field1 = &myString; Now the pointer points to myString.
Mar 14, 2013 at 4:21pm
or *myClass->field1 = "TEST";

myClass->field1 represents a pointer
*myClass->field1 represents the string pointed to by field1.

The problem with doing
1
2
std::string myString = "TEST";
myClass->field1 = &myString;

is that the field1 pointer will become invalid when myString goes out of scope.

EDIT:
On another note, you probably don't need to use string pointers in your class. It would probably be advised not to. There's no need to them to be allocated on the heap.
Last edited on Mar 14, 2013 at 4:22pm
Mar 14, 2013 at 4:39pm
closed account (D80DSL3A)
The problem with doing *myClass->field1 = "TEST"; is that field1 isn't pointing to a string yet, so SEGFAULT.

I agreed re. pointing to a string which may go out of scope though. It's a flawed approach. ResidentBiscuit gave the best advice (in my opinion)
...change the type of the fields to std::string...
Mar 14, 2013 at 4:42pm
1
2
3
4
5
    std::string field1;	
    std::string field2;	
    LONG64* id;	
    std::string field3;	
    std::string field4;


just do it like that, and there should be no problem...
Mar 14, 2013 at 5:27pm
fun2code wrote:
field1 isn't pointing to a string yet
I suppose it's too much to assume they would have been initialized in the constructor.

Resident did give the best advice.
Mar 14, 2013 at 6:58pm
closed account (D80DSL3A)
Indeed I did miss this:
(constructor call already made ​​previously)
so you could be right about that.
However, the posted class definition
1
2
3
4
5
6
7
8
class myClass
{ public:
    std::string* field1;	
    std::string* field2;	
    LONG64* id;	
    std::string* field3;	
    std::string* field4;
};

contains not even a prototype for any constructor, so who knows?
Some clarification by OP would be useful.
Mar 14, 2013 at 7:15pm
@fun2code
I didn't mean for that to sound hostile (looking back on it it kind of did); I have a problem about making assumptions. :P
Last edited on Mar 14, 2013 at 7:16pm
Mar 14, 2013 at 7:24pm
closed account (D80DSL3A)
No worries. I didn't read it that way.
Assumptions are hard to avoid making when info is lacking.
Mar 14, 2013 at 8:29pm
It also looks like the OP made an object with the same name as the class, although not illegal - I wouldn't recommend doing that because it can be too confusing.

Otherwise, the OP may not have known about having to create an object to call class functions.
Mar 15, 2013 at 7:36am
Hi,

The source code is just a snippet for educational purpose only.

As ResidentBiscuit said: "now you need to dereference..." and with the example provided by Zereo
std::string* field1; I finally understand the concept.

Thanks to all for reply.
This forum is great!

Best regards!
Topic archived. No new replies allowed.