Vectors

I have written my first program using vector, however I can't get it to compile. Lines using vector, like:
std::vector<sf::Image> iPlants(60);
return the errors:
error: expected identifier before numeric constant|
error: expected ',' or '...' before numeric constant|

Also, I also had problems with this line:
std::vector<int> Evolution::ReturnX()
I got a 'invalid declarator before 'Evolution''.

How can I solve this problem?
Last edited on
Errr... Bump.
Can you get a simple little program to work with vectors?

The errors suggest <vector> hasn't been included, esp. the second one.
It indeed hadn't been included... however when I did there was no change.

I wrote a small program and it compiled...
Bumping this again...
We'll have to see some more code. Please show the files (or a minimal representation of them) that produce the above errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdlib>
#include <vector>

using namespace std;

class Something
{
    vector<int> Stuff (5);

    for(int i = 0; i < 5; i++)
    {
        Stuff[i] = i;
    }
}

int main()
{
     return 0;
}

You can't have code just in the middle of a class like that. It has to be part of a method.

This will work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <vector>

using namespace std;

class Something
{
public:
    void SomeFunc()
    {
        vector<int> Stuff (5);

        for(int i = 0; i < 5; i++)
        {
            Stuff[i] = i;
        }
    }
};

int main()
{
     return 0;
}
Last edited on
I thought that creating a vector inside a class was no different from doing something like

1
2
3
4
class Blah
{
       int x, y;
};


Well, anyways it seems that I will have to correct some things in my program... Thanks.
Last edited on
That is correct, but you can't have a for loop inside a class like that, you need to have it inside a function instead like shacktar shown.
That was an oversight of mine. But if I can create a vector in a class why this simple code won't run?

1
2
3
4
5
6
7
8
9
#include <cstdlib>
#include <vector>
class Something
{
    std::vector<int> x (40);
};
int main()
{
}


If I put it inside a function or something I get no error... But I guess that once the program leaves the method's context that vector will be lost, right?
You can only initialize static const integral members within a class. For objects, you can only declare them. For your vector, you would have to declare it as a member without explicitly using a constructor (thus, using the default constructor).

This will do what you intend in the above example:

1
2
3
4
5
6
7
8
9
10
class Something
{
    std::vector<int> x;

public:
    Something()
    {
        x.resize(40);
    }
};
Oh, I see now. Thank you for clearing that up.
Or equivalently, using the constructor's initializer list

1
2
3
4
5
6
7
8
9
10
11
class Something
{
    const static int count = 40;

    std::vector<int> x;

public:
    Something() : x(count)
    {
    }
};
Last edited on
Topic archived. No new replies allowed.