菜鸟遇到菜问题哭了,帮帮忙把

ryam1126 2008-04-18 12:47:44
以下是我按着一本书的代码打的,有47个错误,我看了几个小时没看出错在那
#pragma once
#include<iostream>
#include<string>
#include<queue>
using namespace std;

class Weapon()
{
private:
string _name;
int _power;
public:
Weapon();
Weapon(char name,int power);
string GetName();
int GetPower();
bool operator < (const Weapon &other) const;
bool operator > (const Weapon &other) const;
};
typedef priority_queue< Weapon,vector<Weapon> >Weapons;
//对流的操作支持
ostream &operator<<(osteam &cout,Weapon &weapon);





#include"Weapon.h"

using namespace std;

//让Weapon支持流的操作
ostream &operator<<(ostream &cout,Weapon &weapon)
{
cout<<weapon.getName()<<"("<<weapon.getPower()<<")";
return cout;
}
Weapon::Weapon()
{ }
Weapon::weapon(string name,int power):_name(name),_power(power)
{ }
string weapon::GetName()
{ return _name; }

int weapon::GetPower()
{ return _power; }

bool weapon::operator <(const Weapon &other) const
{
return this->_power < other._power;
}
bool weapon::operator >(const Weapon &other) const
{
return this->_power > other._power;
}




:\msdev98\myprojects\西游记\weapon.h(9) : error C2143: syntax error : missing ';' before 'private'
e:\msdev98\myprojects\西游记\weapon.h(12) : error C2143: syntax error : missing ';' before 'public'
e:\msdev98\myprojects\西游记\weapon.h(14) : error C2144: syntax error : missing ')' before type 'char'
e:\msdev98\myprojects\西游记\weapon.h(14) : error C2059: syntax error : ')'
e:\msdev98\myprojects\西游记\weapon.h(17) : error C2143: syntax error : missing ',' before '&'
e:\msdev98\myprojects\西游记\weapon.h(17) : error C2059: syntax error : '&'
e:\msdev98\myprojects\西游记\weapon.h(17) : error C2270: '<' : modifiers not allowed on nonmember functions
e:\msdev98\myprojects\西游记\weapon.h(17) : error C2803: 'operator <' must have at least one formal parameter of class type
e:\msdev98\myprojects\西游记\weapon.h(17) : error C2805: binary 'operator <' has too few parameters
e:\msdev98\myprojects\西游记\weapon.h(18) : error C2143: syntax error : missing ',' before '&'
e:\msdev98\myprojects\西游记\weapon.h(18) : error C2059: syntax error : '&'
e:\msdev98\myprojects\西游记\weapon.h(18) : error C2270: '>' : modifiers not allowed on nonmember functions
e:\msdev98\myprojects\西游记\weapon.h(18) : error C2803: 'operator >' must have at least one formal parameter of class type
e:\msdev98\myprojects\西游记\weapon.h(18) : error C2805: binary 'operator >' has too few parameters
e:\msdev98\myprojects\西游记\weapon.h(19) : warning C4508: 'Weapon' : function should return a value; 'void' return type assumed
e:\msdev98\myprojects\西游记\weapon.h(20) : error C2923: 'vector' : 'Weapon' is invalid as template argument '#1', type expected
e:\msdev98\myprojects\西游记\weapon.h(7) : see declaration of 'Weapon'
e:\msdev98\myprojects\西游记\weapon.h(20) : error C2923: 'priority_queue' : 'Weapon' is invalid as template argument '#1', type expected
e:\msdev98\myprojects\西游记\weapon.h(7) : see declaration of 'Weapon'
e:\msdev98\myprojects\西游记\weapon.h(22) : error C2061: syntax error : identifier 'osteam'
e:\msdev98\myprojects\西游记\weapon.h(22) : error C2809: 'operator <<' has no formal parameters
e:\msdev98\myprojects\西游记\weapon.cpp(6) : error C2061: syntax error : identifier 'Weapon'
e:\msdev98\myprojects\西游记\weapon.cpp(7) : error C2805: binary 'operator <<' has too few parameters
e:\msdev98\myprojects\西游记\weapon.cpp(8) : error C2065: 'weapon' : undeclared identifier
e:\msdev98\myprojects\西游记\weapon.cpp(8) : error C2228: left of '.getName' must have class/struct/union type
e:\msdev98\myprojects\西游记\weapon.cpp(8) : error C2228: left of '.getPower' must have class/struct/union type
e:\msdev98\myprojects\西游记\weapon.cpp(11) : error C2653: 'Weapon' : is not a class or namespace name
e:\msdev98\myprojects\西游记\weapon.cpp(12) : error C2084: function 'int __cdecl Weapon(void)' already has a body
e:\msdev98\myprojects\西游记\weapon.cpp(13) : error C2653: 'Weapon' : is not a class or namespace name
e:\msdev98\myprojects\西游记\weapon.cpp(13) : error C2373: 'weapon' : redefinition; different type modifiers
e:\msdev98\myprojects\西游记\weapon.cpp(14) : error C2550: 'weapon' : constructor initializer lists are only allowed on constructor definitions
e:\msdev98\myprojects\西游记\weapon.cpp(14) : warning C4508: 'weapon' : function should return a value; 'void' return type assumed
e:\msdev98\myprojects\西游记\weapon.cpp(15) : error C2653: 'weapon' : is not a class or namespace name
e:\msdev98\myprojects\西游记\weapon.cpp(16) : error C2065: '_name' : undeclared identifier
e:\msdev98\myprojects\西游记\weapon.cpp(18) : error C2653: 'weapon' : is not a class or namespace name
e:\msdev98\myprojects\西游记\weapon.cpp(19) : error C2065: '_power' : undeclared identifier
e:\msdev98\myprojects\西游记\weapon.cpp(21) : error C2653: 'weapon' : is not a class or namespace name
e:\msdev98\myprojects\西游记\weapon.cpp(21) : error C2143: syntax error : missing ',' before '&'
e:\msdev98\myprojects\西游记\weapon.cpp(21) : error C2059: syntax error : '&'
e:\msdev98\myprojects\西游记\weapon.cpp(22) : error C2270: '<' : modifiers not allowed on nonmember functions
e:\msdev98\myprojects\西游记\weapon.cpp(23) : error C2673: '<' : global functions do not have 'this' pointers
e:\msdev98\myprojects\西游记\weapon.cpp(23) : error C2227: left of '->_power' must point to class/struct/union
e:\msdev98\myprojects\西游记\weapon.cpp(23) : error C2065: 'other' : undeclared identifier
e:\msdev98\myprojects\西游记\weapon.cpp(23) : error C2228: left of '._power' must have class/struct/union type
e:\msdev98\myprojects\西游记\weapon.cpp(25) : error C2653: 'weapon' : is not a class or namespace name
e:\msdev98\myprojects\西游记\weapon.cpp(25) : error C2143: syntax error : missing ',' before '&'
e:\msdev98\myprojects\西游记\weapon.cpp(25) : error C2059: syntax error : '&'
e:\msdev98\myprojects\西游记\weapon.cpp(26) : error C2270: '>' : modifiers not allowed on nonmember functions
e:\msdev98\myprojects\西游记\weapon.cpp(27) : error C2673: '>' : global functions do not have 'this' pointers
e:\msdev98\myprojects\西游记\weapon.cpp(27) : error C2227: left of '->_power' must point to class/struct/union
e:\msdev98\myprojects\西游记\weapon.cpp(27) : error C2228: left of '._power' must have class/struct/union type

