• 全部
  • 问答

C++ 语法错误:C2064: 项不会计算为接受 0 个参数的函数

ccql 西安建筑科技大学 在校  2019-07-23 11:23:45
#include<iostream>
#include"Sales_item.h"
using namespace std;
int main() {
Sales_item total;
if (cin >> total) {
Sales_item trans;
while (cin >> trans) {
if (total.isbn() == trans.isbn())
total += trans;
else {
cout << total << endl;
total = trans;
}
}
cout << total << endl;
}
else {
cerr << "no data" << endl;
return -1;
}
return 0;
}
这个是c++ primer plus上面的程序,但是报错了,因为初学所以不太会改,谢谢。
...全文
148 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
csucxy 2019-07-25
前面那个是C++ Primer第四版的,下面这个是第五版的。差别不大(相应的成员变量名、成员函数名稍有不同)。 第四版 成员变量:std::string isbn; 成员函数: bool same_isbn(const Sales_item& rhs) const { return isbn == rhs.isbn; } 第五版 成员变量:std::string bookNo; 成员函数: std::string isbn() const { return bookNo; } ////////////////////////////////////////////////////////////// #ifndef SALESITEM_H // we're here only if SALESITEM_H has not yet been defined #define SALESITEM_H // Definition of Sales_item class and related functions goes here #include <iostream> #include <string> class Sales_item { // these declarations are explained section 7.2.1, p. 270 // and in chapter 14, pages 557, 558, 561 friend std::istream& operator>>(std::istream&, Sales_item&); friend std::ostream& operator<<(std::ostream&, const Sales_item&); friend bool operator<(const Sales_item&, const Sales_item&); friend bool operator==(const Sales_item&, const Sales_item&); public: // constructors are explained in section 7.1.4, pages 262 - 265 // default constructor needed to initialize members of built-in type #if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS) Sales_item() = default; #else Sales_item(): units_sold(0), revenue(0.0) { } #endif Sales_item(const std::string &book): bookNo(book), units_sold(0), revenue(0.0) { } Sales_item(std::istream &is) { is >> *this; } public: // operations on Sales_item objects // member binary operator: left-hand operand bound to implicit this pointer Sales_item& operator+=(const Sales_item&); // operations on Sales_item objects std::string isbn() const { return bookNo; } double avg_price() const; // private members as before private: std::string bookNo; // implicitly initialized to the empty string #ifdef IN_CLASS_INITS unsigned units_sold = 0; // explicitly initialized double revenue = 0.0; #else unsigned units_sold; double revenue; #endif }; // used in chapter 10 inline bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) { return lhs.isbn() == rhs.isbn(); } // nonmember binary operator: must declare a parameter for each operand Sales_item operator+(const Sales_item&, const Sales_item&); inline bool operator==(const Sales_item &lhs, const Sales_item &rhs) { // must be made a friend of Sales_item return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.isbn() == rhs.isbn(); } inline bool operator!=(const Sales_item &lhs, const Sales_item &rhs) { return !(lhs == rhs); // != defined in terms of operator== } // assumes that both objects refer to the same ISBN Sales_item& Sales_item::operator+=(const Sales_item& rhs) { units_sold += rhs.units_sold; revenue += rhs.revenue; return *this; } // assumes that both objects refer to the same ISBN Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs) { Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return ret += rhs; // add in the contents of (|rhs|) return ret; // return (|ret|) by value } std::istream& operator>>(std::istream& in, Sales_item& s) { double price; in >> s.bookNo >> s.units_sold >> price; // check that the inputs succeeded if (in) s.revenue = s.units_sold * price; else s = Sales_item(); // input failed: reset object to default state return in; } std::ostream& operator<<(std::ostream& out, const Sales_item& s) { out << s.isbn() << " " << s.units_sold << " " << s.revenue << " " << s.avg_price(); return out; } double Sales_item::avg_price() const { if (units_sold) return revenue/units_sold; else return 0; } #endif
回复
csucxy 2019-07-25
这才是头文件内容吧?另外好像也不是c++ primer plus中的,好像是c++ primer中的 #ifndef SALESITEM_H #define SALESITEM_H // Definition of Sales_item class and related functions goes here #include <iostream> #include <string> class Sales_item { friend bool operator==(const Sales_item&, const Sales_item&); // other members as before public: // added constructors to initialize from a string or an istream Sales_item(const std::string& book) : isbn(book), units_sold(0), revenue(0.0) { } Sales_item(std::istream& is) { is >> *this; } friend std::istream& operator>>(std::istream&, Sales_item&); friend std::ostream& operator<<(std::ostream&, const Sales_item&); public: // operations on Sales_item objects // member binary operator: left-hand operand bound to implicit this pointer Sales_item& operator+=(const Sales_item&); // other members as before public: // operations on Sales_item objects double avg_price() const; bool same_isbn(const Sales_item& rhs) const { return isbn == rhs.isbn; } // default constructor needed to initialize members of built-in type Sales_item() : units_sold(0), revenue(0.0) { } // private members as before private: std::string isbn; unsigned units_sold; double revenue; }; // nonmember binary operator: must declare a parameter for each operand Sales_item operator+(const Sales_item&, const Sales_item&); inline bool operator==(const Sales_item& lhs, const Sales_item& rhs) { // must be made a friend of Sales_item return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.same_isbn(rhs); } inline bool operator!=(const Sales_item& lhs, const Sales_item& rhs) { return !(lhs == rhs); // != defined in terms of operator== } using std::istream; using std::ostream; // assumes that both objects refer to the same isbn inline Sales_item& Sales_item::operator+=(const Sales_item& rhs) { units_sold += rhs.units_sold; revenue += rhs.revenue; return *this; } // assumes that both objects refer to the same isbn inline Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs) { Sales_item ret(lhs); // copy lhs into a local object that we'll return ret += rhs; // add in the contents of rhs return ret; // return ret by value } inline istream& operator>>(istream& in, Sales_item& s) { double price; in >> s.isbn >> s.units_sold >> price; // check that the inputs succeeded if (in) s.revenue = s.units_sold * price; else s = Sales_item(); // input failed: reset object to default state return in; } inline ostream& operator<<(ostream& out, const Sales_item& s) { out << s.isbn << "\t" << s.units_sold << "\t" << s.revenue << "\t" << s.avg_price(); return out; } inline double Sales_item::avg_price() const { if (units_sold) return revenue / units_sold; else return 0; } #endif
回复
月凉西厢 2019-07-24
isbn只是个成员变量,不是函数,干嘛要加括号
回复
Italink 2019-07-24
把你的类代码也贴出来,不要贴图
回复
ccql 2019-07-24
引用 5 楼 csucxy 的回复:
你确定是书上的程序?

