0

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.

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".

I tried every possible solution in here: Eclipse CDT C++11/C++0x support without any success. 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

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

Community
  • 1
  • 1
chrstnwhlrt
  • 331
  • 1
  • 4
  • 18

3 Answers3

1

Maybe I'm wrong, but I think your problem don't come from 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(); 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.

Hulor
  • 219
  • 1
  • 5
  • I think the point is printing the length of the string which is 10 characters long. – Joel Mar 01 '13 at 02:05
  • Oh yes, sorry, my bad. So, the problem must come frome the string.h include against juste string. I edit it. – Hulor Mar 01 '13 at 02:09
  • As described here: http://www.mochima.com/tutorials/vectors.html you can access methods via this pointer. Or is there any other way doing this? – chrstnwhlrt Mar 01 '13 at 02:12
  • ...I also added a second code block without string having the exactly same problem.. – chrstnwhlrt Mar 01 '13 at 02:15
  • An alternative of the -> operator is to use ther * operator, look : `testClassVector.begin()->test();` can be write as : `(*testClassVector.begin()).test();` Maybe the ->operator isn't functionnal – Hulor Mar 01 '13 at 02:17
  • Just to be clear: the compiling works and it prints "test" after running without any problems. The only annoying here is eclipse telling me, there is no method "test".. – chrstnwhlrt Mar 01 '13 at 02:25
  • Did you take a look on this link : http://stackoverflow.com/questions/13674258/eclipse-method-could-not-be-resolved-for-vector-of-vectors. Maybe there is the problem? – Hulor Mar 01 '13 at 02:33
  • Good idea, but I created now several projects with nearly all combinations of c++11 activation in the project settings. All holding just the (second) test code shown above and either they don't know the std::thread or they don't know the methods inside the vector items.. – chrstnwhlrt Mar 01 '13 at 03:19
  • What about : http://stackoverflow.com/questions/7905025/string-could-not-resolved-error-in-eclipse-for-c or http://stackoverflow.com/questions/5977542/eclipse-cdt-unresolved-inclusion-of-stl-header, did you try anything? – Hulor Mar 01 '13 at 09:45
  • Both tested without success :( – chrstnwhlrt Mar 01 '13 at 21:54
0

I don't have Eclipse set up but the problem appears to be around 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 <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.

Update from comment:

Does this generate the Eclipse warning?

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;
Joel
  • 2,928
  • 2
  • 24
  • 34
  • It does print out 10, but the error in eclipse is still the same: **Method 'length' could not be resolved** – chrstnwhlrt Mar 01 '13 at 02:18
  • The first ends up with the same error. The second one works (you missed a : after the first std) and gives me the length of this string. – chrstnwhlrt Mar 01 '13 at 02:56
  • The third version works! But will I have to access every method in that way? – chrstnwhlrt Mar 01 '13 at 03:07
  • Edited in the first post, there must be a way to access these methods without casting, the compile has no problem with that, just eclipse keeps claiming.. – chrstnwhlrt Mar 01 '13 at 03:12
  • I'm wondering if there is a setting that can be tweaked. It looks like Eclipse can't tell a function or class's API without the type spelled out exactly in the code. I hope there's another way for you. – Joel Mar 01 '13 at 03:13
  • I had to enable the c++11, therefore the thread method in the demo code. There must be anybode out there, that is using eclipse (Juno) with c++11 (gcc4.7//ubuntu12.10) and has no problems in using vectors and threads (std)?!! – chrstnwhlrt Mar 01 '13 at 03:16
0

The solution found:

I downloaded 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. To use thread I also added -pthread to the linker flags. With this steps the code could be compiled, but eclipse marks the thread still as unknown. 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. Deselect all except CDT GCC Builtin Compiler Settings. Then untag Share settings entries … . Add the option -std=c++11 to the text box called Command to get compiler specs.

Found here.

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.

Thanks for all your help!

Community
  • 1
  • 1
chrstnwhlrt
  • 331
  • 1
  • 4
  • 18