The template class definition and implementation can be different from the same file.

Source: Internet
Author: User

When writing C ++ programs, we often write the function definition in XXX. h, write the function implementation in XXX. CPP, but when we write functions and classes with templates

The following error occurs:

Stack. h

//stack.h#ifndef STACK_HPP#define STACK_HPP#include <vector>#include <stdexcept>template<typename T, typename TContainer = std::vector<T>>class CStack{public:void push(const T& vValue);void pop();T top() const;bool empty() const {return m_Container.empty();}private:TContainer m_Container;};#endif
Stack. cpp

#include "stack.h"template<typename T, typename TContainer>void CStack<T, TContainer>::push(const T& vValue){m_Container.push_back(vValue);}template<typename T, typename TContainer>void CStack<T, TContainer>::pop(){if (empty()) throw std::out_of_range("Stack::pop: empty stack\n");m_Container.pop_back();}template<typename T, typename TContainer>T CStack<T, TContainer>::top() const{if (empty()) throw std::out_of_range("Stack::top: empty Stack\n");return m_Container.back();}
Then an error occurs during the test of the main function:

#include "stack.h"int main(){       stack<int> IntStack;       ....}


In the past, there was always no way to write definitions and implementations in the same file. Today, we finally found a solution.

First, define stack. HPP and class.

#ifndef STACK_HPP#define STACK_HPP#include <vector>#include <stdexcept>template<typename T, typename TContainer = std::vector<T>>class CStack{public:void push(const T& vValue);void pop();T top() const;bool empty() const {return m_Container.empty();}private:TContainer m_Container;};#endif
Then define stackdef. HPP to implement the functions defined in the template.

#ifndef STACKDEF_HPP#define STACKDEF_HPP#include "stack.hpp"template<typename T, typename TContainer>void CStack<T, TContainer>::push(const T& vValue){m_Container.push_back(vValue);}template<typename T, typename TContainer>void CStack<T, TContainer>::pop(){if (empty()) throw std::out_of_range("Stack::pop: empty stack\n");m_Container.pop_back();}template<typename T, typename TContainer>T CStack<T, TContainer>::top() const{if (empty()) throw std::out_of_range("Stack::top: empty Stack\n");return m_Container.back();}#endif

Final Test

#include "stack_def.hpp"int main(){CStack<int> IntStack;IntStack.push(1);system("pause");return 0;}
That's all!







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.