Problem with output

Hi everybody! I work at some program and don't know how to write correct to output structure.
Here is a code. Please, help me with it.
Program compiling. After execution it shows addresses, but I want value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <list>
#include <algorithm>
#include <string>

#define MAX_W 100

using namespace std;

double rm=3534.54, rh=3854.67;

struct sWorker
{
    string Name;
    int Id;
    double Avr_sal;
};

sWorker *Pers_c(string inName, int inId, double inAvr_sal)
{
    sWorker *p=new sWorker;
    p->Name=inName;
    p->Id=inId;
    p->Avr_sal=inAvr_sal;

    return p;
}

void Pers_d(sWorker *p)
{
    delete p;
}

int main()
{
    sWorker *Kr_T, *Dj_I, *Py_P, *Ku_I;
    list<sWorker*> wol;
    list<sWorker*>::iterator pw;
    wol.push_back(Kr_T=Pers_c("Krav Tar",'F',rm));
    wol.push_back(Dj_I=Pers_c("Djm Iv",'H',rh));
    wol.push_back(Py_P=Pers_c("Pyh Pav",'F',rm));
    wol.push_back(Ku_I=Pers_c("Kup Iv",'H',rh));

    for(pw=wol.begin(); pw!=wol.end(); pw++)
    {
        cout<<*pw<<endl;
    }
    
    Pers_d(Kr_T);
    Pers_d(Dj_I);
    Pers_d(Py_P);
    Pers_d(Ku_I);

    return 0;
}
Last edited on
First, use actual constructors/destructors. Those Per_c/Per_d functions you have there are pure evil.

And there's no "pl" anywhere in your code other than the line in which you print it. I assume you meant "pw" there, in which case it'd be

cout << **pw << endl;.
Can you explain deeper about "pure evil". It's better not to use them?
Why or what is better to use?
When I did cout<<**pw<<endl; my compiler showed very long mistake which begins:
error: no match for 'operator<<' in std::cout<<*pw.std::_List_iterator ...... and more
Can you explain deeper about "pure evil".

What you're implementing are a contructor and a destructor on a struct. Why not use a class? Implementing your own constructors and destructors on a struct are not a good idea because unlike regular constructors and destructors they don't get called automatically on creation or deletion. They rely on your code knowing to explicity call them. Great possibility of uninitialized values on instantiation and memory leaks on deletion. structs also don't provide data hiding. By default all members are public.

For example with your code, the following is perfectly legal:
 
  Sworker w;

W is uninitialized because the constructor is not called.

error: no match for 'operator<<' in std::cout<<*pw.std::_List_iterator

sWorker doesn't provide an overload for the >> operator. ostream (cout) doesn't know how to format a sWorker object.

This should solve both your problems:
1
2
3
4
5
6
7
8
9
10
11
12
class sWorker
{  string Name;
    int Id;
    double Avr_sal;
public:
    sWorker ();  // default constructor
    sWorker (string name, int id, double avgsal)  // explicit constructor
    { Name=name; Id=id; Avr_sal=avgsal; }
    ~sWorker {}  // destructor
    friend ostream & operator << (ostream & os, const sWorker & w)
    {  os << "name=" << w.Name << " id=" << w.Id << "Avg Sal=" << fixed << setprecision(2) << w.Avr_Sal << endl; } 
};


It's also generally not a good idea to create a list of pointers to dynamically allocated objects. Note that at the end of program, wol still contains pointers to objects which have now been deleted. You should at least do a
 
  wol.clear();  //  empty the list 


Better yet is to push the workers onto the list by value.
1
2
3
4
  list<sWorker> wol;  //  By value, not pointer
  
  wol.push_back(sWorker("Krav Tar",'F',rm));
... etc

No dynamic memory needed. No possible memory leaks to worrry about.
Last edited on
Topic archived. No new replies allowed.