注释、文件编码导致的诡异的C2039

这里写图片描述
这里写图片描述
编译报错:
2>g:\trunk\thelastday\frameworks\runtime-src\classes\net_external\msg_seq_no\msgseqnomanager.cpp(30): error C2039: “resetSequence”: 不是“MsgSeqNoManager”的成员
看来是找不到方法的定义。

几经周折,得出原因是注释及文件编码导致。
验证过程:
1、检查文件保存格式为UTF-8无bom,尝试更改成UTF-8有bom或BG2312后,成功编译。
2、格式改为UTF-8无bom,在注释最后加入随意字母“重连时重置a”,成功编译。
估计是UTF-8无bom格式下,void resetSequence();这句代码被当作注释了。

需要将友元函数的返回类型改为 ostream&,并在函数内部将输出语句改为 return os,同时在 main 函数内输出对象的语句需要改为 cout << c1 << endl; 和 cout << c2 << endl;。代码如下: ``` #include<iostream> using namespace std; class Complex { private: double real, image; public: Complex(double rel = 0, double img = 0) { real = rel; image = img; } void display() { cout << "(" << real; if (image > 0) cout << "+" << image << "*i)"; else if (image < 0) cout << image << "*i)"; else cout << ")"; } friend Complex operator -(Complex& c1, const Complex& c2); friend Complex operator /(Complex& c1, const Complex& c2); friend bool operator ==(Complex& c1, Complex& c2); friend bool operator !=(Complex& c1, Complex& c2); friend ostream& operator <<(ostream& os, Complex& c); }; Complex operator -(Complex& c1, const Complex& c2) { Complex temp; temp.real = c1.real - c2.real; temp.image = c1.image - c2.image; return temp; } Complex operator /(Complex& c1, const Complex& c2) { Complex FFF; FFF.real = c1.real / c2.real; FFF.image = c1.image / c2.image; return FFF; } bool operator ==(Complex& c1, Complex& c2) { return (c1.real == c2.real) && (c1.image == c2.image); } bool operator !=(Complex& c1, Complex& c2) { return !(c1 == c2); } ostream& operator <<(ostream& os, Complex& c) { os << "(" << c.real; if (c.image > 0) os << "+" << c.image << "*i)"; else if (c.image < 0) os << c.image << "*i)"; else os << ")"; return os; } int main() { Complex c1(10, 9), c2(3, -4), c3; c3 = c1 / 5; c1.display(); cout << "/"; cout << 5; cout << "="; c3.display(); cout << endl; cout << "c1 == c2 ? 结果:"; cout << (c1 == c2) << endl; cout << "c1 != c2 ? 结果:"; cout << (c1 != c2) << endl; cout << c1 << endl; cout << c2 << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值