直接上代码:
nl.h
#ifndef NL_H #define NL_H #include <iosfwd> namespace ZJ { /** Insert a newline character ' ' without flushing the ostream */ std::ostream& nl(std::ostream& os); } #endif // NL_H
nl.cpp
#include "nl.h" using namespace std; ostream& ZJ::nl(ostream& os) { return os << ' '; }
main.cpp
#include "nl.h" #include <iostream> using namespace ZJ; using namespace std; int main() { cout << "newlines"<< nl << "between"<< nl << "each"<< nl << "word"<< endl; cout << "................................"; return 0; }
报错:
nl.cpp
src
l.cpp(7) : error C2676: 二进制“<<”:“std::ostream”不定义该运算符或到预定义运算符可接收的类型的转换
原因:
nl.h中的#include <iosfwd>换成#include <iostream>