Pointer assignment Error

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!
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.
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.
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
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.
ie. myClass->field1 = &myString; Now the pointer points to myString.
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
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...
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...
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.
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.
@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
No worries. I didn't read it that way.
Assumptions are hard to avoid making when info is lacking.
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.
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.