我的函数get_num
返回一个变量long int
。
我想写下一个运算符:
long int& operator [] (long int i) {
long int a = get_num(i);
int& b = a;
return b;
}
但我得到了下一个错误:
error C2440: 'initializing' : cannot convert from 'long' to 'int &'
error C2440: 'return' : cannot convert from 'int' to 'long &'
如何在不更改功能的情况下修复它get_num
?
任何帮助表示赞赏!
我有一堂课:
class B {
B* next;
long int nom;
long int denom;
public:
long int get_nom() {return nom; }
long int get_denom() {return denom; }
};
class List {
B* head;
public:
long int& operator [] (long int desired_denom) {
// here I search the node that containts the denom that is equal to desired_denom
// and insert it to tmp (it's a pointer to B).
long int a1 = tmp->get_nom()
long int& a2 = a1;
return a2;
}
};
现在在main
,我想做:
int main() {
A a; // assume that it creates the list of B and put values in each node (each B)
// here I want to do:
a[2] = 3; // it should search the node that his denom is equal to 2, and puts 3 instead of his nom
return 0;
}
例如:
如果我的清单是:
(nom=5, denom=6)->(nom=1,denom=8)->(nom=4, denom=2)->NULL
该行a[2]=3
搜索他的denom为2的节点(它是第三个节点)并将他的nom设置为3。
所以在这一行之后,我的清单将是:
(nom=5, denom=6)->(nom=1,denom=8)->(nom=3, denom=2)->NULL