同一解决方案下,两个项目间自定义class调用问题
时间:2008-12-07
来源:互联网
首先陈述一下我要达到的目标。我建立了一个Test解决方案,底下有两个项目Test和Tested(当然这只是为了关注问题所在所作的抽象化),我想在Test里调用Tested中的定义class。具体code如下:
Test.cpp
#include "../Tested/Tested.h"
int main() {
Tested test1;
Tested test2(5);
}
Tested.h
#include <iostream>
class Tested {
protected:
int T;
public:
Tested();
Tested(int t);
// Tested() : T(0) {}
// Tested(int t) : T(t) {}
};
Tested.cpp
#include "Tested.h"
Tested::Tested() : T(0) {}
Tested::Tested(int t) : T(t) {}
VC++ Express 2008编译Test报告如下错误:
2>test.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Tested::Tested(int)" (??0Tested@@QAE@H@Z),该符号在函数 _main 中被引用
2>test.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Tested::Tested(void)" (??0Tested@@QAE@XZ),该符号在函数 _main 中被引用
意思好象是红色部分constructor没法被解析。然而,我若对Tested稍作改动,即将两个constructor直接定义在头文件里(见蓝色部分)并去掉Tested.cpp,Test运行便正常!!
所以问题貌似出在Test项目不能包含定义在Tested.cpp里的信息。我想请问是怎么回事?该如何修改使得Test项目认识定义在cpp文件里的东西?!万分感谢!!
Test.cpp
#include "../Tested/Tested.h"
int main() {
Tested test1;
Tested test2(5);
}
Tested.h
#include <iostream>
class Tested {
protected:
int T;
public:
Tested();
Tested(int t);
// Tested() : T(0) {}
// Tested(int t) : T(t) {}
};
Tested.cpp
#include "Tested.h"
Tested::Tested() : T(0) {}
Tested::Tested(int t) : T(t) {}
VC++ Express 2008编译Test报告如下错误:
2>test.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Tested::Tested(int)" (??0Tested@@QAE@H@Z),该符号在函数 _main 中被引用
2>test.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Tested::Tested(void)" (??0Tested@@QAE@XZ),该符号在函数 _main 中被引用
意思好象是红色部分constructor没法被解析。然而,我若对Tested稍作改动,即将两个constructor直接定义在头文件里(见蓝色部分)并去掉Tested.cpp,Test运行便正常!!
所以问题貌似出在Test项目不能包含定义在Tested.cpp里的信息。我想请问是怎么回事?该如何修改使得Test项目认识定义在cpp文件里的东西?!万分感谢!!
作者: miniwolfy 发布时间: 2008-12-07
将Tested中的相应CPP文件加到Test中来一起编译就行了
作者: A_B_C_ABC 发布时间: 2008-12-07
添加进去是没问题。想请教这是否是项目间互相调用的惯例做法?还是另有玄机?
作者: miniwolfy 发布时间: 2008-12-07
一般把一个项目做成DLL或者静态库.这样在另一个项目中直接引用头文件和编译好的lib就行了.
作者: yndfcd 发布时间: 2008-12-07
噢!多谢!只是不知DLL或者静态库该怎么做?
作者: miniwolfy 发布时间: 2008-12-07
可以将配置类型设置为lib/dll
作者: b_cup 发布时间: 2011-12-27
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28