Home > OS >  I cannot pass arguments to my class in c
I cannot pass arguments to my class in c

Time:11-07

I’m trying to pass an argument to my class but it’s not working

the #includes

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif

the class Name and age are vectors Don’t mind the function called oldP

    class user{
        public:
        vector<string>Name;
        vector<int>Age;

    
    void outP() {

        for (unsigned int  i = 0; i < Name.size(); i  )
        {

            cout << Name[i] << " , ";

        }
    }
    user(vector<string> name, vector<int> age) {
        Name = name;
        Age = age;
    }

    


};

This is the function that uses the name and age and tries to pass it into the class

    int holder() {
    string _name;
    int _age;

    int f = 0;



    while (true)
    {
        f  ;

        cout << "enter name :";
        cin>>_name;

        cout << endl;

        cout << "enter age :";
        cin >> _age;



        if (f == 6)
        {
            break;

        }

    }

    user user1 = user(_name ,_age);


    
    return 0
}

The program partially runs for when the function is called it breaks

CodePudding user response:

Your constructor takes arguments of type vector<string> and vector<int> but you are trying to pass in values of type string and int. Since there is no implicit conversion from a given type T to a type vector<T>, you'll need to rectify that mismatch -- either change the types that constructor accepts, or change the types of the variables that you are passing to the constructor.

Based on the names of the variables, my guess is you want to get rid of vector entirely... unless there is some reason why a given user should be allowed have multiple names and multiple ages?

  • Related