-2

I have the following code in my cocos2d C++ application, but the code is not compiling:

  std::string MyBasketTimer::getImageByType(MyBasket* basket) {
        std::string retVal=NULL;
        if(getBasketType()==1){
            retVal= new std::string("count_bg.png");
        }
        else if(getBasketType()==2){
            retVal= new std::string("count_bg.png");
        }

        return retVal;
    }

The error is get is

invalid conversion from 'std::string* {aka std::basic_string<char>*}' to 'char' [-fpermissive]

What am I doing wrong?

Matt Davis
  • 45,297
  • 16
  • 93
  • 124
AndroidDev
  • 15,993
  • 29
  • 85
  • 119
  • 5
    The problem is that you're trying to write Java or C#, but in C++. You should pick up a good beginner's book and start reading. – molbdnilo Jan 08 '14 at 16:21
  • 2
    [Here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is a list of such books. – John Dibling Jan 08 '14 at 16:25

5 Answers5

4

Your return type is std::string but you are trying to assign a pointer to std::string to it:

retVal= new std::string("count_bg.png");

You need to assign an std::string to retVal:

retVal = std::string("count_bg.png");

or use the implicit conversion from string literal:

retVal = "count_bg.png";

Furthermore, this

std::string retVal=NULL;

will most likely cause a runtime error: you cannot instantiate a string with a null pointer. This will call the std::string constructor that takes a const char*, and that is assumed to point to a null-terminated string.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
3

The assignment std::string retVal = NULL; is invalid. Just default construct it using std::string retVal;

Also drop the new keywords as they create objects on the heap and return pointers to them. You need, for example, retVal = std::string("count_bg.png"); (This is one important difference between C++ and Java).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

In C++ (unlike some other languages) you don't need to allocate all class variables with new. Just assign it.

retVal= "count_bg.png";

Mark B
  • 95,107
  • 10
  • 109
  • 188
3

std::string retVal isn't a pointer. You can't initialize it with NULL (which should rather be nullptr...), nor assign the result of a memory allocation through new.

Just don't initialize it and assign directly the string then.

std::string retVal;
//...
retVal = "count_bg.png"
//...
return retVal;
JBL
  • 12,588
  • 4
  • 53
  • 84
-2

Your code would be correct if the return type of the function would be std::string *. For example

  std::string * MyBasketTimer::getImageByType(MyBasket* basket) {
        std::string *retVal=NULL;
        if(getBasketType()==1){
            retVal= new std::string("count_bg.png");
        }
        else if(getBasketType()==2){
            retVal= new std::string("count_bg.png");
        }

        return retVal;
    }

However you declared the function such a way that it has return type std::string. So the valid function implementation will look the following way

  std::string MyBasketTimer::getImageByType(MyBasket* basket) {
        std::string retVal;
        if(getBasketType()==1){
            retVal.assign("count_bg.png");
        }
        else if(getBasketType()==2){
            retVal.assign("count_bg.png");
        }

        return retVal;
    }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    But the caller must delete the pointer in the first case – Bathsheba Jan 08 '14 at 16:29
  • 1
    @Bathsheba And what is the problem? – Vlad from Moscow Jan 08 '14 at 16:30
  • 7
    @VladfromMoscow the problem is that it is a fragile solution, and OP will use it and not delete it, then complain that C++ needs GC etc. etc. :-) – juanchopanza Jan 08 '14 at 16:32
  • @ juanchopanza This has nothing common with the original post. He has some function declaration and need correctly to implement it. – Vlad from Moscow Jan 08 '14 at 16:34
  • 2
    -1 There's absolutely no reason to post the first half of your answer. It is obviously not the right solution to the OP's problem. – Praetorian Jan 08 '14 at 17:21
  • 5
    Typical Vlad posting dangerous and ill-advised pieces of code for no apparent reason. – Lightness Races in Orbit Jan 08 '14 at 17:24
  • +1 for posting a solution that helps the OP understand C++ even though it is ill-advised because the called has to call delete; and a contrasting method of doing the same thing. – Apriori Jan 08 '14 at 20:03
  • 1
    @Rich I wrote in the post "Your code would be correct if... " I think all is clear. We did not discuss with the aothor of the question how to implement better some or other interface. The author got a full answer and his knowledge of C++ was enlarged. As for other trolls that mark down my posts then it is their aim to mark down my posta. It is not the first time. They think that they are very clever. – Vlad from Moscow Jan 08 '14 at 20:08
  • @VladfromMoscow yes my intention in my comment was to indicate that I agree with you. I wanted to explicitly say I was giving an up-vote and why because so many are being overly critical of the solution you provided. Clearly OP misunderstands some C++ concepts, and not necessarily design principal. Your answer addresses the issue at hand well. – Apriori Jan 08 '14 at 20:23