...全文
246 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
scq2099yt 2008-04-18
  • 打赏
  • 举报
回复
up
yupengchen951124 2008-04-18
  • 打赏
  • 举报
回复
怎么把西游记也显示出来了……呵呵
lyfyes 2008-04-18
  • 打赏
  • 举报
回复
你的;用法,有用错地方啊.如果要调试的话,可以将代码发给我,我帮你调116509625@163.com
Eleven 2008-04-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 shelliu 的回复:]
#include <iostream>
#include <string>
#include <queue>
是不是该加个.h啊?
[/Quote]
那是C++的标准库,不加.h
makun 2008-04-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 shelliu 的回复:]
#include <iostream>
#include <string>
#include <queue>
是不是该加个.h啊?
[/Quote]

你等级怎么升上来的?
STL的内容。
shelliu 2008-04-18
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <queue>
是不是该加个.h啊?
ToperRay 2008-04-18
  • 打赏
  • 举报
回复
看仔细了再敲,怀疑你敲错了。
fox000002 2008-04-18
  • 打赏
  • 举报
回复
代码写得规范些。





// weapon.h
#ifndef WEAPON_H_
#define WEAPON_H_

#pragma once

#include <iostream>
#include <string>
#include <queue>
using namespace std;

class Weapon
{
private:
string _name;
int _power;
public:
Weapon();
Weapon(string name,int power);
string GetName();
int GetPower();
bool operator < (const Weapon &other) const;
bool operator > (const Weapon &other) const;
};
typedef priority_queue < Weapon,vector <Weapon> >Weapons;
//对流的操作支持
ostream &operator << (ostream &cout, Weapon &weapon);

#endif // WEAPON_H_

// weapon.cpp
#include "weapon.h"

using namespace std;

//让Weapon支持流的操作
ostream &operator <<(ostream &cout,Weapon &weapon)
{
cout <<weapon.GetName() <<"(" <<weapon.GetPower() <<")";
return cout;
}

Weapon::Weapon()
{ }

Weapon::Weapon(string name,int power):_name(name),_power(power)
{ }
string Weapon::GetName()
{ return _name; }

int Weapon::GetPower()
{ return _power; }

bool Weapon::operator <(const Weapon &other) const
{
return this->_power < other._power;
}
bool Weapon::operator >(const Weapon &other) const
{
return this->_power > other._power;
}


yaoy053619 2008-04-18
  • 打赏
  • 举报
回复
class Weapon() 不写括号。class Weapon
流运算符<<间不用加空格
yaoy053619 2008-04-18
  • 打赏
  • 举报
回复
class Weapon()应该写成class Weapon,不用加括号。
ostream &operator < <(osteam &cout,Weapon &weapon);
去掉<<中间的空格。
其他的错误较简单。
我编译过你的程序了,按照上述办法可以编通。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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