咋不行呢?

jinjunweiruan1 2009-06-18 10:02:21
#ifndef SALESITEM_H
#define SALESITEM_H
#include<iostream>
#include<string>

class Sales_item{
public:
std::istream& input(std::istream&in);
std::ostream& output(std::ostream&out);
double avg_price() const;
bool same_isbn (const Sales_item &rhs) const;
{return isbn=rhs.isbn;
}

private:
std::string isbn;
unsigned units_sold;
double revenue;
};
#endif


#include"Sales_item.hpp"

std::istream& Sales_item::input(std::istream& in)
{
double price;
in>>isbn>>units_sold>>price;
if(in)
revenue=units_sold*price;
else{
units_sold=0;
revenue=0;
}
return in;
}

std::ostream& Sales_item::output(std::ostream& out)
{
out<<isbn<<"\t"<<units_sold<<"\t"<<revenue<<"\t"<<avg_price();
return out;
}

double Sales_item::avg_price() const
{
if(units_sold)
return revenue/units_sold;
else
return 0;
}


#include"Sales_item.hpp"
#include<cstdlib>
#include<iostream>
using namespace std;
int main()
{
Sales_item item;
cout<<"Enter some transactions(Ctrl+Z to end:"<<endl;
while(item.input(cin)){
cout<<"The treansaction readed is:"<<endl;
item.output(cout);
cout<<endl;

}
system("pause");


代码绝对正确啊。。我把头文件和CPP放在同一个文件里面。

我拿CPP的源代码编译。怎么不行啊?

1 C:\Documents and Settings\暴··\桌面\新建文件夹 (3)\未命名3.cpp In file included from C:\Documents and Settings\暴··\桌面\新建文件夹 (3)\未命名3.cpp
12 C:\Documents and Settings\暴··\桌面\新建文件夹 (3)\Sales_item.hpp expected unqualified-id before '{' token
C:\Documents and Settings\暴··\桌面\新建文件夹 (3)\未命名3.cpp In function `int main()':
15 C:\Documents and Settings\暴··\桌面\新建文件夹 (3)\未命名3.cpp expected `}' at end of input

这是什么错误腌
...全文
166 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinjunweiruan1 2009-06-19
  • 打赏
  • 举报
回复
啊哦。看了几个英文网站关于我这个问题的。。原因。。。……。……OH。也许是DEV的BUG。
jinjunweiruan1 2009-06-18
  • 打赏
  • 举报
回复
26 E:\CCCCCCCCCCC\DEV-CPP\Makefile.win [Build Error] *** multiple target patterns. Stop.

怎么出现这个错误啊?我把代码按11L的改过了
ameyume 2009-06-18
  • 打赏
  • 举报
回复
jinjunweiruan1 2009-06-18
  • 打赏
  • 举报
回复
……。……原来要这样创建个工程啊。。……。……初学不懂啊哈哈。明白了。

ameyume 2009-06-18
  • 打赏
  • 举报
回复
需要创建一个C++工程,这个会吧?然后把三个文件添加进去,F7编译或者Ctrl + Alt + F7重编译,最后F5运行就可以了
jinjunweiruan1 2009-06-18
  • 打赏
  • 举报
回复
……。……郁闷了。hpp为什么不行啊?

……。……


代码是改了下。但是我现在不明白。具体怎么把头文件和CPP文件组织起来啊

我现在有3个。
Sales_item1.cpp(有int main())
Sales_item.cpp(#include"Sales_item.h"的这个代码)
Sales_item.h(头文件)

我现在该怎么编译呢?
把这3个文件放同一个文件里?任何文件?

然后呢?我要打开哪个代码文件进行编译呢?

MichaelBomb 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wlwshang 的回复:]
#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>

class Sales_item{
public:
std::istream& input(std::istream&in);
std::ostream& output(std::ostream&out);
double avg_price() const;
bool same_isbn (const Sales_item &rhs) const;
{return isbn=rhs.isbn;
} //return does not…
[/Quote]

up
tuziyan99 2009-06-18
  • 打赏
  • 举报
回复
有问题有人帮助解决! 很好啊
pengzhixi 2009-06-18
  • 打赏
  • 举报
回复
bool same_isbn (const Sales_item &rhs) const;
{return isbn=rhs.isbn; //改成return isbn==rhs.isbn;
} //

另外你#include"Sales_item.hpp"//这里的路径改成绝对路径试下
ameyume 2009-06-18
  • 打赏
  • 举报
回复
11楼已经解释了
gleen 2009-06-18
  • 打赏
  • 举报
回复
顶楼上的
wlwshang 2009-06-18
  • 打赏
  • 举报
回复
#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>

class Sales_item{
public:
std::istream& input(std::istream&in);
std::ostream& output(std::ostream&out);
double avg_price() const;
bool same_isbn (const Sales_item &rhs) const;
{return isbn=rhs.isbn;
} //return does not match the bool

private:
std::string isbn;
unsigned units_sold;
double revenue;
};
#endif


#include"Sales_item.hpp" //not .hpp,should be .h
std::istream& Sales_item::input(std::istream& in)
{
double price;
in>>isbn>>units_sold>>price;
if(in)
revenue=units_sold*price;
else{
units_sold=0;
revenue=0;
}
return in;
}

std::ostream& Sales_item::output(std::ostream& out)
{
out < <isbn < <"\t" < <units_sold < <"\t" < <revenue < <"\t" < <avg_price();
return out;
}

double Sales_item::avg_price() const
{
if(units_sold)
return revenue/units_sold;
else
return 0;
}


#include"Sales_item.hpp" //should be .h
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
Sales_item item;
cout < <"Enter some transactions(Ctrl+Z to end:" < <endl;
while(item.input(cin)){
cout < <"The treansaction readed is:" < <endl;
item.output(cout);
cout < <endl;


}
system("pause");
//need a curly bracket and return 0}
jinjunweiruan1 2009-06-18
  • 打赏
  • 举报
回复
……。……解决问题呀。你们拿去编译试试。
光宇广贞 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Loaden 的回复:]
引用楼主 jinjunweiruan1 的帖子:
代码绝对正确啊。。我把头文件和CPP放在同一个文件里面。

楼主很自信啊,不过编译器不通人情...
[/Quote]
jinjunweiruan1 2009-06-18
  • 打赏
  • 举报
回复
那咋的了。。。……。……
maosher 2009-06-18
  • 打赏
  • 举报
回复
#include"Sales_item.hpp"

这句怎么能过得去,
不是主函数的问题
jinjunweiruan1 2009-06-18
  • 打赏
  • 举报
回复
是不是把CPP文件和HPP文件放在一个文件里面

然后打开CPP的文件。编译,这样就可以??

怎么编译指向#include"Sales_item.hpp"这句……、……
jinjunweiruan1 2009-06-18
  • 打赏
  • 举报
回复
怎么编译器指向#include"Sales_item.hpp"这句呢?
老邓 2009-06-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 jinjunweiruan1 的帖子:]
代码绝对正确啊。。我把头文件和CPP放在同一个文件里面。 [/Quote]
楼主很自信啊,不过编译器不通人情...
jinjunweiruan1 2009-06-18
  • 打赏
  • 举报
回复
不是啊,编译器编译的时候。指向的错误的那条代码是#include"Sales_item.hpp"

这句

加载更多回复(2)

64,688

社区成员

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

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