【问题标题】:Eclipse c++11 // vectorEclipse c++11 // 向量
【发布时间】:2013-02-15 10:29:25
【问题描述】:

这真的让我发疯了:

#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(),即使代码运行良好 eclipse 也会警告我 “方法 '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();

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

方法“测试”无法解析

【问题讨论】:

  • 试试 '#include '
  • 试过了,没有解决问题..

标签: c++ eclipse multithreading vector c++11


【解决方案1】:

也许我错了,但我认为你的问题不是来自 Eclypse。 Juste,vector 上的 begin() 首先返回一个 std::vector&lt;T&gt;::iterator,这不是一个指针,也没有方法长度,但如果这是你想要的,你可以用 myvector.size(); 询问向量大小。 问题可能来自您的#include &lt;string.h&gt;,它与#include &lt;string&gt; 不同,string.h 用于字符串操作,如 strcmp、strstr 等...juste string 将定义 std::string 对象。

【讨论】:

  • 我认为关键是打印 10 个字符长的字符串的长度。
  • 哦,是的,对不起,我的错。因此,问题必须来自 string.h 包含针对 juste 字符串。我编辑它。
  • 如此处所述:mochima.com/tutorials/vectors.html 您可以通过此指针访问方法。或者有没有其他方法可以做到这一点?
  • ...我还添加了第二个没有字符串的代码块,具有完全相同的问题..
  • -> 运算符的替代方法是使用 * 运算符,看:testClassVector.begin()-&gt;test(); 可以写成:(*testClassVector.begin()).test(); 也许 -> 运算符不起作用
【解决方案2】:

我没有设置 Eclipse,但问题似乎在 std::string 附近。如果从示例中删除线程,问题会消失吗? (我也改成#include &lt;string&gt;而不是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;

【讨论】:

  • 确实打印出10,但是eclipse中的错误还是一样:Method 'length' could not be resolved
  • 第一个以同样的错误结束。第二个有效(你错过了第一个标准之后的 : )并给了我这个字符串的长度。
  • 第三版有效!但是我必须以这种方式访问​​所有方法吗?
  • 在第一篇文章中编辑,必须有一种方法可以在不强制转换的情况下访问这些方法,编译没有问题,只是eclipse一直声称..
  • 我想知道是否有可以调整的设置。如果没有在代码中准确拼写出的类型,Eclipse 似乎无法告诉函数或类的 API。我希望你有另一种方式。
【解决方案3】:

找到的解决方案:

我下载了eclipse开普勒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 Builtin Compiler Settings 之外的所有选项。然后取消标记共享设置条目...。将选项 -std=c++11 添加到名为 Command 的文本框中以获取编译器规范。

找到here

现在 - 令人难以置信但真实 - 它可以工作,即使没有任何 eclipse 标记的错误。解决方案是使用 eclipse 的(测试版)版本,它似乎以更好的方式处理这个问题。

感谢您的所有帮助!

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 2018-09-30
    • 2014-07-18
    • 1970-01-01
    • 2014-04-15
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多