c++ 带默认值的构造函数 定义与实现分离

//stack.h
#include <vector>
using namespace std;

template<class T>
class Stack{
private:
	int maxsize;
	int top;
	vector<T> vec;
public:
	Stack(int ms = 0);


};
//stack.cpp
#include "stack.h"

template<class T>
Stack<T>::
Stack(int ms):maxsize(ms){
    cout<<"get it"<<endl;
}
//main.cpp
<pre name="code" class="cpp">#include <iostream>
#include "stack.cpp"
using namespace std;

int main()
{
	Stack<int> s(2);
    Stack<int> s1;
	return 0;
}


 
 

采取上述方法定义的stack类构造函数可用。

当 stack.h 中构造函数定义更改为:

Stack(int);

同时,stack.cpp中实现改为:

Stack(int ms = 0):maxsize(ms){}时,报错。


将 stack.h 中构造函数定义更改为:

Stack(int ms = 0):maxsize(ms), top(-1);

stack.cpp 中实现改为:

Stack(int ms){...}

报错


猜你喜欢

转载自blog.csdn.net/snail010101/article/details/51275977