VS2005碰到的无法解析问题?????急?? 在线等待~!~
———————————LessThan.h————————————
#ifndef LESSTHAN_H
#define LESSTHAN_H
#pragma once
class LessThan{
public:
LessThan(int val)
:_val(val){}
int comp_val()const { return _val;}
void comp_val(int nval) { _val = nval; }
bool operator()(int _value)const;
private:
int _val;
};
#endif
————————————LessThan.cpp————————
#include "LessThan.h"
inline bool LessThan::
operator()(int value) const
{
return value < _val;
}
——————————————main.cpp————————
#include<iostream>
#include<vector>
#include"LessThan.h"
using std::cout;
using std::endl;
int count_less_than(const vector<int>&vec, int comp)
{
LessThan lt(comp);
int count=0;
for(int ix=0; ix<vec.size(); ++ix)
if( lt(vec[ix]) )
++count;
return count;
}
int main()
{
int ia[16]={17,12,44,9,18,45,6,14,23,67,9,0,27,55,8,16};
vector<int>vec(ia, ia+16);
int comp_val=20;
cout<<"Number of elements less than"
<<comp_val<<"are"
<<count_less_than(vec, comp_val)<<endl;
}
——————————————问题如下——————————
在VS2005下,上面编译回出现一个问题:
Main.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall LessThan::operator()(int)const " (??RLessThan@@QBE_NH@Z),该符号在函数 "int __cdecl count_less_than(class std::vector<int,class std::allocator<int> > const &,int)" (?count_less_than@@YAHABV?$vector@HV?$allocator@H@std@@@std@@H@Z) 中被引用
D:\Program_shou2005\Essential-4.9\Debug\Essential-4.9.exe : fatal error LNK1120: 1 个无法解析的外部命令
我的解决是:把.cpp中的operator()(int value) const修改到.h里。 此问题解决了!且输出答案`!`
问个问题:为什么不能分开编译了? 这几天一连都出现这样的问题!`纳闷得很`!`谁给我解决解决