好心人帮忙看看这段程序哪里错了 本帖最后由 freebody111 于 2011-03-02 21:18:13 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用VC++6.0编译就报错啊C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(16) : error C2143: syntax error : missing ';' before '<'C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(16) : error C2059: syntax error : '<'C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(16) : error C2238: unexpected token(s) preceding ';'C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(97) : error C2143: syntax error : missing ';' before '<'C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(97) : error C2350: 'Goods::operater' is not a static memberC:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(97) : error C2143: syntax error : missing ';' before '<'C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(98) : error C2143: syntax error : missing ';' before '{'C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(98) : error C2447: missing function header (old-style formal list?) 就是 bool operater<(const Goods &s)const; 这行 答案就是你的operator拼错了,拼成了operater我在vc6下能明显的看到他没有变成蓝色,仔细一看,是你拼错了 #include<iostream>#include<string>#include<map>#include <stdio.h>#include <stdlib.h>using namespace std;class Goods{public: Goods(int number,string name,string tpye,double mprice); void Show(); void Set(int number); void Set(int number,string s_temp); void Set(double mprice); int ReadNumber(); string ReadString(int number); double ReadPrice(); bool operator < (const Goods& s) const; int m_number; string m_name; string m_tpye; double m_price;};class Item{public: Goods m_goods; int m_count;};class Cart{private: map<Goods,int> map_goods; public: void Add(Goods goods,int number = 1); void Remove(Goods goods,int number = 1); double price(); void Details();};Goods::Goods(int number,string name,string tpye,double mprice){ m_number = number; m_name = name; m_tpye = tpye; m_price = mprice;}void Goods::Show(){ cout<<"商品编号 "<<"商品名称 "<<"商品种类 "<<"商品单价"<<endl; cout<<m_number<<" "<<m_name<<" "<<m_tpye<<" "<<m_price<<endl;}void Goods::Set(int number){ m_number = number;}void Goods::Set(int number,string s_temp){ if(number == 1) m_name = s_temp; else if(number == 2) m_tpye = s_temp; else cout<<"没有这种属性"<<endl;}void Goods::Set(double mprice){ m_price = mprice;}int Goods::ReadNumber(){ return m_number;}string Goods::ReadString(int number){ if(number == 1) return m_name; else if(number == 2) return m_tpye; else { cout<<"没有这种属性"<<endl; return 0; }}double Goods::ReadPrice(){ return m_price;}bool Goods::operator < (const Goods& s) const { if(m_number < s.m_number) return true; else if(m_number == s.m_number) return true; }void Cart::Add(Goods goods,int number){ map_goods.insert(pair<Goods,int>(goods,number));}void Cart::Remove(Goods goods,int number){ map<Goods,int>::iterator i = map_goods.find(goods); if(i != map_goods.end()) { i->second -= number; if(i->second <=0 ) map_goods.erase(i); }}double Cart::price(){ double r = 0; for(map<Goods,int>::iterator i=map_goods.begin();i!= map_goods.end(); ++i) r += i->first.m_price; return r;}void Cart::Details(){ for(map<Goods,int>::iterator i=map_goods.begin();i!= map_goods.end(); ++i) { cout << "商品编码" << i->first.m_number <<endl; cout << "商品名称" << i->first.m_name <<endl; cout << "商品种类" << i->first.m_tpye <<endl; cout << "商品单价" << i->first.m_price <<endl; }}int main(){ Cart cart; Goods goods1(1,"木马","玩具",49.5); Goods goods2(2,"饼干","食品",6.3); Goods goods3(3,"洗发水","日用品",19.6); cart.Add(goods1); cart.Add(goods2,2);//同时添加了两件 cart.Add(goods3,4); cart.Details(); cout<<"**********"<<endl; cart.Add(goods2); cart.Details(); cout<<"**********"<<endl; cart.Add(goods2,2); cart.Details(); cout<<"**********"<<endl; cart.Remove(goods2);//移除一件; cart.Details(); cout<<"**********"<<endl; cart.Remove(goods2,5); cart.Details(); return 0;} #include<iostream>#include<string>#include<map>using namespace std;class Goods{public: Goods(int number,string name,string tpye,double mprice); void Show(); void Set(int number); void Set(int number,string s_temp); void Set(double mprice); int ReadNumber(); string ReadString(int number); double ReadPrice(); bool operator < (const Goods& s) const; int m_number; string m_name; string m_tpye; double m_price;};class Item{public: Goods m_goods; int m_count;};class Cart{private: map<Goods,int> map_goods; public: void Add(Goods goods,int number = 1); void Remove(Goods goods,int number = 1); double price(); void Details();};Goods::Goods(int number,string name,string tpye,double mprice){ m_number = number; m_name = name; m_tpye = tpye; m_price = mprice;}void Goods::Show(){ cout<<"商品编号 "<<"商品名称 "<<"商品种类 "<<"商品单价"<<endl; cout<<m_number<<" "<<m_name<<" "<<m_tpye<<" "<<m_price<<endl;}void Goods::Set(int number){ m_number = number;}void Goods::Set(int number,string s_temp){ if(number == 1) m_name = s_temp; else if(number == 2) m_tpye = s_temp; else cout<<"没有这种属性"<<endl;}void Goods::Set(double mprice){ m_price = mprice;}int Goods::ReadNumber(){ return m_number;}string Goods::ReadString(int number){ if(number == 1) return m_name; else if(number == 2) return m_tpye; else { cout<<"没有这种属性"<<endl; return 0; }}double Goods::ReadPrice(){ return m_price;}bool Goods::operator < (const Goods& s) const { if(m_number < s.m_number) return true; else if(m_number == s.m_number) return true; }void Cart::Add(Goods goods,int number){ map_goods.insert(pair<Goods,int>(goods,number));}void Cart::Remove(Goods goods,int number){ map<Goods,int>::iterator i = map_goods.find(goods); if(i != map_goods.end()) { i->second -= number; if(i->second <=0 ) map_goods.erase(i); }}double Cart::price(){ double r = 0; for(map<Goods,int>::iterator i=map_goods.begin();i!= map_goods.end(); ++i) r += i->first.m_price; return r;}void Cart::Details(){ for(map<Goods,int>::iterator i=map_goods.begin();i!= map_goods.end(); ++i) { cout << "商品编码" << i->first.m_number <<endl; cout << "商品名称" << i->first.m_name <<endl; cout << "商品种类" << i->first.m_tpye <<endl; cout << "商品单价" << i->first.m_price <<endl; }}int main(){ Cart cart; Goods goods1(1,"木马","玩具",49.5); Goods goods2(2,"饼干","食品",6.3); Goods goods3(3,"洗发水","日用品",19.6); cart.Add(goods1); cart.Add(goods2,2);//同时添加了两件 cart.Add(goods3,4); cart.Details(); cout<<"**********"<<endl; cart.Add(goods2); cart.Details(); cout<<"**********"<<endl; cart.Add(goods2,2); cart.Details(); cout<<"**********"<<endl; cart.Remove(goods2);//移除一件; cart.Details(); cout<<"**********"<<endl; cart.Remove(goods2,5); cart.Details(); return 0;} bool Goods::operater < (Goods const& s)const{ if(m_number < s.m_number) return true; else if(m_number == s.m_number) return true; return false;//再加这句}1.差一种返回值,2.operater这个单词就是错的吧,应该是operator 求助 如何取得网友验证码 WH_GETMESSAGE和CWPSTRUCT ====高分求助!!===== 如何显示出电脑内已经注册的dll文件列表? VC7.0的程序到别处去出错 单独一个 CopyFile怎么拷贝不了文件? 关于内存泄漏的问题!! 被封杀了吗?为什么进入CSDN的基础类时显示"文本内容中发现无效字符"??? 请高手解决 为什么总是读不到HTTP信息头,怎样才可以取到文件的长度 ? 询问打开文件对话框的显示问题。 请问如何把这个字符串赋给CString
C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(16) : error C2143: syntax error : missing ';' before '<'
C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(16) : error C2059: syntax error : '<'
C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(16) : error C2238: unexpected token(s) preceding ';'
C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(97) : error C2143: syntax error : missing ';' before '<'
C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(97) : error C2350: 'Goods::operater' is not a static member
C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(97) : error C2143: syntax error : missing ';' before '<'
C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(98) : error C2143: syntax error : missing ';' before '{'
C:\Program Files\Microsoft Visual Studio\MyProjects\11111\11.cpp(98) : error C2447: missing function header (old-style formal list?)
我在vc6下能明显的看到他没有变成蓝色,仔细一看,是你拼错了
#include<string>
#include<map>
#include <stdio.h>
#include <stdlib.h>
using namespace std;class Goods
{
public:
Goods(int number,string name,string tpye,double mprice);
void Show();
void Set(int number);
void Set(int number,string s_temp);
void Set(double mprice);
int ReadNumber();
string ReadString(int number);
double ReadPrice();
bool operator < (const Goods& s) const; int m_number;
string m_name;
string m_tpye;
double m_price;
};class Item
{
public:
Goods m_goods;
int m_count;
};class Cart
{
private:
map<Goods,int> map_goods;
public:
void Add(Goods goods,int number = 1);
void Remove(Goods goods,int number = 1);
double price();
void Details();
};Goods::Goods(int number,string name,string tpye,double mprice)
{
m_number = number;
m_name = name;
m_tpye = tpye;
m_price = mprice;
}
void Goods::Show()
{
cout<<"商品编号 "<<"商品名称 "<<"商品种类 "<<"商品单价"<<endl;
cout<<m_number<<" "<<m_name<<" "<<m_tpye<<" "<<m_price<<endl;
}void Goods::Set(int number)
{
m_number = number;
}void Goods::Set(int number,string s_temp)
{
if(number == 1)
m_name = s_temp;
else if(number == 2)
m_tpye = s_temp;
else
cout<<"没有这种属性"<<endl;
}void Goods::Set(double mprice)
{
m_price = mprice;
}
int Goods::ReadNumber()
{
return m_number;
}string Goods::ReadString(int number)
{
if(number == 1)
return m_name;
else if(number == 2)
return m_tpye;
else
{
cout<<"没有这种属性"<<endl;
return 0;
}
}double Goods::ReadPrice()
{
return m_price;
}bool Goods::operator < (const Goods& s) const
{
if(m_number < s.m_number)
return true;
else if(m_number == s.m_number)
return true;
}void Cart::Add(Goods goods,int number)
{
map_goods.insert(pair<Goods,int>(goods,number));
}
void Cart::Remove(Goods goods,int number)
{
map<Goods,int>::iterator i = map_goods.find(goods);
if(i != map_goods.end())
{
i->second -= number;
if(i->second <=0 )
map_goods.erase(i);
}
}
double Cart::price()
{
double r = 0;
for(map<Goods,int>::iterator i=map_goods.begin();i!= map_goods.end(); ++i)
r += i->first.m_price;
return r;
}
void Cart::Details()
{
for(map<Goods,int>::iterator i=map_goods.begin();i!= map_goods.end(); ++i)
{
cout << "商品编码" << i->first.m_number <<endl;
cout << "商品名称" << i->first.m_name <<endl;
cout << "商品种类" << i->first.m_tpye <<endl;
cout << "商品单价" << i->first.m_price <<endl;
}
}
int main()
{
Cart cart;
Goods goods1(1,"木马","玩具",49.5);
Goods goods2(2,"饼干","食品",6.3);
Goods goods3(3,"洗发水","日用品",19.6);
cart.Add(goods1);
cart.Add(goods2,2);//同时添加了两件
cart.Add(goods3,4);
cart.Details();
cout<<"**********"<<endl;
cart.Add(goods2);
cart.Details();
cout<<"**********"<<endl;
cart.Add(goods2,2);
cart.Details();
cout<<"**********"<<endl;
cart.Remove(goods2);//移除一件;
cart.Details();
cout<<"**********"<<endl;
cart.Remove(goods2,5);
cart.Details();
return 0;
}
#include<string>
#include<map>
using namespace std;class Goods
{
public:
Goods(int number,string name,string tpye,double mprice);
void Show();
void Set(int number);
void Set(int number,string s_temp);
void Set(double mprice);
int ReadNumber();
string ReadString(int number);
double ReadPrice();
bool operator < (const Goods& s) const; int m_number;
string m_name;
string m_tpye;
double m_price;
};class Item
{
public:
Goods m_goods;
int m_count;
};class Cart
{
private:
map<Goods,int> map_goods;
public:
void Add(Goods goods,int number = 1);
void Remove(Goods goods,int number = 1);
double price();
void Details();
};Goods::Goods(int number,string name,string tpye,double mprice)
{
m_number = number;
m_name = name;
m_tpye = tpye;
m_price = mprice;
}
void Goods::Show()
{
cout<<"商品编号 "<<"商品名称 "<<"商品种类 "<<"商品单价"<<endl;
cout<<m_number<<" "<<m_name<<" "<<m_tpye<<" "<<m_price<<endl;
}void Goods::Set(int number)
{
m_number = number;
}void Goods::Set(int number,string s_temp)
{
if(number == 1)
m_name = s_temp;
else if(number == 2)
m_tpye = s_temp;
else
cout<<"没有这种属性"<<endl;
}void Goods::Set(double mprice)
{
m_price = mprice;
}
int Goods::ReadNumber()
{
return m_number;
}string Goods::ReadString(int number)
{
if(number == 1)
return m_name;
else if(number == 2)
return m_tpye;
else
{
cout<<"没有这种属性"<<endl;
return 0;
}
}double Goods::ReadPrice()
{
return m_price;
}bool Goods::operator < (const Goods& s) const
{
if(m_number < s.m_number)
return true;
else if(m_number == s.m_number)
return true;
}void Cart::Add(Goods goods,int number)
{
map_goods.insert(pair<Goods,int>(goods,number));
}
void Cart::Remove(Goods goods,int number)
{
map<Goods,int>::iterator i = map_goods.find(goods);
if(i != map_goods.end())
{
i->second -= number;
if(i->second <=0 )
map_goods.erase(i);
}
}
double Cart::price()
{
double r = 0;
for(map<Goods,int>::iterator i=map_goods.begin();i!= map_goods.end(); ++i)
r += i->first.m_price;
return r;
}
void Cart::Details()
{
for(map<Goods,int>::iterator i=map_goods.begin();i!= map_goods.end(); ++i)
{
cout << "商品编码" << i->first.m_number <<endl;
cout << "商品名称" << i->first.m_name <<endl;
cout << "商品种类" << i->first.m_tpye <<endl;
cout << "商品单价" << i->first.m_price <<endl;
}
}
int main()
{
Cart cart;
Goods goods1(1,"木马","玩具",49.5);
Goods goods2(2,"饼干","食品",6.3);
Goods goods3(3,"洗发水","日用品",19.6);
cart.Add(goods1);
cart.Add(goods2,2);//同时添加了两件
cart.Add(goods3,4);
cart.Details();
cout<<"**********"<<endl;
cart.Add(goods2);
cart.Details();
cout<<"**********"<<endl;
cart.Add(goods2,2);
cart.Details();
cout<<"**********"<<endl;
cart.Remove(goods2);//移除一件;
cart.Details();
cout<<"**********"<<endl;
cart.Remove(goods2,5);
cart.Details();
return 0;
}
{
if(m_number < s.m_number)
return true;
else if(m_number == s.m_number)
return true;
return false;//再加这句
}
1.差一种返回值,
2.operater这个单词就是错的吧,应该是operator