0

I cannot acces the method from File1 in File2, the message is "has not been declared".

File1.cpp

namespace n1 
{
    namespace 
    {
        bool method(int x)
        {
            return x;
        }
    }
}

File2.cpp

#include <File1.cpp>
namespace n1
{
    TEST_F(Test, shouldReturnTrue)
    {
        Expect_True(method(101));
    }
}

Quarra
  • 2,527
  • 1
  • 19
  • 27
James Dean
  • 73
  • 8

2 Answers2

0

You have to include the first header file into the second header file. Otherwise the declarations in the first header file are not visible in the second header file.

Or you could include the both header files in compilation units where TEST_F is used.

Or you could join the dependent declarations in the both files in one header file.

After you updated your code naming the files as cpp files then the problem is that you are including cpp files one into another. And in some compilation unit the declarations from one cpp file are not visible in another cpp file. Or the problem can be that the both files recursively include each other.

You should move the declarations from the both cpp files in a header file as I wrote above. Do not place common declarations in a cpp file.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

I cannot acces the method from File1 in File2

Indeed. One translation unit doesn't know about declarations in other translation units. Since you call the function in File2.cpp, the function must be declared in that translation unit.

Solution to that problem: Declare the function in the file where you use the declaration:

// File2.cpp
namespace n1 
{
    namespace 
    {
        bool method(); // see next paragraph
    }
}

Now, we have another problem. Functions in anonymous namespaces are implicitly static. And static functions must be defined in all translation units where they are ODR used. So, a declaration is not sufficient after all; we need the definition:

// File2.cpp
namespace n1 
{
    namespace 
    {
        bool method()
        {
        } // see addendum
    }
}

Now, since the function in File1.cpp is never called and the file contains nothing else, we can actually remove that file.


P.S. The function definition has undefined behaviour because it doesn't exist the function by returning a value (nor by throwing) despite the return type being non-void.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • The OP wrote `#include `. – Asteroids With Wings Oct 28 '20 at 20:43
  • @AsteroidsWithWings And the problem is no longer reproducible after that change. That is a bad solution to the problem however because cpp files should never be included. – eerorika Oct 28 '20 at 20:49
  • Indeed. It does however make your answer incorrect according to the state the question was in 15 minutes before you posted it ;) – Asteroids With Wings Oct 28 '20 at 20:50
  • @AsteroidsWithWings In my opinion, the original question was a good one, and is worth answering. The new question is incorrect and has no answer because the problem doesn't exist. – eerorika Oct 28 '20 at 20:54