what's wrong? error C2664

latofi 2010-02-07 11:13:45
student.h

#ifndef STUDENT_H_
#define STUDENT_H_

#include <iostream>
#include <string>
#include <valarray>

class Student
{
private:
typedef std::valarray<double> ArrayDb;
std::string name;
ArrayDb scores;
std::ostream & arr_out(std::ostream & os) const;
public:
Student(): name("Null Student"), scores() { }
Student(const std::string & s): name(s), scores() { }
explicit Student(int n): name("Nully"), scores(n) { }
Student(const std::string & s, int n): name(s), scores(n) { }
Student(const std::string & s, const ArrayDb & a): name(s), scores(a) { }
Student(const char *str, const double *pd, int n): name(str), scores(pd, n) { }
~Student() { }
double Average() const;
const std::string & Name() const;
double & operator[] (int i);
double operator[] (int i) const;
friend std::istream & operator>> (std::istream & is, Student & stu);
friend std::istream & getline(std::istream & is, Student & stu);
friend std::ostream & operator<< (std::ostream & os, const Student & stu);
};
#endif

student.cpp

#include "student.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;

double Student::Average() const
{
if(scores.size() > 0)
return scores.sum() / scores.size();
else
return 0;
}

const string & Student::Name() const
{
return name;
}

double & Student::operator[] (int i)
{
return scores[i];
}

double Student::operator[] (int i) const
{
return scores[i];
}

ostream & Student::arr_out(ostream & os) const
{
int i;
int lim = scores.size();
if(lim > 0)
{
for(i = 0; i < lim; i++)
{
os << scores[i] << " ";
if(i % 5 == 4)
os << endl;
}
if(i % 5 != 0)
os << endl;
}
else
os << " empty array ";
return os;
}

istream & operator>> (istream & is, Student & stu)
{
is >> stu.name;
return is;
}

istream & getline(istream & is, Student & stu)
{
getline(is, stu.name);
return is;
}

ostream & operator<< (ostream & os, const Student & stu)
{
os << "Scores for " << stu.name << ":\n";
stu.arr_out(os);
return os;
}

--------------------Configuration: student - Win32 Debug--------------------
Compiling...
student.cpp
C:\Documents and Settings\lin\桌面\student.cpp(58) : error C2664: 'getline' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'class Student &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.

student.obj - 1 error(s), 0 warning(s)
...全文
89 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cocat 2010-02-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kktemp234 的回复:]
引用 4 楼 latofi 的回复:
不是我写的,是抄着primer plus上面的类,然后再自己写的定义。自己再对照书上。但是书上也是这样的啊。。


书上有几处在VS中编译不过去的。记得有一处讲内存分配时的free链表节点时就有问题
[/Quote]

书中的不可能都是可运行的程序,这个事很正常的,可能它用的编译器不一样也是可能得
最帅马老师 2010-02-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 latofi 的回复:]
不是我写的,是抄着primer plus上面的类,然后再自己写的定义。自己再对照书上。但是书上也是这样的啊。。
[/Quote]

书上有几处在VS中编译不过去的。记得有一处讲内存分配时的free链表节点时就有问题
latofi 2010-02-07
  • 打赏
  • 举报
回复
不是我写的,是抄着primer plus上面的类,然后再自己写的定义。自己再对照书上。但是书上也是这样的啊。。
traceless 2010-02-07
  • 打赏
  • 举报
回复
Faint~~~

lz能写出这样的代码,这个错误居然不会改。。。
cocat 2010-02-07
  • 打赏
  • 举报
回复
哇~好快~

using std::ostream;
using std::endl;
using std::istream;


改成

using namespace std;
mstlq 2010-02-07
  • 打赏
  • 举报
回复

istream & getline(istream & is, Student & stu)
{
getline(is, stu.name);
return is;
}

改成

istream & getline(istream & is, Student & stu)
{
std::getline(is, stu.name);
return is;
}

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