一、 #include “A.h” ,不通过静态库
(1)打开VS,新建一个名为project空项目;
(2)新建一个A.h头文件,声明类、函数和变量,代码如下:
#pragma once#ifndef A_H#define A_Hnamespace A {class Stack{private:int *data;//存放栈数据int count;//存放栈顶指针int size;//栈的容量public:Stack(int stacksize = 10);//构造函数建立具有10元素的默认栈~Stack();void Push(int x);//元素入栈int Pop();//元素出栈int howMany();//判定栈中有多个元素};int summ(int a, int b);void f();extern int n;}#endif // !A_H
(3)新建一个B.h头文件,声明类、函数和变量,代码如下:
#pragma once#ifndef B_H#define B_Hnamespace B {class Stack{private:int *data;//存放栈数据int count;//存放栈顶指针int size;//栈的容量public:Stack(int stacksize = 10);//构造函数建立具有10元素的默认栈~Stack();void Push(int x);//元素入栈int Pop();//元素出栈int howMany();//判定栈中有多个元素};int summ(int a, int b);void g();extern int n;}#endif // !B_H
(4)新建一个A.cpp源文件,代码如下:
#include "A.h"//包含头文件#include <iostream>//push和pop都用到了cout,所以包含此头文件using namespace std;A::Stack::Stack(int stacksize){if (stacksize > 0){size = stacksize;data = new int[stacksize];for (int i = 0; i < size; i++)data[i] = 0;}else{data = 0;size = 0;}count = 0;}A::Stack::Stack::~Stack(){delete[]data;}void A::Stack::Stack::Push(int x){if (count < size){data[count] = x;count++;}else{cout << "堆栈已满,不能再压入数据: " << x << endl;}}int A::Stack::Stack::Pop(){if (count <= 0){cout << "堆栈已空!" << endl;exit(1);//堆栈操作失败,退出程序!}count--;return data[count];}int A::Stack::Stack::howMany(){return count;}int A::summ(int a, int b) {return a + b;}void A::f() {cout << "A::f()" << endl;}int A::n = 10;
(5)新建一个B.cpp源文件,代码如下:
#include "B.h"//包含头文件#include <iostream>//push和pop都用到了cout,所以包含此头文件using namespace std;B::Stack::Stack(int stacksize){if (stacksize > 0){size = stacksize;data = new int[stacksize];for (int i = 0; i < size; i++)data[i] = 0;}else{data = 0;size = 0;}count = 0;}B::Stack::Stack::~Stack(){delete[]data;}void B::Stack::Stack::Push(int x){if (count < size){data[count] = x;count++;}else{cout << "堆栈已满,不能再压入数据: " << x << endl;}}int B::Stack::Stack::Pop(){if (count <= 0){cout << "堆栈已空!" << endl;exit(1);//堆栈操作失败,退出程序!}count--;return data[count];}int B::Stack::Stack::howMany(){return count;}int B::summ(int a, int b) {return a + b+100;}void B::g() {cout << "B::g()" << endl;}int B::n = 20;
(6)新建一个main.cpp源文件,代码如下:
//应用栈类的主程序:main.cpp#include <iostream>#include "A.h"#include "B.h"using namespace std;using namespace A;using namespace B;void main(){A::Stack s1;s1.Push(1);s1.Push(12);s1.Push(32);int x1 = s1.Pop();int x2 = s1.Pop();int x3 = s1.Pop();cout << x1 << "\t" << x2 << "\t" << x3 << endl;cout << "summ:" << A::summ(3, 4) << endl;cout << "A::n : " << A::n << endl;f();cout << "***************************" << endl;B::Stack s2;s2.Push(11);s2.Push(121);s2.Push(321);int y1 = s2.Pop();int y2 = s2.Pop();int y3 = s2.Pop();cout << y1 << "\t" << y2 << "\t" << y3 << endl;cout << "summ:" << B::summ(3, 4) << endl;cout << "B::n : " << B::n << endl;g();system("PAUSE");}
文件组织如下:
(5)F5调试,结果如下,成功。
二、#include “A.h” ,通过静态库
(1)制作静态库
参考博客:VS 静态链接库(.lib)的制作和调用,并对环境配置进行试验
打开VS,新建一个项目,选择Windows桌面→静态库,输入项目名StaticLib,选择位置,点击确定。
(2)将环境设置为 Debug x64
(3)添加头文件A.h,代码如下(stdafx.h和targetver.h是自动生成的):
#pragma once#ifndef A_H#define A_H#include"stdafx.h" //特别要注意:需要添加#include"stdafx.h" ;namespace A {class Stack{private:int *data;//存放栈数据int count;//存放栈顶指针int size;//栈的容量public:Stack(int stacksize = 10);//构造函数建立具有10元素的默认栈~Stack();void Push(int x);//元素入栈int Pop();//元素出栈int howMany();//判定栈中有多个元素};int summ(int a, int b);void f();extern int n;}#endif // !A_H
(4)添加头文件B.h,代码如下:
#pragma once#ifndef B_H#define B_H#include"stdafx.h" //特别要注意:需要添加#include"stdafx.h" ;namespace B {class Stack{private:int *data;//存放栈数据int count;//存放栈顶指针int size;//栈的容量public:Stack(int stacksize = 10);//构造函数建立具有10元素的默认栈~Stack();void Push(int x);//元素入栈int Pop();//元素出栈int howMany();//判定栈中有多个元素};int summ(int a, int b);void g();extern int n;}#endif // !B_H
(5)添加源文件:A.cpp,(stdafx.cpp是自动生成的),代码如下:
#include"stdafx.h"//特别要注意:把#include"stdafx.h" 放在最前面;#include "A.h"//包含头文件#include <iostream>//push和pop都用到了cout,所以包含此头文件using namespace std;A::Stack::Stack(int stacksize){if (stacksize > 0){size = stacksize;data = new int[stacksize];for (int i = 0; i < size; i++)data[i] = 0;}else{data = 0;size = 0;}count = 0;}A::Stack::Stack::~Stack(){delete[]data;}void A::Stack::Stack::Push(int x){if (count < size){data[count] = x;count++;}else{cout << "堆栈已满,不能再压入数据: " << x << endl;}}int A::Stack::Stack::Pop(){if (count <= 0){cout << "堆栈已空!" << endl;exit(1);//堆栈操作失败,退出程序!}count--;return data[count];}int A::Stack::Stack::howMany(){return count;}int A::summ(int a, int b) {return a + b;}void A::f() {cout << "A::f()" << endl;}int A::n = 10;
(6)添加源文件:B.cpp,,代码如下:
#include"stdafx.h"//特别要注意:把#include"stdafx.h" 放在最前面;#include "B.h"//包含头文件#include <iostream>//push和pop都用到了cout,所以包含此头文件using namespace std;B::Stack::Stack(int stacksize){if (stacksize > 0){size = stacksize;data = new int[stacksize];for (int i = 0; i < size; i++)data[i] = 0;}else{data = 0;size = 0;}count = 0;}B::Stack::Stack::~Stack(){delete[]data;}void B::Stack::Stack::Push(int x){if (count < size){data[count] = x;count++;}else{cout << "堆栈已满,不能再压入数据: " << x << endl;}}int B::Stack::Stack::Pop(){if (count <= 0){cout << "堆栈已空!" << endl;exit(1);//堆栈操作失败,退出程序!}count--;return data[count];}int B::Stack::Stack::howMany(){return count;}int B::summ(int a, int b) {return a + b + 100;}void B::g() {cout << "B::g()" << endl;}int B::n = 20;
(7)右键项目,点击生成;
生成:成功1个;
在D:\1-C++\static\StaticLib\x64\Debug目录下,有StaticLib.lib静态链接库生成。
(8)新建空项目,test-static
(9)将环境设置为 Debug x64
(10)在源文件目录下(D:\1-C++\static\test-static\test-static)新建一个存放头文件的文件夹,假设为h,将stack.h、stdafx.h和targetver.h复制到h中,并将头文件添加到项目的头文件中;
(11)在源文件目录下(D:\1-C++\static\test-static\test-static)新建一个存放静态库文件.lib的文件夹,假设为lib,生成的StaticLib.lib放在该文件夹中;
(10)配置属性
在VS底部点击属性管理器,在Debug|x64处右击,选择添加新项目属性表(也可以选择添加现有属性表,如果有的话)
选择PropertySheet,右击,选择属性
在通用属性 — C/C++ — 常规 — 附加包含目录,选择头文件存放目录;
通用属性 —链接器—常规—附加库目录:选择lib文件存放目录。
在通用属性 —链接器—输入—附加依赖项:加上lib文件名
(11)新建源文件:main.cpp,代码如下:
//应用栈类的主程序:main.cpp#include <iostream>#include "A.h"#include "B.h"using namespace std;using namespace A;using namespace B;void main(){A::Stack s1;s1.Push(1);s1.Push(12);s1.Push(32);int x1 = s1.Pop();int x2 = s1.Pop();int x3 = s1.Pop();cout << x1 << "\t" << x2 << "\t" << x3 << endl;cout << "summ:" << A::summ(3, 4) << endl;cout << "A::n : " << A::n << endl;f();cout << "***************************" << endl;B::Stack s2;s2.Push(11);s2.Push(121);s2.Push(321);int y1 = s2.Pop();int y2 = s2.Pop();int y3 = s2.Pop();cout << y1 << "\t" << y2 << "\t" << y3 << endl;cout << "summ:" << B::summ(3, 4) << endl;cout << "B::n : " << B::n << endl;g();system("PAUSE");}
(12)F5 调试运行,成功。
三、通过在主源文件中重新声明
(1)打开VS,新建一个名为project空项目;
(2)新建一个 A.cpp源文件,声明与定义类、函数和变量,代码如下:
#include <iostream>//push和pop都用到了cout,所以包含此头文件using namespace std;namespace A {class Stack{private:int *data;//存放栈数据int count;//存放栈顶指针int size;//栈的容量public:Stack(int stacksize = 10);//构造函数建立具有10元素的默认栈~Stack();void Push(int x);//元素入栈int Pop();//元素出栈int howMany();//判定栈中有多个元素};int summ(int a, int b);void f();extern int n;}A::Stack::Stack(int stacksize){if (stacksize > 0){size = stacksize;data = new int[stacksize];for (int i = 0; i < size; i++)data[i] = 0;}else{data = 0;size = 0;}count = 0;}A::Stack::Stack::~Stack(){delete[]data;}void A::Stack::Stack::Push(int x){if (count < size){data[count] = x;count++;}else{cout << "堆栈已满,不能再压入数据: " << x << endl;}}int A::Stack::Stack::Pop(){if (count <= 0){cout << "堆栈已空!" << endl;exit(1);//堆栈操作失败,退出程序!}count--;return data[count];}int A::Stack::Stack::howMany(){return count;}int A::summ(int a, int b) {return a + b;}void A::f() {cout << "A::f()" << endl;}int A::n = 10;
(2)新建一个 B.cpp源文件,声明与定义类、函数和变量,代码如下:
#include <iostream>//push和pop都用到了cout,所以包含此头文件using namespace std;namespace B {class Stack{private:int *data;//存放栈数据int count;//存放栈顶指针int size;//栈的容量public:Stack(int stacksize = 10);//构造函数建立具有10元素的默认栈~Stack();void Push(int x);//元素入栈int Pop();//元素出栈int howMany();//判定栈中有多个元素};int summ(int a, int b);void g();extern int n;}B::Stack::Stack(int stacksize){if (stacksize > 0){size = stacksize;data = new int[stacksize];for (int i = 0; i < size; i++)data[i] = 0;}else{data = 0;size = 0;}count = 0;}B::Stack::Stack::~Stack(){delete[]data;}void B::Stack::Stack::Push(int x){if (count < size){data[count] = x;count++;}else{cout << "堆栈已满,不能再压入数据: " << x << endl;}}int B::Stack::Stack::Pop(){if (count <= 0){cout << "堆栈已空!" << endl;exit(1);//堆栈操作失败,退出程序!}count--;return data[count];}int B::Stack::Stack::howMany(){return count;}int B::summ(int a, int b) {return a + b+100;}void B::g() {cout << "B::g()" << endl;}int B::n = 20;
(4)新建一个main.cpp源文件,代码如下:
//应用栈类的主程序:main.cpp#include <iostream>using namespace std;namespace A {class Stack{private:int *data;//存放栈数据int count;//存放栈顶指针int size;//栈的容量public:Stack(int stacksize = 10);//构造函数建立具有10元素的默认栈~Stack();void Push(int x);//元素入栈int Pop();//元素出栈int howMany();//判定栈中有多个元素};int summ(int a, int b);void f();extern int n;}namespace B {class Stack{private:int *data;//存放栈数据int count;//存放栈顶指针int size;//栈的容量public:Stack(int stacksize = 10);//构造函数建立具有10元素的默认栈~Stack();void Push(int x);//元素入栈int Pop();//元素出栈int howMany();//判定栈中有多个元素};int summ(int a, int b);void g();extern int n;}using namespace A;using namespace B;void main(){A::Stack s1;s1.Push(1);s1.Push(12);s1.Push(32);int x1 = s1.Pop();int x2 = s1.Pop();int x3 = s1.Pop();cout << x1 << "\t" << x2 << "\t" << x3 << endl;cout << "summ:" << A::summ(3, 4) << endl;cout << "A::n : " << A::n << endl;f();cout << "***************************" << endl;B::Stack s2;s2.Push(11);s2.Push(121);s2.Push(321);int y1 = s2.Pop();int y2 = s2.Pop();int y3 = s2.Pop();cout << y1 << "\t" << y2 << "\t" << y3 << endl;cout << "summ:" << B::summ(3, 4) << endl;cout << "B::n : " << B::n << endl;g();system("PAUSE");}
文件组织如下:
(7)F5调试,结果如下,成功。