代码之家  ›  专栏  ›  技术社区  ›  Amit Ray

C++代码使用CUT函数停止工作错误

  •  0
  • Amit Ray  · 技术社区  · 6 年前

    #include <iostream>
    #include <cstring>
    using namespace std;
    int add(int a,int b){
      int c = a+b;
      cout<<c<<endl;
    }
    string add(string m,string n){
      string c = m+" "+n;
      cout<<c<<endl;
    }
    int main(){
        string x ="amit";
        string y ="kumar";
        add(x,y);//adding string
        add(5,58);//adding numbers
    }
    

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int add(int a,int b){
      int c = a+b;
      cout<<c<<endl;
    }
    string add(string m,string n){
      string c = m+" "+n;
      return c;
    }
    int main(){
        string x ="amit";
        string y ="kumar";
        string z = add(x,y);
    
        cout<<z<<endl;
        add(5,58);//adding numbers
    }
    

    我用代码块编程。为什么这是唐突的行为。我做错什么了。

    1 回复  |  直到 6 年前
        1
  •  4
  •   walnut    6 年前

    undefined behavior .

    string add(string m,string n) 和 int add(int a,int b) 宣布 non-void return

    void 它将在两个项目中按预期工作。

    如果尚未启用,请在编译器中启用其他警告。它应该警告你这个问题。如果它确实警告过你,那么请不要忽视这些警告。把它们都修好。


    你也需要注意 #include<string> ,不是 #include<cstring> ,使用 std::string