有没有办法避免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++中,我想要做的就是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)