0

I am trying to complete an assignment for beginner comp sci. We are just learning about space allocation and using headers, as well as dynamic memory allocation. One of the requirements is that we have to use unnamed namespace in a header for our const variable and the function declaration. I receive a compiling error on line 28 about "undefined reference to anonymous namespace" I am unsure how to fix this or what syntax or typo is causing it. We are just learning about this so don't judge too harshly haha.

mylibrary.hpp
#include <iostream>
#include <memory>

#ifndef MYLIBRARY_HPP
#define MYLIBRARY_HPP

namespace
{
    int counter = 0;
    const int SIZE = 10;
    std::unique_ptr<char[]> deleteRepeats(char arr[]);
}

#endif // MYLIBRARY_HPP
main.cpp
using std::cin;
using std::cout; //using declarations to avoid namespace std
using std::endl;

int main()
{
   
    char originalArray[SIZE]; //declaration of array that will be used in the program and all its values
    originalArray [0] = 'a';
    originalArray [1] = 'b';
    originalArray [2] = 'b';
    originalArray [3] = 'c';
    originalArray [4] = 'a';
    originalArray [5] = 'c';
    originalArray [6] = 'a';
    originalArray [7] = 'c';
    originalArray [8] = 'b';
    originalArray [9] = 'c';
    std::unique_ptr<char[]> noRepeats = deleteRepeats(originalArray); //function call

    cout << "the amount of repeats is... " << SIZE-counter << endl; //prints out the number of repeats

    for(int i =0; i<counter; i++) //displays new array
    {
        cout << noRepeats[i] << endl;
    }

    return 0;
}
function.cpp
#include <iostream>
#include <memory>

//my function definitions
namespace
{
    //Precondition: an array of defined size contains only valid characters
    //Postconition: a new array is generated with only unique values
    std::unique_ptr<char[]> deleteRepeats(char arr[]) //goes through the array and checks if it has repeats
    {
        for(int i=0; i<SIZE/2; i++)
        {
            if(arr[i] = arr[SIZE -1-i]) //marks any repeats
            {
                arr[i] = '-';
                arr[SIZE-1-i] = '-';
            }
        }
        for(int i =0; i<SIZE; i++) //counts new array
        {
            if(arr[i] != '-')
            {
                counter++;
            }
        }
        std::unique_ptr<char[]> newArray(new char[counter]); //declares a new array using a pointer
        int location = 0;
        for(int i = 0; i<SIZE; i++)
        {
            if(arr[i] != '-')
            {
                newArray[location++] = arr[i];
            }
        }
        return newArray;
    }
}
6
  • 1
    Taking your code and making it all one file shows no errors, but the warning you will need to fix it. Commented Sep 11, 2023 at 19:57
  • 1
    FYI //Postconition: a new array is generated with only unique values -- There are far more easier ways to do this than your attempt. std::sort and then std::unique, or simply store everything into a std::set, and create a new array from the std::set contents. Commented Sep 11, 2023 at 19:59
  • @PaulMcKenzie Really? Every time I compile in the software the class gives us I get a red flag on my function call in main.cpp "C:\Users\Shaun\Desktop\CS Work\CS2 Assign 1-A\main.cpp|28|undefined reference to `(anonymous namespace)::deleteRepeats(char*)'| " Commented Sep 11, 2023 at 20:46
  • Take the code at the link, copy and paste it exactly as it is in one file. Compile it. Do you still see the error? Commented Sep 11, 2023 at 20:56
  • Header files shouldn't contain declarations in an unnamed namespace. Commented Sep 11, 2023 at 21:03

2 Answers 2

1
Two versions of function deleteRepeats

Your program has two translation units: main.cpp and MyFunction.cpp. Each has its own anonymous namespace. The names that are declared and/or defined in the anonymous namespace of main.cpp can only be referenced (i.e., used) within main.cpp. Similarly, the names that are declared and/or defined in the anonymous namespace of MyFunction.cpp can only be referenced within MyFunction.cpp.

That means that function deleteRepeats defined in MyFunction.cpp cannot be called from main.cpp. Instead, main.cpp declares its own function deleteRepeats (in its anonymous namespace), which is a completely different function than the one defined in MyFunction.cpp. In main.cpp, function deleteRepeats is delcared, but never receives a defintion.

The error message:

"undefined reference to anonymous namespace"

refers to function deleteRepeats in main.cpp, which has no definition.

I recommend checking with your teacher to see exactly which names are supposed to be in the anonymous namespace.

Side issues

As identified in the comments, there are several problems with the definition of deleteRepeats given in MyFunction.cpp.

  1. Comparison should use two equal signs (==), rather than the one used by assignment (=).
  2. The logic for identifying duplicates is overly complicated/wrong.

One simple way to eliminate the duplicates is to create a std::set, and insert all the elements in the array. Then you could create a new array by iterating over the elements of the set.

Sign up to request clarification or add additional context in comments.

Comments

0

Every time you include the header, you get a new unnamed namespace with a different secret name.

The only way to make it "work" is to only include it once, and only use the objects in that one source file. Kind of defeats the reason for having that header file at all.

There is no way to refer to an unnamed namespace in a different source file. This is the purpose of the construct!

2 Comments

Okay, and ... how do we solve OP's problem then?
We don't really. A by-the-letter-of-the-law solution would be to have another function, outside the namespace, and then in one source have that function call the anonymous one. But then why bother to put that namespace in the header?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.