0

これは本当に私を夢中にさせています:

#include <iostream>
#include <vector>
#include <string.h>
#include <thread>

using namespace std;

void test() {
    vector<string> myvector;
    string a("Teststring");
    myvector.push_back(a);
    cout << myvector.begin()->length() << endl;
}

int main() {
    thread(test).join();
    return 0;
}

コードは、コンパイラに -std=c++11 フラグを指定し、リンカに -pthread フラグを指定して正常にコンパイルされます。

BUT: Eclipse は std::thread または myvector.begin()->length() のいずれかを知っています

ここで考えられるすべての解決策を試しました: Eclipse CDT C++11/C++0x supportは成功しませんでした。これには何時間もかかりました。何が間違っているのでしょうか?!

このコードで問題なくプロジェクトをセットアップできる人はいますか?

編集: 他のコード例 - 同じ問題:

#include <iostream>
#include <vector>
#include <thread>


using namespace std;

class TestClass {
public:
    void test() {
        cout << "test" << endl;
    }
};

void test() {
    vector<TestClass> testClassVector;
    TestClass x;
    testClassVector.push_back(x);
    testClassVector.begin()->test();
}

int main() {
    thread(test).join();

    return 0;
}

コンパイルして正常に実行されますが、Eclipse で返されます:メソッド 'test' を解決できませんでした

編集:

作業バージョン:

((TestClass)*(testClassVector.begin())).test();

TestClass foo2 = *(testClassVector.begin());
    foo2.test();

まだ動作していません:

testClassVector.begin()->test();

最後のものは、上記の 2 つのようにコンパイルおよび動作しますが、Eclipse は次のように主張します。

メソッド 'test' を解決できませんでした

4

3 に答える 3

1

私が間違っているかもしれませんが、あなたの問題は Eclypse に起因するものではないと思います。ただ、ベクトルのbegin()はstd::vector<T>::iterator最初に戻ります。これはポインタではなく、メソッドの長さはありませんが、これが必要な場合は、ベクトルのサイズを尋ねることができますmyvector.size();#include <string.h>string.hは、 strcmp #include <string>、strstr などの文字列操作用です...ちょうど文字列が std::string オブジェクトを定義します。

于 2013-03-01T01:45:46.967 に答える
0

Eclipse をセットアップしていませんが、問題は周辺にあるようstd::stringです。例からスレッドを削除すると、問題は解決しますか? #include <string>( string.hの代わりにも変更しました)

#include <iostream>
#include <vector>
#include <string>
#include <thread>

using namespace std;

#if 0
void test() {
    vector<string> myvector;
    string a("Teststring");
    myvector.push_back(a);
    cout << myvector.begin()->length() << endl;
}
#endif

int main() {
    //thread(test).join();
    vector<string> myvector;
    string a("Teststring");
    myvector.push_back(a);
    cout << myvector.begin()->length() << endl;
    return 0;
}

うまくいけば、それが出力されるはず10です。

コメントからの更新:

これにより Eclipse の警告が生成されますか?

auto tmp = *(myvector.begin());
std::cout << tmp.length() << std::endl;

これはどうですか?

std::string foo("abc123");
std::cout << foo.length() << std::endl;

もう1つ推測します:

std::string foo2 = *(myvector.begin());
std::cout << foo2.length() << std::endl;
于 2013-03-01T02:12:18.910 に答える
0

解決策は次のとおりです。

日食ケプラーケプラーをダウンロードしました

新しいプロジェクトを作成し、このソース コードをコンパイルしようとしました (上記のように):

#include <iostream>
#include <vector>
#include <thread>

using namespace std;

class TestClass {
public:
    void test() {
        cout << "test" << endl;
    }
};

void test() {
    vector<TestClass> testClassVector;
    TestClass x;
    testClassVector.push_back(x);
    testClassVector.begin()->test();
}

int main() {
    thread(test).join();
    return 0;
}

eclipse の最初の実行で、スレッドは新しい C++11 標準に属しており 、コンパイラ フラグに-std=c++11を追加する必要があるとのことでした。スレッドを使用するために、リンカ フラグに-pthreadも追加しました。この手順でコードをコンパイルできますが、Eclipse はスレッドをまだ不明としてマークします。これを修正するために、次の手順に進みました。

C/C++ ビルド (プロジェクト設定) の下で、プリプロセッサのインクルード パスを見つけて、[プロバイダー] タブに移動します。CDT GCC Builtin Compiler Settings 以外のすべての選択を解除します。次に、共有設定エントリのタグを外します … . Command というテキスト ボックスにオプション -std=c++11 を追加して、コンパイラの仕様を取得します。

ここにあります

今では信じられないことですが、日食によってマークされたエラーがなくても機能します。解決策は、Eclipse の (ベータ) バージョンを使用することです。これは、これをより適切に処理するようです。

ご助力いただきありがとうございます!

于 2013-03-01T22:54:35.637 に答える