相关疑难解决方法(0)

是否可以避免在实现文件中重复类名?

有没有办法避免Graph::实现文件中的重复,但仍然将类拆分为头+实现?如:

头文件:

#ifndef Graph_H
#define Graph_H

class Graph {
public:
    Graph(int n);
    void printGraph();
    void addEdge();
    void removeEdge();
};

#endif
Run Code Online (Sandbox Code Playgroud)

实施档案:

Graph::Graph(int n){}
void Graph::printGraph(){}
void Graph::addEdge(){}
void Graph::removeEdge(){}
Run Code Online (Sandbox Code Playgroud)

c++ syntax class

19
推荐指数
4
解决办法
5398
查看次数

如果我在标题中声明了一个名称空间,我可以避免在.cpp文件中使用类名吗?

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

可悲的是,我已经看了20多个主题和我的C++书,现在已经超过两个小时了,但却无法解决这个问题.我认为这是因为我正在尝试使用我在C++中进行的10年历史的java训练.

第一次试用:

//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
  ...
}
Run Code Online (Sandbox Code Playgroud)

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

class …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces class definition

6
推荐指数
1
解决办法
868
查看次数

标签 统计

c++ ×2

class ×2

definition ×1

namespaces ×1

syntax ×1