My program is compiling even though it shouldn't. What flags should I be setting?

Codeblocks tends to often allow non-standard c++ code to compile by default. Usually, the assignments I submit compile in his compiler but this time it didn't and I got an F. I have these flags set:

-ansi
-Wall
-Wextra
-pendantic
-Wmissing-declarations
-Winit-self

but the code is still compiling without errors or warnings. The instructor said to also set these but I didn't because I couldn't find them.

-Wold-style-cast
-Woverloaded-virtual
-Wuninitialized


This is the code that didn't compile on his computer

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>
#include <iomanip> //for displaying table

using namespace std;

class Employee
{
    private:
        string name;
        int idNumber;
        string department;
        string position;

    public:

        //default constructor
        Employee()
        {
            name = "";
            idNumber = 0;
            department = "";
            position = "";
        }

        Employee(string n, int id, string dept, string pos)
        {
            name = n;
            idNumber = id;
            department = dept;
            position = pos;
        }
        Employee(string n, int id)
        {
            name = n;
            idNumber = id;
            department = "";
            position = "";
        }


        void setName(string n)
        {
            name = n;
        }
        void setIdNumber(int id)
        {
            idNumber = id;
        }
        void setDepartment(int dept)
        {
            department = dept;
        }
        void setPosition(string pos)
        {
            position = pos;
        }

        string getName()
        {
            return name;
        }
        int getIdNumber()
        {
            return idNumber;
        }
        string getDepartment()
        {
            return department;
        }
        string getPosition()
        {
            return position;
        }

};

//will make array of employees so they can be easily iterated through and displayed in table
const int NUM_EMPLOYEES = 3;

int main()
{

    Employee employees[NUM_EMPLOYEES];

    employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"};
    employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
    employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"};

    cout << "\n";
    cout << setw(15) << "Employee Name" << setw(15) << "ID Number" << setw(20) << "Department" << setw(20) << "Position";
    cout << "\n\n";


    for (int i = 0; i < NUM_EMPLOYEES; i++)
    {
         cout << setw(15) << employees[i].getName() << setw(15) << employees[i].getIdNumber() << setw(20)
              << employees[i].getDepartment() << setw(20) << employees[i].getPosition() << "\n";
    }

    cout << "\n";
    return 0;
}



Last edited on
Compiling with the options you said was set when you compiled the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
g++ -ansi -Wall -Wextra -pedantic -Wmissing-declarations -Winit-self employee.cpp

