繁体   English   中英

Eclipse C ++ 11 //矢量

[英]Eclipse c++11 // vector

这真的让我发疯:

#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标志给链接器,代码可以正常编译。

但是:即使代码运行良好,Eclipse也不知道std :: thread或myvector.begin()-> length(),警告我“方法'length'无法解析”

我在这里尝试了所有可能的解决方案: Eclipse CDT C ++ 11 / C ++ 0x支持没有成功。 现在这花了我很多小时,我在做什么错?

有没有人可以在没有此代码问题的情况下获得项目设置?

编辑:其他代码示例-相同的问题:

#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();

最后一个编译并像上面的两个一样工作,但是蚀仍然声称:

方法“测试”无法解析

也许我是错的,但我认为您的问题并非来自于Eclypse。 Juste,向量上的begin()首先返回std::vector<T>::iterator ,这不是指针,并且没有方法长度,但是您可以使用myvector.size();来请求向量大小myvector.size(); 如果这是您想要的。 问题可能来自您的#include <string.h>#include <string> ,string.h用于字符串操作,如strcmp,strstr等... juste string将定义std :: string宾语。

我没有设置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;

我猜也是:

std::string foo2 = *(myvector.begin());
std::cout << foo2.length() << std::endl;

找到的解决方案:

我下载了eclipse kepler Kepler

创建了一个新项目,并尝试编译此源代码(如上):

#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 ++ Build下(在项目设置下),找到Preprocessor Include Path并转到Providers选项卡。 取消选择除CDT GCC内置编译器设置以外的所有内容。 然后取消标记共享设置条目...。 将选项-std = c ++ 11添加到名为Command的文本框中以获取编译器规格。

这里找到。

现在-令人难以置信,但却是真实的-即使没有日食标记的任何错误,它也可以工作。 该解决方案使用的是Eclipse的Beta版,似乎可以更好地解决此问题。

感谢你的帮助!

暂无
暂无

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

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