如何在另一个C 标头文件中导入类
How to import a class in another C++ header file?
我在构造课程时遇到了一个问题。类"图形"在另一个文件中导入类"袋",然后使用"袋子"作为其组件。
//Graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <fstream>
#include <iostream>
#include <vector>
#include "Bag.h"
class Bag;
class Graph
{
public:
Graph(int V);
Graph(std::ifstream& in_file);
int getV() { return V; }
int getE() { return E; }
void addEdge(int v, int w);
void showadj() ;
private:
int V;
int E;
std::vector<Bag> adj;
};
#endif
和" bag.h"如下:
//Bag.h
#ifndef BAG_H
#define BAG_H
#include <vector>
#include <iostream>
class Bag
{
public:
Bag();
void addBag(int i) { content.push_back(i); }
void showBag();
private:
std::vector<int> content;
};
#endif
graph.cpp:
//Graph.cpp
#include "Graph.h"
#include "Bag.h"
Graph::Graph(int V) : V(V), E(0)
{
for (int i = 0; i < V; i++)
{
Bag bag;
adj.push_back(bag);
}
}
bag.cpp(对不起,算了):
#include "Bag.h"
void Bag::showBag()
{
for (int i : content)
{
std::cout << i << " ";
}
}
当我尝试补充这两个类时,出现错误说:
C:UsersADMINI~1AppDataLocalTempccMj4Ybn.o:newtest.cpp:(.text+0x1a2): undef
ined reference to `Bag::Bag()'
collect2.exe: error: ld returned 1 exit status
您还需要实现构造函数Bag::Bag()
,因为它在Bag.cpp
文件中丢失。
这是错误告诉您的。如果您不需要构造函数,则应将其从类定义中删除,这也可以解决此错误。
另一种选择是在Bag.h
文件中提供一个空构造函数
class Bag
{
public:
Bag() {}
...
}
相关文章:
- 从文件导入数据时遇到问题.我有一个没完没了的循环
- 使用 GDAL 将 DXF 块从一个文件导入到另一个文件
- 如何将COM .tlb文件导入具有相对路径C++文件中?
- C++将分隔文件导入矢量
- 将 .ui 文件导入 Qt5 C++时出错"was not declared in this scope"
- 为什么我无法将头文件导入到其中一个头文件中的库中?
- 将 Obj 文件导入 vtk
- 在编译时将文本文件导入原始字符串文本
- 从文件导入注册表
- QComplete-如何从文件导入数据
- 如何从 .so 文件导入 python 模块
- 将.csv文件导入 QT GUI 上的 sqlite3 数据库表
- 这是将csv文件导入MS Access的正确SQL语法吗
- 从文本文件导入的不同线类型创建变量
- 如果使用LoadLibrary显式链接dll,则列出从PE文件导入的函数
- 如何将CSV文件导入QTableWidget
- 使用C++从MAT文件导入三维阵列
- C++ 操作方法:从.txt或.csv文件导入数值变量
- 为什么我将数字数据从文本文件导入二维数组的代码不起作用
- 将值从*.dat文件导入数组