如果在标头中声明了名称空间,是否可以避免在.cpp文件中使用类名? [英] Can I avoid using the class name in the .cpp file if I declare a namespace in the header?

查看:74
本文介绍了如果在标头中声明了名称空间,是否可以避免在.cpp文件中使用类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,我要做的就是在 .h 文件中声明一个 DisplayInfo 类,然后在 .cpp 文件中声明一个,而不必键入第一个 DisplayInfo :: DisplayInfo()和每个函数定义.

可悲的是,我已经研究了20多个主题,而我的C ++书籍已经有两个多小时了,但仍无法解决.我认为这是因为我正在尝试在C ++中使用已有1​​0年历史的Java培训.

第一次审判:

 //DisplayInfo.h命名空间DisplayInfoNamespace{类DisplayInfo{上市:DisplayInfo();//默认构造函数float getWidth();float getHeight();...};}//DisplayInfo.cpp使用名称空间DisplayInfoNamespace;//不起作用使用名称空间DisplayInfoNamespace :: DisplayInfo//也不起作用使用DisplayInfoNamespace :: DisplayInfo//不起作用{DisplayInfo :: DisplayInfo(){};//当我删除命名空间时有效,但是第一个DisplayInfo ::是我不想输入的内容DisplayInfo :: getWidth(){返回DisplayInfo :: width;}//更多我不想输入的DisplayInfo ::...} 

对于第二次试用,我尝试切换订单,所以是

  Class DisplayInfo{命名空间DisplayInfoNamespace{...}} 

.cpp 文件中,尝试了上述所有加号

 使用名称空间DisplayInfo :: DisplayInfoNamespace; 

对于第三次尝试,我尝试使用此标头声明它:

 命名空间DisplayInfoNamespace{类DisplayInfo;}DisplayInfo类{上市:我所有的方法和构造函数}; 

我使用的是VisualStudio2010 Express,尽管仔细阅读了错误消息,但仍无法在标头和.cpp文件中找到正确的类和名称空间排列,以解决此问题.

现在我花了30分钟输入它之后,是 C ++:类命名空间"吗?答案?(又名,您必须使用typedef吗?)

解决方案

在类外进行操作时,无法缩短 A :: A()定义语法./p>

在该类中,您无需使用havinf就可以选择正确的作用域来就地定义函数.

示例:

* .h中的

 //命名空间meh {A类{上市:一种() {std :: cout<<构建体"<<std :: endl;}无效foo();无效bar();}无效foo();}无效foo();//在* .cpp中无效foo(){std :: cout<<来自名称空间外部的foo"<<std :: endl;}无效的meh :: foo(){std :: cout<<"<<来自命名空间内部的foo,而不是来自类内部的foo"std :: endl;}无效的meh :: A :: foo(){std :: cout<<"foo"<<std :: endl;}命名空间meh {无效A :: bar(){std :: cout<<酒吧"<<std :: endl;}} 

如您所见,名称空间宁愿在方法名称前添加另一件事,而不是删除它.

In C++, all I want to do is declare a DisplayInfo class in a .h file, and then in the .cpp file, not have to type the first DisplayInfo::DisplayInfo() and every function definition.

Sadly, I've looked at over 20 topics and my C++ book for over two hours now and have not been able to resolve this. I think it's because I'm trying to use my 10-year-old java training in C++.

1st trial:

//DisplayInfo.h  
namespace DisplayInfoNamespace 
{
  Class DisplayInfo 
  {
    public:
    DisplayInfo(); //default constructor
    float getWidth();
    float getHeight();
    ...
  };
}

//DisplayInfo.cpp
using namespace DisplayInfoNamespace;  //doesn't work
using namespace DisplayInfoNamespace::DisplayInfo //doesn't work either
using DisplayInfoNamespace::DisplayInfo //doesn't work
{
  DisplayInfo::DisplayInfo() {}; //works when I remove the namespace, but the first DisplayInfo:: is what I don't want to type 
  DisplayInfo::getWidth() {return DisplayInfo::width;}  //more DisplayInfo:: that I don't want to type
  ...
}

For the second trial, I tried switching the order, so it was

class DisplayInfo
{

  namespace DisplayInfoNamespace
  {
  ...
  }
}

And in the .cpp file, tried all of the above plus

using namespace DisplayInfo::DisplayInfoNamespace; 

For the third trial I tried forward declaring it with this header:

namespace DisplayInfoNamespace
{
  class DisplayInfo;
}
class DisplayInfo
{
public:
...all my methods and constructors...
};

I'm using VisualStudio2010 express and despite carefully reading the error messages have not been able to find the right arrangement of classes and namespaces in the header and .cpp file to make this work out.

And now after I spent 30 minutes typing this, is C++: "Class namespaces"? the answer? (aka no, you have to use typedefs?)

解决方案

There is no way to shorten the A::A() definition syntax, when you do it outside of the class.

within the class it would alow you to define the functions inplace without havinf to select the correct scope.

example:

// in *.h
namespace meh {
  class A {
  public:
    A() {
      std::cout << "A construct" << std::endl;
    }

    void foo();
    void bar();
  }

  void foo();
}

void foo();


// in *.cpp

void foo() {
  std::cout << "foo from outside the namespace" << std::endl;
}

void meh::foo() {
  std::cout << "foo from inside the namespace, but not inside the class" << std::endl;
}

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


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

As you can see, namespaces would rather add another thing to put in front of your method name, rather than remove one.

这篇关于如果在标头中声明了名称空间,是否可以避免在.cpp文件中使用类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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