简体   繁体   English

如何定义使用命名空间的全局结构,以便使用该结构的文件不使用此命名空间? C ++

[英]How to define a global struct which uses namespace so that files which use the struct do not use this namespace? c++

I have a common_module.h file which should store structures and functions used by most of .cpp files. 我有一个common_module.h文件,它应该存储大多数.cpp文件使用的结构和函数。

I want it to have an RColor struct. 我希望它有一个RColor结构。

RColor uses a lot of functions and variables from namespace cv. RColor使用命名空间cv中的许多函数和变量。 The project is made in a way that .cpp files generally do not use cv namespace (All work with it is mostly done by structures like RColor ) 该项目是以.cpp文件通常使用cv命名空间的方式制作的(所有使用它的工作主要由RColor结构RColor

I don't want to always write cv::something in definition of RColor . 我不想总是在RColor定义中写cv::something So i tried to create an RColor prototype in common_module.h and put it definition to Rcolor.cpp : 所以我尝试在common_module.h创建一个RColor原型并将其定义为Rcolor.cpp

//common_module.h
//...
struct RColor;

#include "stdafx.h"
#include<opencv2/opencv.hpp>
using namespace cv;
struct RColor
{
    ...
};

//Project0.cpp (main file)
#include "stdafx.h"
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<windows.h>
RColor col;

I get error: 我收到错误:

1>error C2079: 'col' uses undefined struct 'RColor'

You're getting the error because code using RColor needs to see its definition, not just its declaration. 您收到错误是因为使用RColor代码需要查看其定义,而不仅仅是其声明。 You'll have to move the definition to the header. 您必须将定义移动到标题。

As to how to deal with the namespace cv , you write: 至于如何处理命名空间cv ,你写:

I don't want to always write cv::something in definition of RColor . 我不想总是在RColor定义中写cv::something

The correct response to this is "don't be lazy, write it." 对此的正确回应是“不要懒惰,写下来”。 Explicit qualification is good, that's what namespaces are for. 显式限定很好,这就是名称空间的用途。 At least in the class definition itself, you have no way around it (*) - you want to prevent the identifiers from cv from polluting the global namespace. 至少在类定义本身中,你无法绕过它(*) - 你想阻止cv的标识符污染全局命名空间。 Note that it also makes the code more self-documenting: cv::transform tells a reader way more about the type or function than just transform does (is the latter cv::transform or std::transform or ...?) 请注意,它还使代码更加自我记录: cv::transform告诉读者更多关于类型或函数的信息,而不仅仅是transform (后者是cv::transformstd::transform或......?)

If you really want and need to save on typing cv:: inside member functions of RColor , you can put using namespace cv; 如果你真的想要并且需要节省输入RColor cv:: inside成员函数,你可以using namespace cv; inside the member function definitions. 在成员函数定义中。 But I wouldn't do even that. 但我不会这样做。


(*) There is actually a way to achieve what you want, but it's quite hacky (and would never pass code review with me). (*)有实际上一种方式来实现你想要什么,但它是相当哈克(和永远不会通过代码审查和我)。 But out of a sense of completeness, here goes: 但出于完整感,这里有:

// common_module.h

namespace NobodyTryToUseThisName
{
  using namespace cv;

  struct RColor
  {
    // ... definition here
  };
}

using NobodyTryToUseThisName::RColor;

But as I say, I do not recommend doing this. 但正如我所说,我不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM