mardi 6 juin 2017

Splitting a vector of objects in c++

I know my message is not properly formattd, but I'm new so please help me anyway. I have two classes: Father and Son. In Son I have an int and a string. In father i have a vector of son objects. How can I access just the part of the vector composed by integers and/or the part just composed by strings? Thank you very much!


CLASS SON.H

#ifndef SON_H
#define SON_H
#include <iostream>
using namespace std;

class Son
{
    public:
        Son() {}
        Son(int first, string second);
        virtual ~Son();
        int getFirst() const;
        string getSecond() const;
    private:
        int _first_parameter;
        string _second_parameter;
};

#endif // SON_H


CLASS SON.CPP

#include "son.h"

Son::Son(int first, string second)
{
    _first_parameter=first;
    _second_parameter=second;
}

Son::~Son()
{
    //dtor
}

int Son::getFirst() const
{
    return _first_parameter;
}

string Son::getSecond() const
{
    return _second_parameter;
}


CLASS FATHER.H

#ifndef FATHER_H
#define FATHER_H
#include <vector>
#include "son.h"

class Father
{
    public:
        Father() {}
        virtual ~Father();
        void filling();
    private:
        vector<Son> _access_vector;
        void _printIntegers(vector<int> v);
        void _printStrings(vector<string> v);
};

#endif // FATHER_H


CLASS FATHER.CPP

#include "father.h"

Father::~Father()
{
    //dtor
}

void Father::filling()
{
    int temp_first_param;
    string temp_second_param;
    cout<<"Insert integer: "<<endl;
    cin>>temp_first_param;
    cout<<"Insert text"<<endl;
    cin>>temp_second_param;
    _access_vector.push_back(Son(temp_first_param, temp_second_param));
    _printIntegers(_access_vector.getFirst());                       /// Here I want to take just the vector of integers
    _printStrings(_access_vector.getSecond());                       /// Here I want to take just the vector of strings
}

void Father::_printIntegers(vector<int> v)
{
    for(unsigned i=0; i<v.size(); ++i)
        cout<<v[i]<<endl;
}

void Father::_printStrings(vector<string> v)
{
    for(unsigned i=0; i<v.size(); ++i)
    {
        cout<<v[i]<<endl;
    }
}


COMPILER ERRORS:

||=== Build: Debug in Vector_access_experiment (compiler: GNU GCC Compiler) ===| C:\Users\Alessandro\Documents\CodeBlocks\Vector_access_experiment\src\father.cpp||In member function 'void Father::filling()':| C:\Users\Alessandro\Documents\CodeBlocks\Vector_access_experiment\src\father.cpp|17|error: 'class std::vector' has no member named 'getFirst'| C:\Users\Alessandro\Documents\CodeBlocks\Vector_access_experiment\src\father.cpp|18|error: 'class std::vector' has no member named 'getSecond'| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Aucun commentaire:

Enregistrer un commentaire