2017-03-02 11 views
-1

私はエラーがなく、私のプログラムを実行することができたので、ヘッダを作成して文字列操作を少し使い方を学んでいました。私のプログラマーは働いていますが、私は将来のsegfaultを避けたいです。 segfaultはどこにありますか?初心者のためのセグメンテーションフォールト

main.cppに

#include <string> 
#include <stdio.h> 
#include <iostream> 
#include <header_test.h> 
//using namespace std; 

int main() 
{ 
    printf("hello world\n"); 

    string x = test(); 

    return 0; 
} 

header_test.h

#ifndef TEST_H 
#define TEST_H 

#include <string> 
#include <iostream> 
using namespace std; 

string test(); 

#endif 

TEST.CPP

#include <header_test.h> 

string test() 
{ 
    string text; 
    cout << "Enter your name: \n"; 
cin >> text ; 

cout << "your name :\n" << text<< "\n"; 


string result; 
string s1 = "addition de "; 
string s2 = " string \n"; 
result = s1 + s2; 

cout << "\n" << result ; 

} 
+4

'test'機能は* *何も返しません。コンパイラが警告していたはずです。 –

+0

別の例として、最も重要な警告コンパイラオプションが使用されていた場合、その問題の内容を知ることができます。 –

+0

このような問題を解決する適切なツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –

答えて

2

TEST.CPPは、return文が欠落しています。これは悪いことが起こる原因となります。

それはresultのようにあなたは返すつもり何に見えますので、あなただけの機能で最後の行として、これを追加する良いことがあります:return result;

関連する問題