Pointing class property to another class with vectors

Posted by jmclem on Stack Overflow See other posts from Stack Overflow or by jmclem
Published on 2010-05-20T16:19:14Z Indexed on 2010/05/20 16:20 UTC
Read the original article Hit count: 165

Filed under:
|
|
|
|

I've got a simple class, and another class that has a property that points to the first class:

#include <iostream>
#include <vector>

using namespace std;

class first{
public:
    int var1;
};

class second{
public:
    first* classvar;
};

Then, i've got a void that's supposed to point "classvar" to the intended iteration of the class "first".

void fill(vector<second>& sec, vector<first>& fir){
    sec[0].classvar = &fir[0];
}

Finally the main(). Create and fill a vector of class "first", create "second" vector, and run the fill function.

int main(){

    vector<first> a(1);
    a[0].var1 = 1000;

    vector<second> b(1);

    fill(b, a);

    cout << b[0].classvar.var1 << '\n';

    system("PAUSE");
    return 0;
}

This gives me the following error:

1>c:\...\main.cpp(29) : error C2228: left of '.var1' must have class/struct/union
1>        type is 'first *'

And I can't figure out why it reads the "classvar" as the whole vector instead of just the single instance. Should I do this

cout << b[0].classvar[0].var1 << '\n';

it reads perfectly.

Can anyone figure out the problem? Thanks in advance

© Stack Overflow or respective owner

Related posts about c++

Related posts about vector