任我冰鸟 2017-03-01 12:57 采纳率: 0%
浏览 1475

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

这段代码是正确的,头文件实现了类的声明和定义。但是当我在建另一个源文件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


  • 写回答

2条回答 默认 最新

  • threenewbee 2017-03-01 15:38
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图