不明确的引用和命名空间(来自两个外部库的定义的冲突) [英] Ambiguous reference and namespace (clash of definitions from the two external libraries)

查看:126
本文介绍了不明确的引用和命名空间(来自两个外部库的定义的冲突)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了定义的崩溃,我无法理解。



示意图的问题如下:



主项目文件有两个包括:

  include< lib1.h> 
include< lib2.h>

第一个标题包括来自库的其他标题,在其中一个有一个直接未覆盖有名称空间)定义:

 模板< typename T&类SparseMatrix; 

lib2.h在

中有以下内容

 命名空间lib2 
{
使用命名空间lib3;

class ...
{
...
SparseMatrix< double> ...
...
}
}

lib3,覆盖了命名空间,还有一个SparseMatrix类的定义。



每个库单独编译没有问题。当我尝试编译使用的可执行文件时,编译器产生一个错误:

  lib2.h:70:7:引用'SparseMatrix'是不明确的

这看起来很奇怪,因为在主程序中,

 使用命名空间lib3; 

因此,我看不到这些定义应该崩溃的原因。
我将非常感谢任何可能的解释的问题。



当然,我可以将lib1中的定义放入自己的命名空间,但是我需要修改相当多的文件,我宁愿不是。



注释:
下面的答案是正确的,但我也能够通过更改包括的文件的顺序来解决问题, lib2然后lib1。

解决方案


>使用命名空间lib3;


但是如果你看看 / code>,正是这样写的。 lib3 命名空间的内容已被引入 lib2 ,并且在定义 SparseMatrix< double> 对象。



您可以将 lib2.h 视为类似这样的所有包括已经解决:

 模板< typename T&类SparseMatrix; //(1)

命名空间lib3
{
template< typename T>类SparseMatrix; //(2)
}

命名空间lib2
{
使用命名空间lib3; //(3)

class ...
{
...
SparseMatrix< double> ... //(4)
// :: SparseMatrix< double>只会看到(1)
// lib2 :: SparseMatrix< double>只会看到(2)
}
}

)声明 SparseMatrix ,它立即在行(4)上可见。第(2)行的声明不是,但是由于第(3)行使它进入命名空间 lib2 ,它现在也在第(4)行可见。



您可以通过完全限定类型来解决这个问题:

  :: SparseMatrix< double> ... 

:: c>

另一个选择是不使用使用命名空间lib3; c> lib2.h 并正确限定 lib3 命名空间的内容。


I experience the collapse of definitions i can't make sense of.

Schematically the problem is as follows:

The main project file has two includes:

include <lib1.h>
include <lib2.h>

The first header includes couple of other headers from the library, in one of those there is a direct (not covered with a name-space) definition:

template<typename T> class SparseMatrix;

The lib2.h has the following inside

namespace lib2
{
   using namespace lib3;

   class ...
   {
      ...
      SparseMatrix<double> ...
      ...
    }
}

Inside lib3, covered with the namespace, there is also a definition of SparseMatrix class.

Each library separately compiles with no problem. When I try to compile the executable which uses the, compiler produces an error:

lib2.h:70:7: error: reference to 'SparseMatrix' is ambiguous

Which looks strange to me since nowhere in the main program do I write

using namespace lib3;

Thus I see no reason why those definitions should collapse. I would greatly appreciate any possible explanation for the problem.

Of course, I can enclose the definitions from the lib1 into a their own namespace, but then I would need to modify quite a number of files there, which I would rather not do.

COMMENT: the answer below is correct but i was also able to get around the problem by changing the order of included files, i.e. first include lib2 and then lib1.

解决方案

nowhere in the main program do i write using namespace lib3;

But if you look in lib2.h, exactly that has been written. The contents of the lib3 namespace have been brought into lib2 and are now visible when defining the SparseMatrix<double> object.

You can think of lib2.h as being something like this after all includes have been resolved:

template <typename T> class SparseMatrix;    // (1)

namespace lib3
{
   template <typename T> class SparseMatrix; // (2)
}

namespace lib2
{
   using namespace lib3; // (3)

   class ...
   {
      ...
      SparseMatrix<double> ... // (4)
      // ::SparseMatrix<double> would only see (1)
      // lib2::SparseMatrix<double> would only see (2)
    }
}

The line marked (1) declares SparseMatrix which is immediately visible on line (4). The declaration on line (2) would not be, but since line (3) brings it into namespace lib2, it is now also visible on line (4).

You can get around this simply by fully qualifying the type:

::SparseMatrix<double> ...

:: with no preceding namespace denotes the global namespace.

The other alternative is to not have using namespace lib3; in lib2.h and properly qualify the contents of the lib3 namespace.

这篇关于不明确的引用和命名空间(来自两个外部库的定义的冲突)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