C++关于Dll

如下三段代码:
//test.hpp
#ifndef TEST_H
#define TEST_H

#ifndef TEST_API
#define TEST_API
#endif

#ifdef TEST_EXPORTS
#undef TEST_API
#define TEST_API __declspec(dllexport)
#endif
#ifdef TEST_IMPORTS
#undef TEST_API
#define TEST_API __declspec(dllimport)
#endif

namespace test {
class TEST_API test
{
public:
test();
virtual ~test()
{
}
void print(char* str);
void operator<<(char* str);
};
extern TEST_API int test_int;
}
#endif

//test.cpp
#include "test.hpp"
#include <iostream>
namespace test {
TEST_API test::test()
{
}
TEST_API void test::print(char* str)
{
(*this)<<str;
}
TEST_API void test::operator<<( char* str)
{
std::cout<<str<<std::endl;
}
TEST_API int test_int = 101;
}

//T.cpp
#include <iostream>
#include "test.hpp"
using namespace std;

int main(int argc, char *argv[])
{
cout<<"the number is : "<<test::test_int<<endl;
test::test t;
t<<"hello";
return 0;
}
首先使用命令行:cl /LD /MT /EHsc /D"TEST_EXPORTS" test.cpp  生成test.dll以及test.lib
然后命令行: cl /MT /EHsc /D"TEST_IMPORTS" T.cpp /link test.lib 生成T.exe
注意两者使用/MD或者/MT要统一,然后就是生成exe时必须使用/D"TEST_IMPORTS",否则test_int是无法导出的(无法解析的外部符号 "int test::test_int" (?test_int@test@@3HA) )。
但是我由于对C++还不怎么熟悉,有一点很是不明白:如果 test::test t;我写为 test::test t();,那么编译时错误就会为:
T.cpp(9) : error C2296: '<<' : illegal, left operand has type 'test::test (__cdecl *)(void)'
T.cpp(9) : error C2297: '<<' : illegal, right operand has type 'const char [6]'
按理说test类的构造函数就是没有参数,怎么会使用了空括号之后就出现问题呢。为此我加了一个print函数,对print的调用与此一致。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值