怎么解决error LNK2001,fatal error LNK1120?

newtocsdn 2004-12-01 08:19:47
错误信息:
--------------------Configuration: p118 - Win32 Debug--------------------
Compiling...
p118.cpp
Skipping... (no relevant changes detected)
Tri.cpp
Linking...
Tri.obj : error LNK2001: unresolved external symbol "private: static class std::vector<int,class std::allocator<int> > Triangular::_elems" (?_elems@Triangular@@0V?$vector@HV?$allocator@H@std@@@std@@A)
Debug/p118.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

p118.exe - 2 error(s), 0 warning(s)

Tri.h文件
//#include <iostream>
#include <vector>

using namespace std;

class Triangular
{

public:
static bool is_elem(int);
static void gen_elements(int length);
static void gen_elems_to_value(int value);
// static void display(int length,int beg_pos,ostream &os=cout);

private:
// static const int _max_elems=1024;
static vector<int> _elems;

};

Tri.cpp文件
#include "Tri.h"
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

static const int _max_elems=1024;

bool Triangular::is_elem(int value)
{
if(!_elems.size()||_elems[_elems.size()-1<value])
gen_elems_to_value(value);

vector<int>::iterator found_it;
vector<int>::iterator end_it=_elems.end();

found_it=find(_elems.begin(),end_it,value);
return found_it!=end_it;

}


void Triangular::gen_elements(int length)
{
if(length<0||length>_max_elems)
{
cerr<<"Oops! unable postion.\n";
return ;
}

if(_elems.size()<length)
{
int ix=_elems.size() ? _elems.size()+1 : 1;
for(;ix<=length;++ix)
_elems.push_back(ix*(ix+1)/2);
}
}

void Triangular::gen_elems_to_value(int value)
{
int ix=_elems.size();
if(!ix)
{
_elems.push_back(1);
ix=1;
}

while(_elems[ix-1]<value && ix<_max_elems)
{
//cout<<"elems to value:"<<ix*(ix+1)/2<<endl;
++ix;
_elems.push_back(ix*(ix+1)/2);
}

if(ix==_max_elems)
cerr<<"Triangular Sequence: Oops: value too large"
<<value<<"--exceeds max size of"
<<_max_elems<<endl;
}

main文件
#include "Tri.h"
#include <iostream>

using namespace std;

int main()
{
char ch;
bool more=true;

while(more)
{
cout<<"Please enter value:";
int ival;
cin>>ival;

bool is_elem=Triangular::is_elem(ival);

cout<<ival
<<(is_elem ? "is" : " is not")
<<"an element in the Triangular series.\n"
<<"Another value? (y/n)";

cin>>ch;
if(ch=='n'||ch=='N')
more=false;
}

system("pause");
return 0;
}
...全文
202 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
newtocsdn 2004-12-08
  • 打赏
  • 举报
回复
up again
newtocsdn 2004-12-02
  • 打赏
  • 举报
回复
怎么样独立运用 gen_elements(int length);gen_elems_to_value(int value); ???
请给点指点

newtocsdn 2004-12-02
  • 打赏
  • 举报
回复
呵呵
int a[5] ={1,2,3,4,5};
vector<int> Triangular::_elems(a,a+5);
加到Tri.cpp就ok了
谢谢了
chinadragonss 2004-12-01
  • 打赏
  • 举报
回复
静态变量要初始化啊。

如下例可以初始化。
int a[5] ={1,2,3,4,5};
vector<int> Triangular::_elems(a,a+5);

把这2句加到Tri.cpp或main里都可以吧。
newtocsdn 2004-12-01
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/lnk2001.asp
newtocsdn 2004-12-01
  • 打赏
  • 举报
回复
To chinadragonss(独孤俊):
我在线查了一下msdn
有不少解释
但是没有找到关于初始化的解法
请问
是单步调试?
还是其它方式知道没有初始化的???
谢谢
newtocsdn 2004-12-01
  • 打赏
  • 举报
回复
初始化后
错误更多!

chinadragonss 2004-12-01
  • 打赏
  • 举报
回复
int a[5] ={1,2,3,4,5};
vector<int> Triangular::_elems(a,a+5);
chinadragonss 2004-12-01
  • 打赏
  • 举报
回复
static vector<int> _elems; 没有初始化。

64,649

社区成员

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

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