public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100135] New: ICE when using constants in a mdoule
@ 2021-04-18  9:56 patrick.kox at commandoregel dot be
  2023-10-05 20:28 ` [Bug c++/100135] [modules] ICE when using constants in a module nicolas.werner at hotmail dot de
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: patrick.kox at commandoregel dot be @ 2021-04-18  9:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100135

            Bug ID: 100135
           Summary: ICE when using constants in a mdoule
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: patrick.kox at commandoregel dot be
  Target Milestone: ---

Using the following code from the book (Professional C++ 5th Edition) by Marc
Grergoire causes an ICE when trying to compile the "Employee" module.

I have 2 files:
Employee.cpp (the Interface, called Employee.cppm in the book):
===
export module employee;
import<string>;
namespace Records {

const int DefaultStartingSalary{30'000};
export const int DefaultRaiseAndDemeritAmount{1'000};

export class Employee{
        public:
        Employee(const std::string& firstName, const std::string& lastName);

        void promote(int raiseAmount = DefaultRaiseAndDemeritAmount);
        void demote(int demeritAmount = DefaultRaiseAndDemeritAmount);
        void hire();            // Hires or rehires the employee
        void fire();            // Dismisses the employee
        void display() const;   // Display employee information on the console

        // Getters and setters
        void setFirstName(const std::string& firstName);
        const std::string& getFirstName() const;

        void setLastName(const std::string& lastName);
        const std::string& getLastName() const;

        void setEmployeeNumber(int employeeNumber);
        int getEmployeeNumber()const;

        void setSalary(int salary);
        int getSalary()const;

        bool isHired() const;

        private:
        std::string m_firstName;
        std::string m_lastName;
        int m_employeeNumber{-1};
        int m_salary{DefaultStartingSalary};
        bool m_hired{false};
};
}
===
And Employee-i.cpp (called Employee.cpp in the book):
===
module employee;
import<iostream>;
//import <format>;      // Not Available
using namespace std;

namespace Records {
Employee::Employee(const string& firstName, const string& lastName) :
m_firstName{firstName}, m_lastName{lastName}
{
}

void Employee::promote(int raiseAmount)
{
        setSalary(getSalary() + raiseAmount);
}

void Employee::demote(int demeritAmount)
{
        setSalary(getSalary() - demeritAmount);
}

void Employee::hire()
{
        m_hired = true;
}

void Employee::fire()
{
        m_hired = false;
}

void Employee::display() const
{
        cout << "Employee: " << getLastName() << ", " << getFirstName() <<
endl;
        cout << "-------------------------" << endl;
        cout << (isHired() ? "Current employee" : "Former employee") << endl;
        cout << "Employee number: " << getEmployeeNumber() << endl;
        cout << "Salary: $" << getSalary() << endl;
        cout << endl;
}

// Getters and setters
void Employee::setFirstName(const string& firstName)
{
        m_firstName = firstName;
}

const string& Employee::getFirstName() const
{
        return m_firstName;
}

void Employee::setLastName(const string& lastName)
{
        m_lastName = lastName;
}

const string& Employee::getLastName() const
{
        return m_lastName;
}

void Employee::setEmployeeNumber(int employeeNumber)
{
        m_employeeNumber = employeeNumber;
}
int Employee::getEmployeeNumber() const
{
        return m_employeeNumber;
}

void Employee::setSalary(int salary)
{
        m_salary = salary;
}

int Employee::getSalary() const
{
        return m_salary;
}

bool Employee::isHired() const
{
        return m_hired;
}
}
===
Note: import <format>; is commented out since this module is not available yet
(the code is also modified to use the "old cout" syntax and not the new
format(""); syntax.

The error message thie causes is:
Employee.cpp:19:14: error: ‘void Records::Employee::promote(int)’ references
internal linkage entity ‘const int Records::DefaultRaiseAndDemeritAmount’
   19 |         void promote(int raiseAmount = DefaultRaiseAndDemeritAmount);
      |              ^~~~~~~
Employee.cpp:20:14: error: ‘void Records::Employee::demote(int)’ references
internal linkage entity ‘const int Records::DefaultRaiseAndDemeritAmount’
   20 |         void demote(int demeritAmount = DefaultRaiseAndDemeritAmount);
      |              ^~~~~~
Employee.cpp:15:14: error: ‘class Records::Employee’ references internal
linkage entity ‘const int Records::DefaultStartingSalary’
   15 | export class Employee{
      |              ^~~~~~~~
Employee.cpp:4:8: error: failed to write compiled module: Bad file data
    4 | export module employee;
      |        ^~~~~~
Employee.cpp:4:8: note: compiled module file is ‘gcm.cache/employee.gcm’
In module imported at Employee-i.cpp:5:1:
employee: error: failed to read compiled module: No such file or directory
employee: note: compiled module file is ‘gcm.cache/employee.gcm’
employee: note: imports must be built before being imported
employee: fatal error: returning to the gate for a mechanical issue
compilation terminated.
make: *** [Makefile:55: gcm] Error 1

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-03-06 20:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-18  9:56 [Bug c++/100135] New: ICE when using constants in a mdoule patrick.kox at commandoregel dot be
2023-10-05 20:28 ` [Bug c++/100135] [modules] ICE when using constants in a module nicolas.werner at hotmail dot de
2023-10-05 20:51 ` nicolas.werner at hotmail dot de
2024-03-06 20:16 ` ppalka at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).