"Undeclared identifier" error referring to a module partition I'm already importing

Osman Zakir 101 Reputation points
2022-02-10T10:47:58.647+00:00

I have the project I pushed to this repository. I have an error in there saying that Package is an undeclared identifier, in Truckload.cpp on line 9, even though I already have the line

import :package;

at the top.

I'm using VS 2022. Configuration is Debug x64 with the "Scan Sources for Module Dependencies" and "Multi-processor Compilation" settings turned on.

Please help me fix the error. Thanks.

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,513 questions
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,271 Reputation points Microsoft Vendor
    2022-02-11T02:05:48.34+00:00

    Hi @Osman Zakir ,

    There are several mistakes in your code, after correcting the program will work.(Be sure to export the classes and methods to be used in modules using export.)

    1. In Package.ixx you need to add export. export module truckload:package;
      import :shared_box;
      export class Package
      {
      public:
      Package(SharedBox box) : m_box{ box }, m_next{ nullptr } {}
      ~Package() { delete m_next; }
      SharedBox get_box() const { return m_box; }
      Package* get_next() { return m_next; }
      void set_next(Package* package) { m_next = package; }
      SharedBox m_box;
      Package* m_next;
      };
    2. The function addbox and the variable box_perline are named incorrectly in Truckload.cpp module truckload;
      import :package;
      import <iostream>; void Truckload::list_boxes() const
      {
      const std::size_t boxes_perline{ 4 };
      std::size_t count{};
      for (Package* e{ m_head }; e; e = e->get_next())
      {
      std::cout << ' ';
      e->get_box()->list_box();
      if (!(++count % boxes_perline))
      {
      std::cout << std::endl;
      }
      }
      if (count % boxes_perline)
      {
      std::cout << std::endl;
      }
      } SharedBox Truckload::get_first_box()
      {
      m_current = m_head;
      return m_current ? m_current->get_box() : nullptr;
      } SharedBox Truckload::get_next_box()
      {
      if (!m_current)
      {
      return get_first_box();
      }
      	m_current = m_current->get_next();  
      
      	return m_current ? m_current->get_box() : nullptr;  
      
      } Truckload::Truckload(const std::vector<SharedBox>& boxes)
      {
      for (const auto& box : boxes)
      {
      add_box(box);
      }
      } Truckload::Truckload(const Truckload& src)
      {
      for (Package* e{ src.m_head }; e; e = e->get_next())
      {
      add_box(e->get_box());
      }
      } void Truckload::add_box(SharedBox box)
      {
      auto package{ new Package{box} };
      if (m_tail)
      {
      m_tail->set_next(package);
      }
      else
      {
      m_head = package;
      }
      	m_tail = package;  
      
      } bool Truckload::remove_box(SharedBox box_to_remove)
      {
      Package* previous{ nullptr };
      Package* current{ m_head };
      while (current)
      {
      if (current->get_box() == box_to_remove)
      {
      if (previous)
      {
      previous->set_next(current->get_next());
      }
      			if (current == m_head)  
      			{  
      				m_head = current->get_next();  
      			}  
      			if (current == m_tail)  
      			{  
      				m_tail = previous;  
      			}  
      			if (current == m_current)  
      			{  
      				m_current = m_current->get_next();  
      			}  
      
      			current->set_next(nullptr);  
      			delete current;  
      
      			return true;  
      		}  
      		previous = current;  
      		current = current->get_next();  
      	}  
      	return false;  
      
      }

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful