c++ compiler cant find function overloaded with namespace

40 Views Asked by At

I have the following four files,

//struct.h
#pragma once
namespace coffee {
    class MD2 {};
    class Recorder{};
    class Book{};
}


//setup.h
#pragma once
#include "struct.h"
void wire(coffee::MD2 md2, coffee::Book book){}

//strategy.h
#pragma once
#include "struct.h"
#include "setup.h"
namespace strategy {
    int wire(coffee::MD2 md2, coffee::Recorder recorder) {}
    int setup(coffee::MD2 md2, coffee::Recorder recorder, coffee::Book book) {
        wire(md2, recorder);
        wire(md2, book); // <--- cant find this function
    }
}

//main.cpp
#include <iostream>
#include "strategy.h"
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

There is not using namespace X in header, but the compiler still cant find wire(md2, book), may I please know why?

1

There are 1 best solutions below

0
Chris On BEST ANSWER

As noted in comments, the wire in namespace strategy shadows the wire included from struct.h. Access that function by prepending with ::.

For instance, the following prints foo rather than bar::foo:

#include <iostream>

void foo() {
    std::cout << "foo" << std::endl;
}

namespace bar {
    void foo() {
        std::cout << "bar::foo" << std::endl;
    }

    void baz() { ::foo(); }
}

int main() {
    bar::baz();
}