0

std :: cout、std :: stringstream、およびstd :: string.c_str()で問題が発生しています。主に、どこかのバッファに引っかかっているものがあるようですが、問題を解決する方法がわかりません。

StackOverflowでコードを読むのが気に入らない場合は、私のgithubへの関連リンクを次に示します。TLStringクラスTestクラス、および単体テストです。最後までスキップして、より簡潔な質問を述べます。

私の単体テストでは、次のコードがあります。

    Test <std::string> strtest; // A unit test object expecting strings.
    Test <const char*> chtest; // A unit test object expecting const char*s
    // ...

    TurnLeft::Utils::TLString str3("Exterminate.");
    // ...

    /* Basically, name() and expect() will use the passed arg. 
     * in the output in order to
     * display output such as the following:
     * str(): expecting 'Exterminate.' | Actual 'Exterminate.' => PASS
     */
    strtest.name("str()").expect("Exterminate.").test( str3.str() );

    /* To try and determine where the corruption was occuring, I did a 
     * simple cout here, and got what should be the expected result of 
     * the next test,
     * meaning that the actual test should be succeeding.
     */
    std::cout << str3.c_str() << std::endl //outputs Exterminate. normally.

    /* But when I try to invoke that same method (c_str()) within the test
     * object, it simply copies the argument passed in for name().
     */
    chtest.name("c_str()").expect("Exterminate.").test( str3.c_str() );
    // Should output 'Exterminate.' as in the saatement before, but instead
    // outputs 'c_str()'.

Testクラスのコードは次のとおりです。

namespace unittest{
static std::string status[2] = {"FAIL", "PASS"};

    template <class ExpectedResult>
    class Test
    {
     private:
        ExpectedResult  expected;
        ExpectedResult  actual;
        std::string     testName;
     public:
        Test();
        Test <ExpectedResult>& expect (ExpectedResult value);
        Test <ExpectedResult>& name   (std::string);
        void test   (ExpectedResult value);
    };

template <class ExpectedResult> Test <ExpectedResult>&
Test<ExpectedResult>::expect(ExpectedResult value)
{    
    expected = value;
    return *this;
}

template <class ExpectedResult>
Test <ExpectedResult>&
Test<ExpectedResult>::name(std::string aName)
{
    testName = aName;
    return *this;
}

    template <class ExpectedResult>
    void Test<ExpectedResult>::test(ExpectedResult value)
    {
        actual = value;
        std::cout << testName << ": ";
        std::cout << "Expecting: " << expected << " | ";
        std::cout << "Actual: " << actual;
        std::cout << " => " << status[actual==expected] << std::endl;
    }

TLStringクラスは、私が書いているクラスであり、C ++の文字列にさらに流動的な操作(たとえば、連結)を提供します。これらの操作を処理するために文字列ストリームを使用します。TLStream::c_str()メソッドは実際にはこれを実行しているだけです。return stream.str().c_str();

actualですから、の値がどのように割り当てられているかについて、私は本当に混乱していますtestName。変数に対する変数が相互作用に近づくのは、両方がCLIに出力されるときだけであり、この場合のように、これら2つは異なるデータ型であるため、競合が発生している場所はわかりません。

非常に簡単に言えば、サードパーティのライブラリがC ++文字列ではなくC文字列に依存する時期がわからず、クラスを制限する理由がないため、c_str()関数で記述しました。std :: ios内でも、いくつかの目的でc文字列を使用する必要があります。

どんな助けでも大歓迎です。

ありがとう!

4

2 に答える 2

6

std::stringstream.str()タイプの一時オブジェクトを返しますstd::string。この一時的なものは、戻るときにスコープ外になりTLStream::c_str()、返さchar const*れたポインタは解放されたメモリを指します。

于 2012-05-17T19:34:07.243 に答える
0
    std::cout << " => " << status[actual==expected] << std::endl;

これは、文字列リテラルへのポインティングをへのポインティングと比較==しますconst char*"Exterminate"const char*str3.c_str()

それらのポインタは異なります。

strcmpポインタの同等性ではなく、のようなものを使用してそれらを比較する必要があります。

于 2012-05-17T19:48:48.403 に答える