关于类声明与实现的分离的一个问题

这段代码是正确的,头文件实现了类的声明和定义。但是当我在建另一个源文件a.cpp时,在a.cpp里写上#include"Struct_new.h"
_就立马报错了,
错误 1 error C2146: 语法错误: 缺少“;”(在标识符“elem1”的前面) c:\users\admin\documents\visual studio 2013\projects\struct\struct\struct_new.h 8 1 struct

说未定义识别法“Datatype”。这是为什么呢?_

主程序:
struct Datatype{
int a = 0;
int b = 0;
};
bool operator<(const Datatype& item1, const Datatype& item2){ return item1.a < item2.a; }
using namespace std;
#include"Struct_new.h"

int main(){
Datatype v1, v2;
v1.a = 3; v1.b = 4;
v2.a = 5; v2.b = 6;
T C(v1, v2);
C.compare();
system("pause");
return 0;
}


类的头文件:
#ifndef STRUCT_NEW_H
#define STRUCT_NEW_H
#include
#include
using namespace std;
class T{
private:
Datatype elem1, elem2;
public:
T(Datatype l1, Datatype l2) :elem1(l1), elem2(l2){ }
void compare();
};

void T::compare(){
if (elem1.a < elem2.a)
cout << "重载函数正确" << endl;
cout << "重载错误" << endl;
}
#endif


Datatype在哪里定义的,显然在h文件中你用到了,但是没有定义,也没有之前包含任何有这个定义的头文件。

Datatype v1, v2;前面要加上关键字struct
或者你的struct的定义改成:
struct Datatype{
int a = 0;
int b = 0;
}Datatype;