1

I'm trying to create an array of classes using a vector, but I think I'm getting the syntax wrong from instantiating the array. The error I'm getting is:

error: request for member 'setX' in objects[0], which is of non-class type 'std::vector'

#include <iostream>
#include <vector>

using std::cout;

class A {
    public:
        void setX(int a) { x = a; }
        int getX() { return x; }
    private:
        int x;
};

int main() {

    std::vector<A> *objects[1];

    objects[0].setX(5);
    objects[1].setX(6);

    cout << "object[0].getX() = " << objects[0].getX() << "\nobject[1].getX() = " << objects[1].getX() << std::endl;
}
1
  • 1
    By the way... By declaring the size with a 1... This will make the size 1... Not 2. It looks like you were thinking in vb Commented Aug 18, 2012 at 1:27

4 Answers 4

5
std::vector<A> objects; // declare a vector of objects of type A
objects.push_back(A()); // add one object of type A to that vector
objects[0].setX(5); // call method on the first element of the vector
Sign up to request clarification or add additional context in comments.

3 Comments

The answer could use a bit more explainatory text, but it's definitely correct so I don't really see what the downvotes are for.
@Grozz A is a class, not a function...So how is it being called in objects.push_back( A() )??
A() is the default constructor for an object of type A. It's a nameless temporary being passed into vector::push_back().
3

With an asterisk and a square brackets, you are declaring an array of pointers to vectors instead of a vector. With std::vector<T> you do not need square brackets or an asterisk:

std::vector<A> objects(2); // 2 is the number of elements; Valid indexes are 0..1, 2 is excluded

objects[0].setX(5); // This will work
objects[1].setX(6);

The reason the compiler thought that you were trying to call setX on a vector is that the square bracket operator, overloaded by the vector, is also a valid operator on an array or a pointer.

Comments

1

An array and std::vector are two completely different container types. An array is actually a fixed-size block of memory, where-as a std:vector object is a dynamic sequential container type, meaning it can be dynamically "grown" and "shrunk" at run-time, and the object itself manages the memory allocation of the objects it owns. Their apparent similarities are that both can access members in O(1) complexity and can use the bracket syntax for accessing members.

What you want is something like the following:

int main() 
{

    //make a call to the std::vector<T> cstor to create a vector that contains
    //two objects of type A
    std::vector<A> objects(2);

    //you can now access those objects in the std::vector through bracket-syntax

    objects[0].setX(5);
    objects[1].setX(6);

    cout << "object[0].getX() = " << objects[0].getX() << "\nobject[1].getX() = " << objects[1].getX() << std::endl;

    return 0;
}

2 Comments

if you however do not require your data structure to have a dynamic size, then use an array. Indexing will be faster. Especially if the size is set at compile time
The potential difference between indexing an array and a vector will be in the nanosecond range, and most often not important. Using a vector will save you from all kinds of other problems.
-1

here what you did is define an array with 1 element of type std::vector*, you may want to read more about vector and array first.

The correct way to define it is:

std::vector<A> objects(2);

or using pointers if that is what you intend to

std::vector<A*> objects(2);
objects[0] = new A();
objects[1] = new A();

4 Comments

That won't work ... you can't assign using operator= an A* to a std::vector<A*> object ... you have to insert it using push_back. You also end up with an extra layer of indirection, making this approach very inefficient...
@Jason of course I can, push_back is to add new element at the end, but I already have 2 elements in the vector, I just what to assign it a value.
Okay, if you think this works, then please explain why your code won't compile: ideone.com/Svx0O
@Jason sorry, my fault. there is a typo in my code: objects[2] should be objects() -> That is how we call vector's constructor. updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.