如果在标头中声明了名称空间,是否可以避免在.cpp文件中使用类名?

编程入门 行业动态 更新时间:2024-10-24 12:20:58
本文介绍了如果在标头中声明了名称空间,是否可以避免在.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文件中使用类名?

本文发布于:2023-10-18 10:46:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声明   名称   文件   空间   标头中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!