Where to place STL / template code c++? -
hi have quick question. part of homework i've been asked write own template vector class (most of code there needs expanded upon). while understand how works , have no idea put code or reference have never seen in context within program.
do create new cpp file information in it, or add in above main method? if create new file (either cpp or h) how reference it, #include normal?
this might seem simple i've tried creating new .h file , including in main program scope definition errors.
most compilers require put template code in header file, rather source. due way template expansion works. include header in whichever files need use vector class.
some things watch out when creating header:
- prevent multiple inclusion. if compiler supports
#pragma once
can put @ top, otherwise use#ifndef my_header_h .......
pattern. - don't forget put semi-colon on end of class!!!!
- never put
using namespace whatever;
in outer scope of header (it's okay use within block scope suchnamespace { ... }
or function). - be careful of name conflicts
std::vector
if calling classvector
- make sure nobody has importedstd
namespace prior including header.
Comments
Post a Comment