作者franklin1997 (柴柴可爱)
看板C_and_CPP
标题[问题]编译完出现 undefined reference to
时间Fri Mar 31 04:29:28 2017
开发平台(Platform): (Ex: Win10, Linux, ...)
linux
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
大家好,刚接触C++没多久(以前学c),还不太熟悉请多指教。
编译完後出现一群undefined reference to,
我觉得应该是在讲class R的问题,可是检查了R找不出错在哪...
详细状况如下:
/tmp/ccY2jGY2.o: In function `R::R(int, int)':
test.cpp:(.text._ZN1RC2Eii[_ZN1RC5Eii]+0x13): undefined reference to `R::r1'
test.cpp:(.text._ZN1RC2Eii[_ZN1RC5Eii]+0x1c): undefined reference to `R::r2'
/tmp/ccY2jGY2.o: In function `R::now()':
test.cpp:(.text._ZN1R3nowEv[_ZN1R3nowEv]+0x10): undefined reference to `R::r1'
/tmp/ccY2jGY2.o: In function `R::Ans(float)':
test.cpp:(.text._ZN1R3AnsEf[_ZN1R3AnsEf]+0x68): undefined reference to `R::s'
test.cpp:(.text._ZN1R3AnsEf[_ZN1R3AnsEf]+0x6e): undefined reference to `R::r1'
test.cpp:(.text._ZN1R3AnsEf[_ZN1R3AnsEf]+0x7f): undefined reference to `R::s'
test.cpp:(.text._ZN1R3AnsEf[_ZN1R3AnsEf]+0x90): undefined reference to `R::r2'
test.cpp:(.text._ZN1R3AnsEf[_ZN1R3AnsEf]+0x96): undefined reference to `R::r1'
test.cpp:(.text._ZN1R3AnsEf[_ZN1R3AnsEf]+0xff): undefined reference to `R::r1'
test.cpp:(.text._ZN1R3AnsEf[_ZN1R3AnsEf]+0x105): undefined reference to
`R::r1'
collect2: error: ld returned 1 exit status
喂入的资料(Input):
预期的正确结果(Expected Output):
错误结果(Wrong Output):
程式码(Code):(请善用置底文网页, 记得排版)
codePad:
https://codepad.remoteinterview.io/HUAVHCNAJT
#include <iostream>
#include <cmath>
#include <cstdlib>
# include <iostream>
# include <string>
# include <fstream>
using namespace std;
//专门拿来算R的class
class R{
public:
int ans;//所求
R(){
cerr<<"no data to calculate";
}
R(int r1_,int r2_){
r1=r1_;
r2=r2_;
} //还没有输入s值的R值
int now(){
ans=r1;
return ans;
} //有输入s的R值
int Ans(float s_){
//s 只有三种:1 ,0 ,0.5
if (s_==1||s_==0||s_==0.5){
s=s_;
}
else{
cerr<<"need to be 1 0.5 0";
exit(1);
}
r1=r1+k*(s-(1/(1+pow(10,(r2 - r1/400))))); //R的计算公式
ans=r1;
return ans;
}
private:
static int r1,r2;
static float s;
static const int k=32;
};
int main(){
ofstream outFile("filein.txt",ios::out);//先做一个filein.txt
if(!outFile) {
cerr<<"err\n";
exit(1);
}
//输入原始数据
outFile << "k\tr1\tr2\n";
outFile << 32<<"\t"<<1613<<"\t"<<1609<<endl;
outFile << "sa\tsb\n";
outFile << 0 <<"\t"<< (1-0)<<"\t"<<endl;
outFile << 0.5 <<"\t"<< (1-0.5)<<"\t"<<endl;
outFile << 0.5 <<"\t"<< (1-0.5)<<"\t"<<endl;
outFile << 0 <<"\t"<< (1-0)<<"\t"<<endl;
outFile << 0 <<"\t"<< (1-0)<<"\t"<<endl;
outFile << 0 <<"\t"<< (1-0)<<"\t"<<endl;
outFile.close();
//接下来要开始读filein,txt,然後写一个fileout.txt
//宣告,等一下会用到
string headline1, headline2;
int r1,r2,k;
float pointa,pointb;//s值
ifstream inFile("filein.txt",ios::in);
ofstream ouFile("fileout.txt",ios::out);
//成功?
if(!inFile){
cerr<<"filein.txt fail";
exit(1);
}
if(!ouFile){
cerr<<"fileout.txt fail";
exit(1);
}
//开始读档
getline(inFile,headline1);
inFile>>k>>r1>>r2;
R ra(r1,r2); //初始化ra
R rb(r2,r1); //初始化rb
ouFile<< ra.now()<<"\t"<< rb.now()<<"\n"; //ra rb原封不动写入fileout.txt
//开始读s值
while(inFile>>pointa>>pointb){
ouFile<< ra.Ans(pointa)<<"\t"<< rb.Ans(pointb)<<"\n"; //算一算写入fileout.txt//
}
inFile.close();
ouFile.close();
}
补充说明(Supplement):*[m
这里的R是在算Elo rate。
s是指每次胜负的得分。赢者得一分,输了零分,平手0.
5
研究了很久都不了,拜托帮忙了
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 218.164.190.214
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1490905771.A.9FB.html
※ 编辑: franklin1997 (218.164.190.214), 03/31/2017 04:34:08
※ 编辑: franklin1997 (218.164.190.214), 03/31/2017 04:34:49
1F:推 steve1012: 你需要 init static data member 吧 03/31 04:37
2F:→ steve1012: 找个地方初始化你class 的r1 03/31 04:38
3F:推 steve1012: 话说static 真的是你要的吗 03/31 04:39
呃 应该是不用没错(打程式的时候自己混乱了
我睡一下再改改看
甘温
※ 编辑: franklin1997 (36.238.194.129), 03/31/2017 05:00:50
4F:推 loveflames: 问题有够明显,static data member写在类里面只是声 03/31 07:30
5F:→ loveflames: 明,非定义 03/31 07:30
我把那两个static删掉了,谢谢两位大大
※ 编辑: franklin1997 (36.238.194.129), 03/31/2017 11:28:37
6F:推 HMKRL: 难怪我觉得这问题好眼熟XDD 这是算elo的对吧XDD 04/07 03:00