我的家庭作业程序语法有问题。提示是以一种可以正确执行程序片段的方式重载插入器:
for (int i = 0; i < v.size(); ++i)
cout << v[i] << endl;
cout << endl;
所以,这是我到目前为止所做的,但我仍然在cout&lt;&lt; v [i]语句(二进制表达式的操作数无效):
unsigned int seed;
struct node
{
int integer;
double value;
};
double random(unsigned int &seed);
void initialize_vector(vector<node> &v);
template<typename T>
void print_vector(const vector<T> &v);
template<typename T>
ostream &operator <<(ostream &out, const vector<T> &v);
template<typename T>
void output(ostream &out, const vector<T> &v);
int main()
{
vector<node> v(10);
initialize_vector(v);
print_vector(v);
return 0;
}
double random(unsigned int &seed)
{
const int MODULUS = 15749;
const int MULTIPLIER = 69069;
const int INCREMENT = 1;
seed = ((MULTIPLIER * seed) + INCREMENT) % MODULUS;
return double(seed) / double(MODULUS);
}
void initialize_vector(vector<node> &v)
{
for (int i = 0; i < v.size(); ++i)
{
v[i].integer = int (11 * random(seed));
v[i].value = double (11 * random(seed));
}
}
template<typename T>
void print_vector(const vector<T> &v)
{
for (int i = 0; i < v.size(); ++i)
cout << v[i] << endl;
cout << endl;
}
template<typename T>
ostream &operator <<(ostream &out, const vector<T> &v)
{
output(out, v);
return (out);
}
template<typename T>
void output(ostream &out, const vector<T> &v)
{
cout << v.integer;
cout << setprecision(2) << fixed << setw(6) << v.value;
}
我尝试过传递节点而不是T,以及传入和不传入最后3个函数的const引用,所以我再次假设我在void输出函数中的语法是错误的。任何帮助或正确方向的点将不胜感激。
*注意:我无法更改ostream&amp; operator功能。
答案 0 :(得分:2)
cout << v[i] << endl;
如果v是节点的向量,那么你还需要一个函数来重载operator&lt;&lt;对于节点
ostream &operator <<(ostream &out, const node &n)
{
out << n.interger << " " << n.value;
return out;
}