-1

I’m trying to learn c++ and now i have to face the implementation of a program which ask me to create a list of geeks and a list of projects to work on. For each geek I have to store his id number and his salary, and for each project I have to store the name of it and his Id number. Then it asks me to associate a specific geek to a specific project.

So my ideas is create a geek.h with information of geek class and the set/get function , than create a project.h with information of project class and set/get function ,and than i would like to implement a addinformation function into a main.cpp and another function to link a specific geek to a project . now i'm struggling a lot here is my partial code

#include<iostream>
#include<vector>
#include<list>
#include<string>
//geek.h
using std::cout;
using std::cin;
using std::vector;
using std::list;
using std::string;
class geek
{
  friend class project;
  private:
  int id_geek;
  double hour_salary;
  public:
      geek(int i,double  h) : id_geek{i},hour_salary{h} {}
      void setid(int id)
  {
      id_geek=id;
  }
  void seths(double hs)
  {
      hour_salary=hs;
  }
  int getid()
  {
      return id_geek;
  }
  double geths()
  {
     return hour_salary;
  }
};

#include<iostream>
#include<vector>
#include<list>
#include<string>
#include<iterator>
#include"geek.h"
//project.h
using std::iterator;
using std::vector;
using std::list;
using std::string;

class project: public geek
{
    friend class geek;
    private:
    int id_project;
    string project_name;
    double total_amount;
    double delivery_date;
    list<project> projectlist;
    public:
    project(int id,string pn,double ta,double dd,int idg,double hr) :id_project{id},project_name{pn},total_amount{ta},delivery_date{dd},geek(idg,hr) {}
    void setid(int id)
    {
        id_project=id;
    }
    void setpn(string pn)
    {
       project_name=pn;
    }
    void setta(double ta)
    {
        total_amount=ta;
    }
    void setdd(double dd)
    {
        delivery_date=dd;
    }
    int getid()
    {
        return id_project;
    }
    string getpn()
    {
        return project_name;
    }
    double getta()
    {
        return total_amount;
    }
    double getdd()
    {
        return delivery_date;
    }
};


#include<iostream>
#include<vector>
#include<list>
#include<string>
#include"project.h"
#include"geek.h"
//fantaware.cpp
using std::vector;
using std::list;
using std::string;

int main()
{
list<geek> geeklist;
list<project> projectlist;
int id ;
double salary;
  void Addgeekinfo(geek g)  
  {
     cout <<"insert geek id:";
     cin >> id;
     cout<<"insert geek salary;";
     cin>>salary;

     (now how do i use geeklist.push-front(?) in order to put elements into the list??)
     (ll have the same prob with aggproject info that's why i did nit read it yet)

     (how can i create a function to link a geek to a project) 
  }
}

`

`

  • 1
    you cannot define a function inside `main`. What happened when you tried to push an element to the list? – 463035818_is_not_an_ai Jun 11 '21 at 20:11
  • so i ll add the function into the geek.h but my prob is still , how can i push the element into the list after i did the 2 cin input ? – Antonio Aversano Jun 11 '21 at 20:15
  • you want to call `push_front` so why don't you give it a try? – 463035818_is_not_an_ai Jun 11 '21 at 20:20
  • Note: Reconsider `project` deriving from `geek`. This means `project`s can be used as if they were `geek`s and that doesn't make much sense. For example, you can add `project`s to `geeklist`. They'd be [sliced](https://stackoverflow.com/questions/274626/what-is-object-slicing), but you can do it and the compiler won't complain because the inheritance relationship says it's OK. – user4581301 Jun 11 '21 at 20:21
  • i wanna give a try to push_front but idk what do i have to put inside the () of geeklist.push_front() do i have to create a void Addgeek( geek o) and than push the o object? – Antonio Aversano Jun 11 '21 at 20:23
  • i'm asking cuz i need some code example on how to do this – Antonio Aversano Jun 11 '21 at 20:24
  • 1
    [cppreference](https://en.cppreference.com/w/cpp/container/list) generally contains simple examples of near everything provided in the standard libraries. Consider it, or similar sites (as if such a thing exists), and of course actual reference books and materials. – WhozCraig Jun 11 '21 at 20:35
  • Yeah I’m reading a lot of simple example but they don’t fit my case like , if I have a list L, and an int a, i ll just need to do push_front(a) to add, but what about th list L , and when I have an int a , and a string b? What do I have to push ? – Antonio Aversano Jun 11 '21 at 20:41

1 Answers1

1

There are some odd things in your code. It looks strange that project inherits from geek and is a friend of geek. Thats not good design and probably you dont need either. project contains a list<project>, thats very odd as well. What is a project? Is it a single project or is it a list of projects? Decide for one and name it accordingly, it cannot be both. Also, you cannot define a function inside a function.

On the other hand, there are also some things that look rather positive in your code: You used the member initialization list, thats a +1! Also you don't use using namespace std; but only have using declarations for things you are actually using. Though having those in headers can be problematic. Not as much as using namespace std; but still any code that includes those headers also includes those using declarations.

Your code is a bit unwieldy, so I allowed myself to reduce it a lot. To push an element to the list you either need to first create an element and then push it (push_front or push_back), or use emplace_front / emplace_back to construct the element in place:

#include<iostream>
#include<list>

struct geek {
  int id_geek;
  double hour_salary;
  geek(int id_geek,double hour_salary) : id_geek(id_geek),hour_salary(hour_salary) {}
};

int main() {
    int id ;
    double salary;
    std::cout <<"insert geek id:";
    std::cin >> id;
    std::cout<<"insert geek salary;";
    std::cin>>salary;

    std::list<geek> geek_list;
    geek_list.emplace_back(id,salary);

    geek g{id,salary};
    geek_list.push_front(g);
}

You also might want to provide an overload for operator<< and operator>> so you can write the same more compact:

geek g;
std::cin >> g;
geek_list.push_front(g);

For details I refer you to this answer: https://stackoverflow.com/a/4421719/4117728

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185