[英]Eclipse c++11 // vector
This is really driving me crazy: 这真的让我发疯:
#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;
}
The code compiles fine with the -std=c++11 flag to the compiler and the -pthread flag to the linker. 使用-std = c ++ 11标志给编译器,使用-pthread标志给链接器,代码可以正常编译。
BUT: Eclipse does either know the std::thread or the myvector.begin()->length(), even if the code runs fine eclipse warns me "Method 'length' could not be resolved" . 但是:即使代码运行良好,Eclipse也不知道std :: thread或myvector.begin()-> length(),警告我“方法'length'无法解析” 。
I tried every possible solution in here: Eclipse CDT C++11/C++0x support without any success. 我在这里尝试了所有可能的解决方案: Eclipse CDT C ++ 11 / C ++ 0x支持没有成功。 This took me so many hours now, what am I doing wrong?! 现在这花了我很多小时,我在做什么错?
Is there anybody getting a project setup without problems with this code? 有没有人可以在没有此代码问题的情况下获得项目设置?
EDIT: Other code example - same problem: 编辑:其他代码示例-相同的问题:
#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;
}
Compiles and runs correct, but returns in eclipse: Method 'test' could not be resolved 编译并正确运行,但是以eclipse返回: 无法解析方法“ test”
EDIT: 编辑:
working versions: 工作版本:
((TestClass)*(testClassVector.begin())).test();
TestClass foo2 = *(testClassVector.begin());
foo2.test();
still not working: 还是行不通:
testClassVector.begin()->test();
The last compiles and works like the two above, but eclipse still claims: 最后一个编译并像上面的两个一样工作,但是蚀仍然声称:
Method 'test' could not be resolved 方法“测试”无法解析
Maybe I'm wrong, but I think your problem don't come from Eclypse. 也许我是错的,但我认为您的问题并非来自于Eclypse。 Juste, begin() on a vector return a std::vector<T>::iterator
first, this is not a pointer and there is no method length, but you can ask for the vector size with myvector.size();
Juste,向量上的begin()首先返回std::vector<T>::iterator
,这不是指针,并且没有方法长度,但是您可以使用myvector.size();
来请求向量大小myvector.size();
if this is what you want. 如果这是您想要的。 The problem could come from your #include <string.h>
that is not the same as #include <string>
, string.h is for string operation like strcmp, strstr, etc... juste string will define the std::string object. 问题可能来自您的#include <string.h>
与#include <string>
,string.h用于字符串操作,如strcmp,strstr等... juste string将定义std :: string宾语。
I don't have Eclipse set up but the problem appears to be around std::string
. 我没有设置Eclipse,但问题似乎出在std::string
周围。 Does the problem go away if you remove the threading from the example? 如果从示例中删除线程,问题是否会解决? (I also changed to #include <string>
instead of string.h) (我也更改为#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;
}
That should hopefully print out 10
. 希望可以打印出10
。
Update from comment: 评论更新:
Does this generate the Eclipse warning? 这会产生Eclipse警告吗?
auto tmp = *(myvector.begin());
std::cout << tmp.length() << std::endl;
What about this? 那这个呢?
std::string foo("abc123");
std::cout << foo.length() << std::endl;
I guess one more too: 我猜也是:
std::string foo2 = *(myvector.begin());
std::cout << foo2.length() << std::endl;
The solution found: 找到的解决方案:
I downloaded eclipse kepler Kepler 我下载了eclipse kepler Kepler
Created a new project and tried to compile this source code (like above): 创建了一个新项目,并尝试编译此源代码(如上):
#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;
}
On the first run eclipse told me, thread belongs to the new c++11 standard and I have to add -std=c++11 to the compiler flags. 第一次运行eclipse告诉我,线程属于新的c ++ 11标准,我必须在编译器标志中添加-std = c ++ 11 。 To use thread I also added -pthread to the linker flags. 为了使用线程,我还向链接器标志添加了-pthread 。 With this steps the code could be compiled, but eclipse marks the thread still as unknown. 通过此步骤,可以编译代码,但是Eclipse将线程标记为未知。 To fix this I proceeded the following step: 要解决此问题,我继续执行以下步骤:
Under C/C++ Build (at project settings), find the Preprocessor Include Path and go to the Providers Tab. 在C / C ++ Build下(在项目设置下),找到Preprocessor Include Path并转到Providers选项卡。 Deselect all except CDT GCC Builtin Compiler Settings. 取消选择除CDT GCC内置编译器设置以外的所有内容。 Then untag Share settings entries … . 然后取消标记共享设置条目...。 Add the option -std=c++11 to the text box called Command to get compiler specs. 将选项-std = c ++ 11添加到名为Command的文本框中以获取编译器规格。
Now - unbelievable but true - it works, even without any errors marked by eclipse. 现在-令人难以置信,但却是真实的-即使没有日食标记的任何错误,它也可以工作。 The solution is using the (beta) version of eclipse, wich seems to handle this in a better way. 该解决方案使用的是Eclipse的Beta版,似乎可以更好地解决此问题。
Thanks for all your help! 感谢你的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.