Compiler error in dynamic array of structures declaration

Hey all! I'm currently learning C++ using Stephen Prata's C++ Primer Plus and loving the adventure so far. Well.. for the most part. Anyway, one of the programming exercises for the chapter I just finished (on branching statements and logical operators) ask you to create a structure that holds the member information of member of the Benevolent Order of Programmers. This structure is supposed to include the members' names, titles, nicknames, and preference for which of those 3 they would like displayed. Then you are supposed to display a menu that allows you to choose how to display the information, either by name, title, nickname, or the member's preference. Anyway, I went a step further and also added a bit to allow the user to input that information manually, instead of through a structure initialization. This also included the user defining how many records they wanted to add. Anyway, here's the code I got so far. I'm sure there's little syntax errors in it, which I will clean up after I get the structure working properly:

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
#include <iostream>
#include <string>

struct record
{
    std::string str_Name;
    std::string str_Title;
    std::string str_Nickname;
    int n_Pref;
};

int main()
{
    std::cout << "How many records would you like to enter into the\n"
              << "BOP's database?\n"
              << std::endl;

    int n_NumRecs = 0;

    std::cin  >> n_NumRecs;

    record* p_Records;
    p_Records = new record[n_NumRecs];

    for (int i = 0; i < n_NumRecs; i++)
    {
        std::cout << "Enter the name for record " << i << ":";
        getline(std::cin, p_Records[i].str_Name);
        std::cout << "Enter the title for record " << i << ":";
        getline(std::cin, p_Records[i].str_Title);
        std::cout << "Enter the nickname for record " << i << ":";
        getline(std::cin, p_Records[i].str_Nickname);
        std::cout << "Enter the persons preference for name display\n (0 = Full Name, 1 = Title, 3 = Nickname): ";
        std::cin  >> p_Records[i].n_Pref;
    }

    std::cout << "Data entry complete.\n";
    showmenu();

    char c_MenuChoice;
    std::cin >> c_MenuChoice;

    while (c_MenuChoice != q)
    {

        switch (c_MenuChoice)
        {
            case 'a' :
            {
                    for (int i = 0; i < n_NumRecs; i++)
                        std::cout << p_Records[i].str_Name;
            }

            case 'b' :
            {
                    for (int i = 0; i < n_NumRecs; i++)
                        std::cout << p_Record[i].str_Title;
            }

            case 'c' :
            {
                    for (int i = 0; i < n_NumRecs; i++)
                        std::cout << p_Record[i].str_Nickname;
            }

            case 'd' :
            {
                for (int i = 0; i < n_NumRecs; i++)
                {
                    switch (p_Record[i].n_Pref)
                    {
                        case 0 : std::cout << p_Record[i].str_Name;

                        case 1 : std::cout << p_Record[i].str_Title;

                        case 2 : std::cout << p_Record[i].str_Nickname;
                    }
                }
            }

            case 'q' :
                break;

            case default :
                std::cout << "That wasn't a valid option you butt-munching anal-dweller.";
        }

    showmenu();
    std::cin >> c_MenuChoice;

    }

    std::cout << "Bye!";
    delete p_records;
    return 0;
}

void showmenu()
{
    std::cout << "Benevolent Order of Programmers Members Report\n"
              << "a. Display by name             b. Display by title\n"
              << "c. Display by nickname         d. Display by preference\n"
              << "q. Quit\n";
}


The problem I am running into is that on line 23 the compiler throws an error stating :

C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|23|note: synthesized method 'record::record()' first required here |

I tried searching for this on google, but there were precious few results actually related to programming, and of those that were, most were talking about class constructors/destructors. IE, stuff over my head. From what I can see, I followed the correct method to declare the pointer for the dynamic array and all in this code:

1
2
record* p_Records;
p_Records = new record[n_NumRecs];


so I'm not sure what's going on. Any help is appreciated. And if needed I'll add comments where appropriate. I try to make my code as readable as possible, but I imagine some folks are used to other styles. :)
Last edited on
What you quoted is only a part of the message which shows what line of code it refers to. What's the full text of that message? Does it say something like

In constructor 'record::record()':
warning: 'record::str_Name' should be initialized in the member initialization list
note: synthesized method 'record::record()' first required here

If so, you can ignore the message.
Last edited on
Naw, nothing like that appeared. Here's the full batch of build messages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp||In constructor 'record::record()':|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|4|warning: 'record::str_Name' should be initialized in the member initialization list [-Weffc++]|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|4|warning: 'record::str_Title' should be initialized in the member initialization list [-Weffc++]|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|4|warning: 'record::str_Nickname' should be initialized in the member initialization list [-Weffc++]|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|4|warning: 'record::n_Pref' should be initialized in the member initialization list [-Weffc++]|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp||In function 'int main()':|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|25|note: synthesized method 'record::record()' first required here |
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|59|error: 'p_Record' was not declared in this scope|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|65|error: 'p_Record' was not declared in this scope|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|72|error: 'p_Record' was not declared in this scope|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|72|warning: switch missing default case [-Wswitch-default]|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|86|error: expected primary-expression before 'default'|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|86|error: expected ':' before 'default'|
C:\Users\Raezzor\Desktop\Coding Projects\6.4\main.cpp|96|error: 'p_records' was not declared in this scope|
||=== Build finished: 7 errors, 5 warnings ===|
Your output is exactly what I wrote.

You can ignore those warnings: you're using a gcc compiler with the option -Weffc++, which attempts to automatically detect some of the programming issues discussed in the book "Effective C++" by Scott Meyers. This mode is not particularly smart and flags a lot of perfectly valid code.
Hah ya so it was the same. The problem is it throws an error at line 25 preventing it from compiling and not just a warning. So would turning off the -Weffc++ flag be ok?
There is no error on line 25, it's only a note that provides additional information for the warnings for line 4. The errors that prevent compilation are on lines 59, 65, 72, 86, and 96.
Good lord, all this learning is killing my reading comprehension somehow! Yup, fixed those and it compiled/ran! Thanks for the help Cubbi.
Note that delete p_Records; is also an error: although it usually compiles quietly, it is a certain memory leak in this case, and possible memory corruption. Either use delete[] p_Records; or simply declare p_Records as a vector<record>.
Ahh good catch. Ya, I meant to use the first. While a small program so not a big deal here, definitely want to do it right from the getgo. :)

Question though. Why is it delete[] p_Records; and not delete p_Records[];

Hmm, actually now that I look at it I see why the 2nd doesn't make sense since it's actually the pointer to the array.
Last edited on
Topic archived. No new replies allowed.