2

I do not know whats going on. The program seemed to work fine on visual studio. Maybe I got something wrong and I cant spot it.. Please someone help me out with this one.

#include <iostream>
#include<string>
#include<cstdlib>

using namespace std;
class Aokiji
{
private:
    string name1;
    string name2;
public:
    string setName(string x)
    {
        name1=x;
    }
    string getName()
    {
        return name1;
    }
};

int main()
{
    Aokiji Grapes;
    Grapes.setName("I got it!");
    cout<< Grapes.getName()<< endl;
    system("pause");
}
Alan Watts
  • 33
  • 1
  • 7
  • Describe the crash. Is it a SegFault? What does a debugger tell you about the crash? – abelenky Apr 14 '14 at 21:52
  • It works fine in visual studio, but doesn't work in what? – ooga Apr 14 '14 at 21:52
  • getName() should be const – qdii Apr 14 '14 at 21:55
  • 1
    possible duplicate of [If a function returns no value, with a valid return type, is it okay to for the compiler to throw garbage?](http://stackoverflow.com/questions/9936011/if-a-function-returns-no-value-with-a-valid-return-type-is-it-okay-to-for-the) – juanchopanza Apr 14 '14 at 22:08

3 Answers3

4

Your setName() function doesn't return anything, but it's supposed to return a std::string. You need to return a value, or make the setName() function void (which is probably what it should be)

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • 1
    But he's not using the return value, so could it cause a crash? – ooga Apr 14 '14 at 21:54
  • @ooga Function doesn't know if you use the return value or not. – Neil Kirk Apr 14 '14 at 21:54
  • @ooga - If that function is silently returning 0 or an integer, then that is a cause of a segmentation fault. Try to assign an integer to a std::string, and you will see what I mean. – PaulMcKenzie Apr 14 '14 at 21:56
  • @NeilKirk I know, but the fact is that he doesn't use it. Maybe it's some kind of "undefined behavior" if the function doesn't return a value when it says it will, even if you don't use it. I don't know. – ooga Apr 14 '14 at 21:56
  • It is undefined behavior. I'm surprised it compiles. A function with a return value requires at least one return statement on some compilers. – Neil Kirk Apr 14 '14 at 21:57
  • Ohhhh, I see. Interesting. – ooga Apr 14 '14 at 21:57
  • Can confirm. What is probably happening is during the malformed string's destructor, bad stuff is happening. How did this code compile? – QuestionC Apr 14 '14 at 21:57
  • 2
    Then the OP's main problem is that his warning level is not set high enough because his compiler should have warned him about this! – ooga Apr 14 '14 at 21:59
  • I am surprised that this is UB. GCC does not even raise an error, it only raises a warning if -Wreturn-type is set. Also, I have never seen a segfault because of that. Anyone could point out where it is UB in the standard? – qdii Apr 14 '14 at 22:01
  • Don't know if this is standard or not. Don't have the ANSI docs in front of me right now. However, if there is any hint of an integer being returned, then std::string constructor will try and construct one using the `const char*` overload, thus causing bad things to happen. – PaulMcKenzie Apr 14 '14 at 22:03
  • @qdii See http://stackoverflow.com/questions/23043042/what-is-the-return-value-if-we-dont-return-anything-from-a-non-void-return-typed/23043061#23043061 – juanchopanza Apr 14 '14 at 22:06
  • @qdii: C++11 6.6.3/4 "Flowing off the end of a function is equivalent to a return with no value; this results in **undefined behavior** in a value-returning function" - so yes a crash is fine even if the return value isn't used. – Michael Burr Apr 14 '14 at 22:14
  • 1
    @qdii: For completeness, note that C is different in this regard. From C99 6.9.1/12: "If the } that terminates a function is reached, **and the value of the function call is used by the caller**, the behavior is undefined" – Michael Burr Apr 14 '14 at 22:19
  • This has fixed the problem, thank you all for your help – Alan Watts Apr 18 '14 at 15:47
0

As was pointed at by PaulMacKenzie, the problem is the missing return statement. This is the reason why using C++, one should always use the -Werror=return-type flag to ensure that functions do not miss return statements.

I'm amazed C++ allows that, actually. The only case I think where it is legitimate that a function could miss a return statement is if it is supposed to return something, but instead it throws an exception and therefore does not return. But that's a very narrow use case, and adding a dummy return statement in that case does not cost anything.

Vincent Fourmond
  • 3,038
  • 1
  • 22
  • 24
0

Replace setName() method in your code by:

void setName(string x)
{
    name1 = x;
}

And you need return 0; in int main() .

lightqb
  • 3
  • 3