【发布时间】:2013-06-27 03:09:42
【问题描述】:
这可能很幼稚,但是我对cpp的预处理器感到很困惑: 我定义了一个头文件——Node.h:
#ifndef NODE_H_
#define NODE_H_
#include<iostream>
class Node{
friend std::ostream &operator<<(std::ostream &os, const Node & n);
public:
Node(const int i = -1);
private:
Node * next;
int value;
friend class List;
};
#endif
然后我在Node.cpp中定义了方法:
#include "Node.h"
using namespace std;
Node::Node(const int i):value(i), next(NULL){}
ostream& operator <<(ostream & os, const Node& n){
return os<<"value : "<<n.value<<endl;
}
最后,我有一个 test.cpp 文件来检查预处理器:
#include "Node.h"
//#include <iostream>
using namespace std;
int main(){
Node * n = new Node;
cout<<*n;
}
但是,当我尝试使用 gcc 编译时,出现以下错误:
/home/xuan/lib/singleLinkedList/test.cpp:6:‘Node::Node(int)’未定义引用
【问题讨论】:
-
建议:
const int在这里没有意义,使用Node(int i)
标签: c++ c-preprocessor