unknow error on class

hello.
to learn good classes, I wrote a draft of the program to simulate a telephone book.
the program compiles but crashes. I did debug, and I reported a problem with the function __ atomic (?). ( segment fault )
never having had a tutor or a teacher, I count on you for help.
thanks in advance.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <string>

using namespace std;



class phoneBook
{
    private:

        string n_phone;
        string name_surname;
        string Contatto;
        int size = 10;
        string* contact = new string[ size ];

    public:

               phoneBook( string, string, string );   //constructor
        string t_n_phone = '\0';              //temporary var
        string t_name_surname = '\0';
        string t_contact = '\0';
        string temp_n_phone();
        string temp_name_surname();
        string temp_contact();

        int counter = 0;

        bool value_temp_contact( string );
        void registry_contact( string );
        void printer();
} ;

inline phoneBook::phoneBook( string t_n_ph , string t_nm_srnm , string t_cntct )
{
    n_phone = t_n_ph;
    name_surname = t_nm_srnm;
    size = counter;
    Contatto = t_cntct;
}

inline string phoneBook::temp_n_phone()
{
    string t_n_prefix_int;
    string t_n_prefix_oper;
    string t_number;

    cout << " enter the new number, by entering: \n 1) country code -> enter; \n 2) prefix operator-> enter; \n 3) number-> enter " << endl;

    getline( cin, t_n_prefix_int );
    getline( cin, t_n_prefix_oper );
    getline( cin, t_number );

    return ( t_n_phone = t_n_prefix_int + " " + t_n_prefix_oper + " " + t_number );
}

inline string phoneBook::temp_name_surname()
{
    string t_name;
    string t_surname;

    cout << " enter the name & surname, by entering: \n 1) name -> enter; \n 2) surname -> enter; \n " << endl;

    getline( cin, t_name );
    getline( cin, t_surname );

    return ( t_name_surname = t_name + " " + t_surname );
}


inline string phoneBook::temp_contact()
{
    return ( t_contact = temp_name_surname() + temp_n_phone() );
}


inline bool phoneBook::value_temp_contact( string t_cntct )
{
    for (int i = 0; i < counter; i++)
    {
        if ( contact[i] == t_cntct )
        {
            cout << " this contact existing on phonebook " << endl;
            return 0;
        }
    }
    return 1;
}

inline void phoneBook::registry_contact( string t_cntct)
{
    counter++;
    contact[counter] = t_cntct;
    cout << " the contact now is on phonebook " << endl;
}

inline void phoneBook::printer()
{
    for (int i = 1; i <= counter; i++)
        cout << contact[i] << endl;
}


int main()
{
    class phoneBook c (c.t_n_phone,c.t_name_surname,c.t_contact);
    char answer;

    cout << " dou you want to additing new contact ? " << endl;
    cin >> answer;

    if (( answer == 'y') || ( answer == 'Y'))
    {
        while ( ( answer == 'y') || ( answer == 'Y'))
        {
            //c.phoneBook(c.t_n_phone,c.t_name_surname,c.t_contact);
            c.temp_n_phone();
            c.temp_name_surname();
            c.temp_contact();
            c.value_temp_contact(c.t_contact);

            if ( c.value_temp_contact(c.t_contact) )
            {
                c.registry_contact(c.t_contact);
            }

            c.printer();

            cout << " another additing? " << endl;
            cin >> answer;

        }
    }

    cout << "Hello world!" << endl;
    return 0;
}



#0 0040541A	__gnu_cxx::__atomic_add(int volatile*, int) () (??:??)
#1 0044E043	std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () (??:??)
#2 00000001	?? () (??:??)
#3 0022FDCC	?? () (??:??)
#4 004010FD	__mingw_CRTStartup () (??:??)
#5 E151F418	?? () (??:??)
#6 003E2B20	?? () (??:??)
#7 ??	?? () (??:??)
closed account (Dy7SLyTq)
first of all... why are you using inline on functions if they aren't? secondly we arent in the c days anymore. you dont need to use class when declaring an object (line 107). thirdly you are initializing it in the constructor with values that havent been initialized.
@..secondly we arent in the c days anymore...

can you explain this please. dont understand.
closed account (Dy7SLyTq)
in c, you had to do what you did with the class. in c++:
1
2
3
4
5
6
7
8
9
class whatev
{
     //... member stuff...
};

int main()
{
     whatev myobject;
}
if i write:

 
phoneBook c; // my object 


i have this error with compiler


C:\Documents and Settings\Administrator\Documenti\PROGRAMMI C++\try class action\main.cpp|107|error: no matching function for call to 'phoneBook::phoneBook()'|
closed account (Dy7SLyTq)
thats because you need to make a constuctor that takes no arguements
I'm sorry, but what can deduce that the manufacturer does not need arguments?

i have changed my code and compile but crashed


1
2
3
4
5
6
7
inline phoneBook::phoneBook()
{
    n_phone = t_n_phone;
    name_surname = t_name_surname;
    size = counter;
    Contatto = t_contact;
}


#0 00409138	__cxa_throw () (??:??)
#1 004054B5	std::__throw_logic_error(char const*) () (??:??)
#2 00473F98	typeinfo for std::__timepunct<wchar_t> () (??:??)
#3 0040D744	std::range_error::~range_error() () (??:??)
#4 0047EA40	(anonymous namespace)::messages_c () (??:??)
#5 003E2520	?? () (??:??)
#6 003E25B4	?? () (??:??)
#7 0040D7AC	std::logic_error::~logic_error() () (??:??)
#8 ??	?? () (??:??)



edit: basic_string::S_construct null not valid
Last edited on
closed account (Dy7SLyTq)
first of all its a constructor. secondly you arent passing it any arguments so it has never heard of those variables you are trying to make equal. i would suggest going through another class tutorial
@DTScode:

I solved the problem. I initialized temporary strings, erroneously, worthless. Therefore, the code now works and does not crash.
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
    public:
 
               phoneBook();   //constructor
        string t_n_phone = {"0"};              //temporary var
        string t_name_surname = {"0"};
        string t_contact = {"0"};
        string temp_n_phone();
        string temp_name_surname();
        string temp_contact();
 
        int counter = 0;
 
        bool value_temp_contact( string );
        void registry_contact( string );
        void printer();
} ;
 
inline phoneBook::phoneBook()
{
    n_phone = t_n_phone;
    name_surname = t_name_surname;
    size = counter;
    contact[0] = t_contact;
}



has just a bug I do not understand:

the first cycle does not read the statement at line 53,

 
getline(cin,t_number);


but the next cycle, everything falls into place. why?


Edit: i use inline function, because all function of class are inline but if i define the functions out of class, this is not true. Then i define inline functions
Last edited on
Topic archived. No new replies allowed.