PDA

View Full Version : STL libraries



tom1972
26th June 2012, 09:22
Good morning to everyone.... I use LINUX-FEDORA
In Qt CREATOR.....
I want to compile a console application in C++.
I need to use STL library ....

I write:

#include <vector>
/*
I even used and this
#include <QVector>
*/
........
int main()
{
vector<int> v;
/*
I even used and this
std::vector<int> v;
*/
........
vector<int>::iterator it = v.begin();
........
}

The compiler sais:
'iteraror' is not a member of 'std::vector<int>'
expected ';' before 'it'
'it' was not declared in this scope

The same things, with g++ (in LINUX), devcpp (in WINDOWS)
Where is the STL library in Qt CREATOR ??
Maybe I must install in LINUX, the STL from somewhere ??
Thanks in advance ...!!!!

yeye_olive
26th June 2012, 12:43
You haven't posted your whole code. For example, it appears that the compiler accepts the declaration "vector<int> v;", therefore you must have put a "using" directive somewhere. Is that right?

It seems that the error is unrelated to Qt. Can you confirm that you can reproduce it on a minimal program without involving Qt (be it libraries or tools, such as QtCreator) at all?

tom1972
26th June 2012, 13:24
My friend I discovered something
In this code IT WORKS !!!

// -------------------------------------------
#include <iostream>
#include <vector>

using namespace std;

int main()
{
cout << endl;

vector<int> v1;
for(int i=1; i<10; i+=2)
{
v1.push_back(i);
}

for(int i=1; i<8; i+=2)
{
cout << v1[i] << endl;
}

cout << endl;
return 0;
}
// -------------------------------------------

But the problem appears only if I use iterators .....
I googled and I found that if I use typedef, the problems stops ....
Look these ....

// -------------------------------------------
#include <iostream>
#include <vector>
//LOOK THIS ....
typedef std::vector<int>::iterator IT;

using namespace std;

int main()
{
cout << endl;

vector<int> v1;
for(int i=1; i<10; i+=2)
{
v1.push_back(i);
}

std::vector<int> v2(v1);

std::vector<int> v3;

for(int i=1; i<20; i+=2)
{
v3.push_back(3*i);
}

IT iter = v1.begin();

while(iter != v1.end())
{
cout << *iter;
iter++;
}

iter = v2.begin();

while(iter != v2.end())
{
cout << *iter;
iter++;
}

IT it1 = v3.begin();
IT it2 = v3.end();

vector<int> v4(it1+5, it2-2);

iter = v4.begin();

while(iter != v4.end())
{
cout << *iter;
iter++;
}


cout << endl;
return 0;
}

// -------------------------------------------

Very strange because
1. It needs "std::" allthough I wrote:
using namespace std;
2. all books I've read say that we use
vector<int>::iterator iter = v1.begin();
without typedef
3.Sorry but I am Greek and I did a lot of mistakes while writing ....

wysota
26th June 2012, 21:42
How is that all related to Qt?

tom1972
26th June 2012, 21:52
My friend I don't think it is related with qt...
It's about C++. I would be grateful to you if you can help me....
Thank you a lot for replying to me. I own to you ....
Why the iterators in <vector>, cannot recognized .....
Thanks in advance.... A C++ lover !!!!

wysota
26th June 2012, 22:34
Apparently the syntax of your program is not correct.

This builds just fine for me:

#include <vector>

int main() {
std::vector<int>::iterator iter;
return 0;
}

So does this:


#include <vector>

using namespace std;

int main() {
vector<int>::iterator iter;
return 0;
}

$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

tom1972
26th June 2012, 23:39
My friend thank you very much for your help...
I will note these you just wrote to me.... Thanks for everything !!