第五版1.6节的代码
回复
csucxy 2019-07-24
你确定是书上的程序?
回复
ccql 2019-07-24
引用 1 楼 Italink 的回复:
把你的类代码也贴出来,不要贴图

这是那个Sales_item头文件
---------------------------------------
#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>


class Sales_item
{
public:
Sales_item(const std::string& book) :isbn(book), units_sold(0), revenue(0.0) {}
Sales_item(std::istream& is) { is >> *this; }
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
public:
Sales_item& operator+=(const Sales_item&);
public:
double avg_price() const;
bool same_isbn(const Sales_item& rhs)const
{
return isbn == rhs.isbn;
}
Sales_item() :units_sold(0), revenue(0.0) {}
public:
std::string isbn;
unsigned units_sold;
double revenue;
};

using std::istream;
using std::ostream;
Sales_item operator+(const Sales_item&, const Sales_item&);
inline bool operator==(const Sales_item& lhs, const Sales_item& rhs)
{
return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.same_isbn(rhs);
}
inline bool operator!=(const Sales_item& lhs, const Sales_item& rhs)
{
return !(lhs == rhs);
}

inline Sales_item& Sales_item::operator +=(const Sales_item & rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
inline Sales_item operator+(const Sales_item & lhs, const Sales_item & rhs)
{
Sales_item ret(lhs);
ret += rhs;
return ret;
}
inline istream& operator>>(istream & in, Sales_item & s)
{
double price;
in >> s.isbn >> s.units_sold >> price;
if (in)
s.revenue = s.units_sold * price;
else
s = Sales_item();
return in;
}
inline ostream& operator<<(ostream & out, const Sales_item & s)
{
out << s.isbn << "\t" << s.units_sold << "\t" << s.revenue << "\t" << s.avg_price();
return out;
}
inline double Sales_item::avg_price() const
{
if (units_sold)
return revenue / units_sold;
else
return 0;
}
#endif
回复
ccql 2019-07-24
引用 2 楼 月凉西厢 的回复:
isbn只是个成员变量,不是函数,干嘛要加括号
........我把括号删了就对了,为什么c++ primer plus那本书上面加了括号?而且其他人的程序好像都没报错
回复
发帖
C++ 语言
创建于2007-09-28

5.9w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2019-07-23 11:23
社区公告
暂无公告