0

I've tried several options but my compiler does not pick up the operator overloading or something else is wrong. I'm using XCode 4.5.2 with default Apple LLVM compiler 4.1.

The error I get is this: Assigning to 'cocos2d::CCString *' from incompatible type 'const char [5]'

on these lines:

CCString *s_piece__locks = "TEST";
cocos2d::CCString *s_piece__locks2 = "TEST";

My .h code:

CCString& operator= (const std::string& str);
//    CCString& operator= (const char* str);  // this doesn't work either
const CCString& operator = (const char *);

My .cpp code (even though this is irelevant):

CCString& CCString::operator= (const std::string& str)
{
    m_sString = CCString::create(str)->m_sString;
    return *this;
}

const CCString& CCString :: operator = (const char* str)
{
    m_sString = CCString::create(str)->m_sString;
    return *this;
}

Your help is very appreciated, thanks!

6
  • 1
    Which line produces the error message? Commented Jan 10, 2013 at 12:18
  • 1
    Based on the error message, you're trying to assign the char[] into a pointer to CCString. Commented Jan 10, 2013 at 12:21
  • @Angew you should add that as the answer. Commented Jan 10, 2013 at 12:23
  • @Angew oops, your comment was not there when I started writing the answer. Commented Jan 10, 2013 at 12:25
  • Looks like you're generally abusing pointers. Commented Jan 10, 2013 at 12:45

2 Answers 2

1

The error message Assigning to 'cocos2d::CCString *' from incompatible type 'const char [5]' suggests that you are assigning a char array to a pointer to cocos2d::CCString.

This should work:

char bar[] = "ABCD";
cocos2d::CCString foo;
foo = bar;
Sign up to request clarification or add additional context in comments.

6 Comments

Actually, that would use a copy constructor though, not copy assignment operator.
I got it, I use it with the pointer variable but the declaration is for a static object, correct? I should add another operator for the pointer CCString *foo = "ABCD", hope it is possible :-(
@leftaroundabout Ah, you are right. Overlooked that, sorry. Let me edit.
@devZone.touchDude.com Why not just dereference the pointer and be done with it?
@Deidara-senpai: I'm converting lot's of code from objectiveC and I want to keep code which has say CCString *str; as class property and I want to use this->str = "test" as assignmen, if possible. Still trying to make this happen but no success yet.
|
0
CCString *s_piece__locks = "TEST";
cocos2d::CCString *s_piece__locks2 = "TEST";

What the heck is this supposed to do? Declaring a pointer does not generate any object except the pointer itself. So basically, for this to "work", there would need to be another CCString object around already, that happens to represent the string "TEST". But even if that's given, how is C++ supposed to know which one to point to? It would need to look "TEST" up in some kind of e.g. hash map.

None of this makes any sense. Change your code to either

  • Direct use of object on stack:

    cocos2d::CCString s_piece;
    s_piece = "TEST";
    
  • Assigning new content to an object that resides somewhere else. You'd normally use a reference for this, e.g.

    void assign_test_to(cocos2d::CCString& target) {
      target = "TEST";
    }
    

    it's also possible with a pointer

    void assign_test_to_ptr(cocos2d::CCString* target) {
      *target = "TEST";
    }
    

    but don't do that unless you have a specific reason to.

In principle, there's another possibility:

cocos2d::CCString* s_piece_locks = new CCString;
*s_piece_locks = "TEST";

but you want to avoid this, as it can very easily lead to memory leaks. What would be ok is

std::unique_ptr<cocos2d::CCString> s_piece_locks = new CCString;
*s_piece_locks = "TEST";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.