简体   繁体   English

c ++从用户输入返回字符串或char

[英]c++ return string or char from user input

im trying to return a string/char (tried with both) from a funtion: 我试图从一个功能返回一个字符串/字符(都尝试):

 char* foo(void)

    {

    using namespace std;
   // char Name[100];
    std::string Name;
    std::cout << "Type your name : ";
    //std::getline(std::cin, Name);
    std::cin >> Name;

    // std::cout << "Hello " << Name << "\n";

   // std::string str(Name);
    return Name;
    }

which gives me this error: 这给了我这个错误:

/home/peter/netsend/main.cpp:22: error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'char*' in return return Name; /home/peter/netsend/main.cpp:22:错误:无法将'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}'转换为'char *'返回返回名称; ^ ^

If i use char Name[100]; 如果我使用char Name [100]; i get this warning when i compile my program: 编译程序时收到此警告:

/home/petter/netsend/main.cpp:13: warning: address of local variable 'Name' returned [-Wreturn-local-addr] char Name[100]; /home/petter/netsend/main.cpp:13:警告:返回了本地变量“名称”的地址[-Wreturn-local-addr] char Name [100]; ^ ^

when i run it i get: 当我运行它时,我得到:

ype your name : hh terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid Aborted (core dumped) ype your name:hh抛出'std :: logic_error'what()实例之后调用终止(what :):basic_string :: __ M_construct null无效中止(转储核心)

if i run this directly in int main it works: 如果我直接在int main中运行它,它将起作用:

using namespace std;
   // char Name[100];
    std::string Name;
    std::cout << "Type your name : ";
    //std::getline(std::cin, Name);
    std::cin >> Name;

    // std::cout << "Hello " << Name << "\n";

   // std::string str(Name);

then i could use Name in my other functions in int main. 那么我可以在int main的其他函数中使用Name。 any ideas? 有任何想法吗?

Thanks 谢谢

Just return a string , like this: 只需返回一个string ,像这样:

auto foo()
    -> std::string
{
    using namespace std;
    string Name;
    cout << "Type your name : ";
    getline( cin, Name );
    return Name;
}

Or instead of the C++11 "trailing return type" syntax, use the old syntax 或使用旧语法代替C ++ 11“跟踪返回类型”语法

std::string foo()

… which means the same and is still much more common, but can't be used in every case. …意思是相同的,并且仍然很常见,但是不能在每种情况下都使用。

I will divide your problem into cases which I will describe briefly. 我将把您的问题分为几个案例,我将对此进行简要描述。

  1. If you create a local array, namely char Name[100] and return it, you will end up with a dangling pointer . 如果创建一个本地数组,即char Name[100]并返回它,则最终将得到一个dangling pointer Name[100] is an automatic variable which means that once the function goes out of scope the pointer that is returned by the function will point to an invalid memory location. Name[100]是一个automatic变量,这意味着一旦函数超出范围,函数返回的pointer将指向无效的存储器位置。

  2. return Name when Name is an std::string . Namestd::string时, return Name Well, here you have a type mismatch. 好吧,这里有一个类型不匹配。 Your function returns a char* and you return a std::string . 您的函数返回一个char*并且您返回一个std::string Hence, your compiler issues an error. 因此,您的编译器会发出错误。

To fix your issue declare Name as a std::string and declare the function to return a std::string like this: std::string foo(void) . 要解决您的问题,请将Name声明为std::string并声明函数以返回std::string如下所示: std::string foo(void)

In the first case, you should probably return a string (ie, std::string foo(void) { ... } ) and then simply use string::c_str() to get the C-style string from it when/if needed. 在第一种情况下,您可能应该返回一个string (即std::string foo(void) { ... } ),然后仅在以下情况下使用string::c_str()从中获取C风格的字符串。需要。

In the second case, the compiler is exactly right. 在第二种情况下,编译器是完全正确的。 You cannot safely use a variable after its scope has ended, that will be undefined behaviour. 在变量作用域结束后,您将无法安全使用变量,这将是不确定的行为。 The char array ceases to exist when the function exits and you're returning the pointer to its first character, a big no=-no. 当函数退出并且返回指向其第一个字符的指针时, char数组将不复存在,即大的no = -no。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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