How to create a vector of class objects in C++?

110,851

Solution 1

vector<Site> myStack();

This is actually a function declaration. The function is called myStack and it returns a vector<Site>. What you actually want is:

vector<Site> myStack;

The type of neighbours at the moment will store copies of the objects, not references. If you really want to store references, I recommend using a std::reference_wrapper (rather than using pointers):

vector<reference_wrapper<Site>> neighbors;

Solution 2

How to create a vector of class objects in C++?

Start with something simpler so you can get the hang of it.

First, create a vector of primitive ints:

#include <vector>
#include <iostream>
using namespace std;
int main(){
  vector<int> sites(5);
  sites.push_back(5);
  for(int x = 0; x < sites.size(); x++){
    cout << sites[x];
  }
  cout << endl;
  return 0;
}

Compiling it:

g++ -o test test.cpp

Running it:

./test
000005

Create a vector of class objects in a similar way as above:

#include <iostream>
#include <vector>
using namespace std;

class Site {
public:
    int i;
};

int main() {
    vector<Site> listofsites;
    Site *s1 = new Site;
    s1->i = 7;
    Site *s2 = new Site;
    s2->i = 9;
    listofsites.push_back(*s1);
    listofsites.push_back(*s2);
    vector<Site>::iterator it;
    for (it = listofsites.begin(); it != listofsites.end(); ++it) {
        cout << it->i;
    }
    return 0;
}

Which should print:

79

Solution 3

vector<Site> myStack();

This is wrong. Lose the ().

You're declaring a function, not a vector.

Just write:

vector<Site> myStack;

Solution 4

You could use:

vector<Site> myStack;
myStack.resize(100); //will create 100 <Site> objects
Share:
110,851
Girardi
Author by

Girardi

Researcher in Computational Neuroscience working in Stochastic data-driven modeling.

Updated on July 09, 2022

Comments

  • Girardi
    Girardi almost 2 years

    I am trying to create a simple stack using vector in C++.

    Here is the code:

    #include <vector>
    
    class Site
    {
        public:
            int i; // site position i (x-axis)
            int s; // site state
            vector<Site> neighbors;
            Site(void);
            Site(int ii, int ss);
            void AddNeighbor(Site &site);
    };
    Site::Site()
    {
        i = -1;
        s = -1;
        vector<Site> neighbors;
    }
    Site::Site(int ii, int ss) 
    {
        i = ii;
        s = ss;
    }
    void Site::AddNeighbor(Site &site)
    {
        neighbors.push_back(site);
    }
    
    void testStack()
    {
        int tot = 600;
        vector<Site> myStack();
        int i = 0;
        while (i < tot)
        {
            Site site(i, 1);
            myStack.push_back(site);
            i++;
        }
    
        i = 0;
        while (i < tot)
        {
            Site *site = myStack.back();
            myStack.pop_back();
            cout << site->i << site->s << endl;
            i++;
        }
    }
    

    Compiler errors:

    ising_wolff.cpp: In function ‘void testStack()’: ising_wolff.cpp:373:17: error: request for member ‘push_back’ in ‘myStack’, which is of non-class type ‘std::vector()’ myStack.push_back(site); ^ ising_wolff.cpp:380:30: error: request for member ‘back’ in ‘myStack’, which is of non-class type ‘std::vector()’ Site *site = myStack.back(); ^ ising_wolff.cpp:381:17: error: request for member ‘pop_back’ in ‘myStack’, which is of non-class type ‘std::vector()’ myStack.pop_back();

    What do these errors mean?

    Here are some sites I have looked at:

    1) Creating objects while adding them into vectors

    2) push_back causing errors in C

    3) how to create vectors of class object

  • Girardi
    Girardi about 10 years
    If I declare neighbors as you said, the method AddNeighbors in class Site will remain correct? Thanks for the answer :)
  • Girardi
    Girardi about 10 years
    when I use std::reference_wrapper, like you proposed, compiler gives me the error: In member function ‘void Site::AddNeighbor(Site&)’: ising_wolff.cpp:38:29: error: no matching function for call to ‘std::vector<std::reference_wrapper<Site> >::push_back(Site&)’ in neighbors.push_back(site);. And then prints a lot of "candidates" to replace it... The list is long that it is impossible to read
  • Joseph Mansfield
    Joseph Mansfield about 10 years
    @Girardi Oh sorry, I misunderstood your previous question. You need to do neighbors.push_back(std::ref(site)).
  • Girardi
    Girardi about 10 years
    it says that ref is not a member of std
  • Girardi
    Girardi about 10 years
    I am sorry, I haven't notice the header in the link you referenced =)