1

I'm following this example of how to write modules in c++ 20: https://learn.microsoft.com/en-us/cpp/cpp/tutorial-named-modules-cpp?view=msvc-170 and I continue getting an error C7621 "module partition 'XXX' for module unit 'XXX' was not found.

Following the example I've created the next ixx *primary module interface:

export module Chapter01;

export import :Question01;

I've then added the ixx module partition file:

export module Chapter01:Question01;

import <iostream>;
import <set>;

using namespace std;

export bool IsUnique(const std::string& str);

And the cpp implementation file:

module Chapter01:Question01;

import <iostream>;
import <set>;

using namespace std;

bool IsUnique(const std::string& str)
{
    set<char> charset = set<char>();

    for (const char c : str)
    {
        if (charset. Contains(c))
        {
            return false;
        }

        charset. Insert(c);
    }

    return true;
}

And I keep getting a C7621: module partition 'Question01' for module unit 'Chapter01' was not found

Looking at the example over and over again I can't seem to find the difference.

Removing the cpp file entirely and putting the implementation in the ixx file fixes the issue, but I would still like to understand why the separation doesn't work.

P.S. What is the best practice when working with modules? To separate the ixx and cpp files or to just include ixx files?

2
  • 2
    Please do not post images of code but copy the code as (copyable) text in the post itself. Commented Feb 2, 2023 at 17:47
  • Yes this is in VS22, adding the tag Commented Feb 2, 2023 at 18:45

1 Answer 1

0

The tutorial you're using is incorrect. It suggests that you can have an implementation partition and interface partition that use the same partition name in the same module.

This is not allowed. All partitions within the same module must have different partition names.

Furthermore, the only reason to have an implementation partition is if you want some other module unit to be able to import that partition. Most implementation partitions don't need to be imported. So your implementation unit should just use module Chapter01;, and it will automatically import the Chapter01 module.

9
  • Changing `module Chapter01:Question01;' to 'module Chapter01;' in the cpp file does seem to solve the issue. But I still don't understand the issue then, or what files here had the same names, all files are named differently and all modules seem to have proper names, it would be great if you could explain that. Also my intention is to indeed import this module and reuse it in a different project, and after the cpp change that doesn't work. Before that a unit test project I had could use that module, but now it fails on 'C3861' "'IsUnique': identifier not found". Commented Feb 3, 2023 at 11:34
  • "it would be great if you could explain that" I don't know what more I can say. Filenames don't matter; module names matter. "Also my intention is to indeed import this module and reuse it in a different project" The module itself or the specific partition unit that implements that function? Because those aren't the same thing. Commented Feb 3, 2023 at 14:31
  • The specific partition unit Commented Feb 4, 2023 at 20:04
  • @AntonVasserman: Well, you can't include a module partition from "a different project". That's the entire point of partition units; they can only be imported to a file that is part of the module they are a partition of. They are inherently private to a module. If you want to have a section of a module that you can import into other modules, what you want is a separate module. Commented Feb 4, 2023 at 20:06
  • Also, you don't need to break modules into tiny pieces. There is no reason why this "different project" couldn't just import Chapter01 in its entirety and use from it which parts it wants. With inclusion, the more you include the slower compilation gets. Importing doesn't do that; that's one of the main points of the feature. Commented Feb 4, 2023 at 20:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.