4

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

Community
  • 1
  • 1
Girardi
  • 2,734
  • 3
  • 35
  • 50

4 Answers4

27

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
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
12
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;
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • If I declare neighbors as you said, the method `AddNeighbors` in class `Site` will remain correct? Thanks for the answer :) – Girardi Feb 20 '14 at 20:56
  • 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 >::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 – Girardi Feb 20 '14 at 21:08
  • @Girardi Oh sorry, I misunderstood your previous question. You need to do `neighbors.push_back(std::ref(site))`. – Joseph Mansfield Feb 20 '14 at 21:09
  • it says that `ref` is not a member of `std` – Girardi Feb 20 '14 at 21:13
  • I am sorry, I haven't notice the `header` in the link you referenced =) – Girardi Feb 20 '14 at 21:18
7
vector<Site> myStack();

This is wrong. Lose the ().

You're declaring a function, not a vector.

Just write:

vector<Site> myStack;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

You could use:

vector<Site> myStack;
myStack.resize(100); //will create 100 <Site> objects