employee.cpp: In function ‘int main()’:
employee.cpp:87:74: warning: extended initializer lists only available with -
std=c++11 or -std=gnu++11 [enabled by default]
employee.cpp:87:74: warning: extended initializer lists only available with -
std=c++11 or -std=gnu++11 [enabled by default]
employee.cpp:88:60: warning: extended initializer lists only available with -
std=c++11 or -std=gnu++11 [enabled by default]
employee.cpp:88:60: warning: extended initializer lists only available with -
std=c++11 or -std=gnu++11 [enabled by default]
employee.cpp:89:67: warning: extended initializer lists only available with -
std=c++11 or -std=gnu++11 [enabled by default]
employee.cpp:89:67: warning: extended initializer lists only available with -
std=c++11 or -std=gnu++11 [enabled by default]
employee.cpp:89:67: error: no match foroperator=’ in ‘employees[2] = {"Joy 
Rogers", 81774, "Manufacturing, Engineer"}’
employee.cpp:89:67: note: candidate is:
employee.cpp:8:7: note: Employee& Employee::operator=(const Employee&)
employee.cpp:8:7: note:   no known conversion for argument 1 from ‘<brace-
enclosed initializer list>’ to ‘const Employee&’


No extra warnings when compiled with:
g++ -ansi -Wall -Wextra -pedantic -Wmissing-declarations -Winit-self -Wold-style-cast -Woverloaded-virtual -Wuninitialized employee.cpp

Compiling with additional option -std=c++11:
1
2
3
4
5
6
7
8
9
10
11
12
13
g++ -ansi -Wall -Wextra -pedantic -Wmissing-declarations -Winit-self -Wold-style-
cast -Woverloaded-virtual -std=c++11 -Wuninitialized employee.cpp

employee.cpp: In function ‘int main()’:
employee.cpp:89:67: error: no match foroperator=’ in ‘employees[2] = {"Joy
 Rogers", 81774, "Manufacturing, Engineer"}’
employee.cpp:89:67: note: candidates are:
employee.cpp:8:7: note: Employee& Employee::operator=(const Employee&)
employee.cpp:8:7: note:   no known conversion for argument 1 from ‘<brace-
enclosed initializer list>’ to ‘const Employee&’
employee.cpp:8:7: note: Employee& Employee::operator=(Employee&&)
employee.cpp:8:7: note:   no known conversion for argument 1 from ‘<brace-
enclosed initializer list>’ to ‘Employee&&’
Last edited on
I had to add the -std=c++11 to get the warnings to disappear (GCC 4.8.2). Otherwise even with your code commands, Smac89, it gave those warnings.
$ g++ -ansi -Wall -Wextra -pedantic -Wmissing-declarations -Winit-self -Wold-style-cast 
-Woverloaded-virtual -Wuninitialized -std=c++11 theperson01.cpp
Last edited on
What compilers are you using with CB? Here is my commands with GNU GCC 4.8.2 under Debian in CB and command line I get this:

$ g++ -ansi -Wall -Wextra -pedantic -Wmissing-declarations -Winit-self -Wold-style-cast 
-Woverloaded-virtual -Wuninitialized theperson01.cpp

theperson01.cpp: In function ‘int main()’:
theperson01.cpp:86:74: warning: extended initializer lists only available with -std=c++11 or 
-std=gnu++11 [enabled by default]
     employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"};
                                                                          ^
theperson01.cpp:86:18: warning: extended initializer lists only available with -std=c++11 or 
-std=gnu++11 [enabled by default]
     employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"};
                  ^
theperson01.cpp:87:60: warning: extended initializer lists only available with -std=c++11 or 
-std=gnu++11 [enabled by default]
     employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
                                                            ^
theperson01.cpp:87:18: warning: extended initializer lists only available with -std=c++11 or 
-std=gnu++11 [enabled by default]
     employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
                  ^
theperson01.cpp:88:69: warning: extended initializer lists only available with -std=c++11 or 
-std=gnu++11 [enabled by default]
     employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"};
                                                                     ^
theperson01.cpp:88:18: warning: extended initializer lists only available with -std=c++11 or 
-std=gnu++11 [enabled by default]
     employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"};
                  ^

$ g++ -ansi -Wall -Wextra -pedantic -Wmissing-declarations -Winit-self -Wold-style-cast 
-Woverloaded-virtual -Wuninitialized -std=c++11 theperson01.cpp


GCC Version output

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.8.2-14' --with-
bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-
languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-
suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --
without-included-gettext --enable-threads=posix --with-gxx-include-
dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-
clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-
unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-
browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-
home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-
jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-
dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch 
--with-arch-32=i586 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --
with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --
host=x86_64-linux-gnu --target=x86_64-linux-gnu

Thread model: posix
gcc version 4.8.2 (Debian 4.8.2-14) 
Last edited on
Just to add my 1 cent worth - It seems to me that your teacher wants C++98 code (hence the request for the -ansi flag), but you have C++11 code. So you might have to alter your code to comply with C++98. But your teacher should have told you all if that is the case.

I am guessing that specifying -std=c++11 overrides the -ansi flag.

Hopefully this helps a bit.
Did your teacher tell you what errors/warnings were that made him fail you? We are just speculating and fixing the error/warnings we are getting on our boxes for our compilers.
> Codeblocks tends to often allow non-standard c++ code to compile by default.

g++ and clang++ allow non-standard c++ code to compile by default. With these compilers, we need to explicitly force conformace with -std=c++11 and -pedantic-errors options.


> What flags should I be setting?

Compile your code with these flags set: -std=c++11 -Wall -Wextra -pedantic-errors


> My program is compiling even though it shouldn't.

Your program is correct; it should compile cleanly with a conforming C++ compiler


> Usually, the assignments I submit compile in his compiler but this time it didn't and I got an F.

Take it up with your teacher; demonstrate that your code conforms to the ISO standard for C++.


These three lines in your code require conformance with the current international standard for C++
1
2
3
    employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"};
    employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
    employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"}; 


echo ..........................
echo -e '\n\nstandard c++ (-std=c++11)\n------------------------------'
echo g++ && g++-4.8 -std=c++11 -Wall -Wextra -pedantic-errors main.cpp && echo 'g++ ok'
echo -e '\nclang++' && clang++ -std=c++11 -stdlib=libc++ -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && echo 'clang++ ok.'
echo -e '\nlegacy standard c++ (-std=c++98)\n------------------------------'
echo g++ && g++-4.8 -std=c++98 -Wall -Wextra -pedantic-errors main.cpp && echo 'g++ ok'
echo -e '\nclang++' && clang++ -std=c++98 -stdlib=libc++ -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && echo 'clang++ ok.'
echo -e '\nnon-standard (g++/clang++ default)\n------------------------------'
echo g++ && g++-4.8 -Wall -Wextra main.cpp && echo 'g++ ok'
echo -e '\nclang++' && clang++ -stdlib=libc++ -Wall -Wextra main.cpp -lsupc++ && echo 'clang++ ok.'
..........................


standard c++ (-std=c++11)
------------------------------
g++
g++ ok

clang++
clang++ ok.

legacy standard c++ (-std=c++98)
------------------------------
g++
main.cpp: In function ‘int main()’:
main.cpp:86:74: error: extended initializer lists only available with -std=c++11 or -std=gnu++11
     employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"}; 
                                                                          ^
main.cpp:86:18: error: extended initializer lists only available with -std=c++11 or -std=gnu++11
     employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"}; 
                  ^
main.cpp:87:60: error: extended initializer lists only available with -std=c++11 or -std=gnu++11
     employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
                                                            ^
main.cpp:87:18: error: extended initializer lists only available with -std=c++11 or -std=gnu++11
     employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
                  ^
main.cpp:88:69: error: extended initializer lists only available with -std=c++11 or -std=gnu++11
     employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"};
                                                                     ^
main.cpp:88:18: error: extended initializer lists only available with -std=c++11 or -std=gnu++11
     employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"};
                  ^

clang++
main.cpp:86:20: error: expected expression
    employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"}; 
                   ^
main.cpp:87:20: error: expected expression
    employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
                   ^
main.cpp:88:20: error: expected expression
    employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"};
                   ^
3 errors generated.

non-standard (g++/clang++ default)
------------------------------
g++
main.cpp: In function ‘int main()’:
main.cpp:86:74: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
     employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"}; 
                                                                          ^
main.cpp:86:18: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
     employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"}; 
                  ^
main.cpp:87:60: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
     employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
                                                            ^
main.cpp:87:18: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
     employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
                  ^
main.cpp:88:69: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
     employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"};
                                                                     ^
main.cpp:88:18: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
     employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"};
                  ^
g++ ok

clang++
main.cpp:86:20: error: expected expression
    employees[0] = {"Susan Meyers", 47899, "Accounting", "Vice President"}; 
                   ^
main.cpp:87:20: error: expected expression
    employees[1] = {"Mark Jones", 39119, "IT", "Programmer"};
                   ^
main.cpp:88:20: error: expected expression
    employees[2] = {"Joy Rogers", 81774, "Manufacturing", "Engineer"};
                   ^
3 errors generated.

http://coliru.stacked-crooked.com/a/f881aa23241994e6
Last edited on
Topic archived. No new replies allowed.