连接时出错.

xieqi 2009-12-23 10:58:56
fatal error LNK1120: 1 unresolved externals
我想写个dll给其他程序调用map容器

public class Class2
{
public:
static map<int ,int> word;
static map<int ,int>::iterator it;
};
public ref class Class1
{
public:
static void del(int x);

};
void Class1::del(int x)
{

Class2::word.erase(x);
}

只要把 static 去掉,就可以,为什么?

public class Class2
{
public:
map<int ,int> word;
map<int ,int>::iterator it;
};
public ref class Class1
{
public:
static void del(int x);

};
Class2 ob;
void Class1::del(int x)
{

ob.word.erase(x);
}

...全文
134 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
DLevel 2009-12-23
  • 打赏
  • 举报
回复
我感觉,用static没错,但是,在你这个实现里面就有问题了,
因为 static的成员函数,只能通过类来调用,而不能通过对象调用.
楼主在测试DLL的时候,先生称对象 比如 Class2 Object;
Object.del(××)不知道是不是这样的。
在你这个里面,想掉del的应该是这样
class2::del(**)
taodm 2009-12-23
  • 打赏
  • 举报
回复
因为那是完整规定的另外半部分。
楼主,搞C++还是要找本好点的教材认真打牢基础的。
xieqi 2009-12-23
  • 打赏
  • 举报
回复
static int ab;//这个就可以啊
简单变量怎么就不用初始化呢?
yanjingfei 2009-12-23
  • 打赏
  • 举报
回复
DLL里面可以有静态变量的。楼主的问题是静态变量没有初始化的问题。
dskit 2009-12-23
  • 打赏
  • 举报
回复
c/c#?
lovesea42 2009-12-23
  • 打赏
  • 举报
回复

map<int ,int> Class1::word;
map<int ,int>::iterator Class1::it;

static成员除了在类定义部分声明外,还需要在实现文件中定义.
xieqi 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 taodm 的回复:]
楼主啊,你这基础也太不扎实了吧,static的数据成员要在一个cpp里给出实现啊。
[/Quote]
我是刚学不久.cpp里给出实现,是什么意思?
但是class Class1
{
public:
void del(int x);
map<int ,int> word;
static int ab;//这个就可以啊
};
xieqi 2009-12-23
  • 打赏
  • 举报
回复
class Class1
{
public:
void del(int x);
static map<int ,int> word;
};
void Class1::del(int x)
{
word.erase(x);
}
int _tmain(int argc, _TCHAR* argv[])
{
Class1 aa;
aa.del(300);
return 0;
}

这样报错
error LNK2001: unresolved external symbol "public: static class std::map<int,int,struct std::less<int>,class std::allocator<struct std::pair<int const ,int> > > Class1::word" (?word@Class1@@2V?$map@HHU?$less@H@std@@V?$allocator@U?$pair@$$CBHH@std@@@2@@std@@A)

fatal error LNK1120: 1 unresolved externals

class Class1
{
public:
void del(int x);
map<int ,int> word;
};
void Class1::del(int x)
{
word.erase(x);
}
int _tmain(int argc, _TCHAR* argv[])
{
Class1 aa;
aa.del(300);
return 0;
}

这样通过.为什么?指去掉一个static
taodm 2009-12-23
  • 打赏
  • 举报
回复
楼主啊,你这基础也太不扎实了吧,static的数据成员要在一个cpp里给出实现啊。
macrojj 2009-12-23
  • 打赏
  • 举报
回复
你在别的cpp里用这个都不得行的。何况dll
macrojj 2009-12-23
  • 打赏
  • 举报
回复
static 只在本编译单元有效。
不会作为外部符号 被迁出去的。
xieqi 2009-12-23
  • 打赏
  • 举报
回复
class Class1
{
public:
void del(int x);
static map<int ,int> word;
static map<int ,int>::iterator it;
};
void Class1::del(int x)
{
word.erase(x);
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
xieqi 2009-12-23
  • 打赏
  • 举报
回复
我不写dll了,改成命令行程序,也会报这个错
lovesea42 2009-12-23
  • 打赏
  • 举报
回复
因为DLL是在程序运行时加载的, 所以声明实例的时候由于DLL里的static变量不可能预先初始化,所以就出现上面的问题,而用指针就不会,因为指针在编译的时候不需要实例化。
冻结 2009-12-23
  • 打赏
  • 举报
回复
dll里不该用static的东西吧。
static是具有全局性质的东西。
反正symbian的DLL不能有static。

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