简体   繁体   中英

c++ string function

i am trying to create a string function which is callable from the main function, however this gives segmentation fault... any ideas???

also is it possible to create a string function which returns a string..

#include <iostream>
#include <stdio.h>
#include <sstream>


using namespace std;

string callMe(string& k){

    cout << "String from callMe: " << k;

}


int main(){

   string k;

   k = "K SHK";

   cout << "String from main function: " << k << endl;

   callMe(k);

}

UPDATE:

its compiles fine..

[root@server dev]# ./stringtest 
String from main function: K SHK
Segmentation fault

your function callMe is expected to return a string, but you never return anything.

If you have compiler warnings enabled, you are warned about this ( i suggest adding -Wall and -Wextra)

It's resolvable be changing the return type from string to void .

The main function however does not need a return statement (but for beginners it may be better to just add return 0; ):

From the C++ standard:

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing "return 0;"

The problem is that you have declared your function as returning a string , but don't return one.

There are two ways to fix it:

  • If you didn't intend to return a string, change callMe to

    void callMe(string& k);

  • If you did intend to return a string, then do so, by adding a return statement, eg

    return "I'm returned from callMe, called with " + k;

Additionally, some style comments:

  • It is a good idea to initialize variables right at the point of definitions where possible. In your case you'd write:

     string k = "K SHK"; 

    or

     string k("K SHK"); 

    If you don't intend to modify the string afterwards, you might also consider to define is as const to catch accidental changes (eg by use of = instead of == ):

     string const k = "K SHK"; 
  • If you don't change the argument in callMe , you should use a reference to const:

     void callMe(string const& k); 

    resp.

     string callMe(string const& k); 

This will allow you to call the function on constant strings and string literals, like callme("I'm a string literal");

void callMe(string& k){

    cout << "String from callMe: " << k;

}

Since you have declared Callme as a function that returns string, and on other side you are returning just nothing,such code can cause segmentation fault.(Since value in eax is undefined).

callMe()和main()函数必须返回某些内容...

In your function callMe there is no return . Try:

void callMe(string& k)
{
   cout << "String from callMe: " << k;
}

The main() function must return an int (usually 0).

Ps: If you don't modify the string k in your function, use a const reference const string& .

also is it possible to create a string function which returns a string.. Certainly. With a slight modification from your original code above it would look something like this...

string callMe(string& k){
    string returnValue("String from callMe: ");
    returnValue += k;
    cout << returnValue << endl;
    return returnValue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM