C++编程问题(编译出错)-2

happiness2080 2008-11-27 07:28:37
// GreedKnapsack.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <string.h>
//using namespace std;

void input()
{
static int c;
cout<<"输入背包容量:";
cin>>c;
cout<<endl;
static int n;
cout<<"输入物品数量: ";
cin>>n;
cout<<endl;
float *w=new float[n];
float *v=new float[n];
float *x=new float[n];
static float value;
for(int j=1;j<=n;j++)
{
float a,b;
cout<<"输入第"<<j<<"种物品重量:";
cin>>a;
w[j]=a;
cout<<"价值(大于0):";
cin>>b;
v[j]=b;
x[j]=v[j]/w[j];
cout<<"x:"<<x[j]<<endl;
}
}

static void Greedy()
{
int cu=c;
int i;
int temp=0;
for(i=0;i<n;i++){
temp=sortResult[i];
if(w[temp]>cu){
break;
}
x[temp]=1;
cu-=w[temp];
value=value=value+v[temp];
}
if(i<=n)
{
x[temp]=cu/w[temp];
value=value+x[temp]*v[temp];
}
return;
}

static void sortPerValue()
{
int i,j,index=0,k=0;
float temp;
for(i=0;i<n;i++)
sortResult[i]=0;
for(i=0;i<n;i++)
{
temp=x[i];
index=i;
for(j=0;j<=n;j++)
{
if((temp<x[j])&&(x[j]!=0))
index=j;
}
sortResult[k++]=index;
x[index]=0;
}
return;
}

static void output()
{
int i;
cout<<"可能方案之一为:"<<endl;
cout<<"总价值="<<value<<endl;
for(i=0;i<n;i++)
cout<<x[i]<<" "<<endl;
return;
}


void main(int argc, char* argv[])
{
input();
Greedy();
output();
}

Compiling...
GreedKnapsack.cpp
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(39) : error C2065: 'c' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(42) : error C2065: 'n' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(43) : error C2065: 'sortResult' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(43) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(44) : error C2065: 'w' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(44) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(47) : error C2065: 'x' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(47) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(47) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(48) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(49) : error C2065: 'value' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(49) : error C2065: 'v' : undeclared identifier
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(49) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(53) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(53) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(53) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(54) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(54) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(64) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(64) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(67) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(67) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(71) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(71) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(74) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(74) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(75) : error C2109: subscript requires array or pointer type
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(75) : error C2106: '=' : left operand must be l-value
G:\学习\C++\GreedKnapsack\GreedKnapsack.cpp(86) : error C2109: subscript requires array or pointer type
执行 cl.exe 时出错.

GreedKnapsack.obj - 1 error(s), 0 warning(s)
...全文
116 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
spofmy 2008-11-27
  • 打赏
  • 举报
回复
问题太多了啊,先把基础学好了再出来贴啊
luojc714 2008-11-27
  • 打赏
  • 举报
回复
虽然你的那个c是静态的,但是它毕竟是局部作用域的,编译器当然要报错了。
程序太长了,其它的就不看了。
a951753 2008-11-27
  • 打赏
  • 举报
回复
楼主你弄那么多静态的出来干嘛,不会用就不要用吗,弄得我晕头转向的
xxgamexx 2008-11-27
  • 打赏
  • 举报
回复
呵呵 慢慢来吧
pusshi 2008-11-27
  • 打赏
  • 举报
回复
正如上面各位所说,是作用域的问题

你可以在函数里定义一名字空间
namespace 名字{ }
toadzw 2008-11-27
  • 打赏
  • 举报
回复
static int n;
cout <<"输入物品数量: ";
cin>>n;
cout <<endl;
float *w=new float[n];
float *v=new float[n];
float *x=new float[n];
static float value;
这些数据都要重新定义一下,你定义在一个函数中,只有在这个函数被调用时才有意义;
happiness2080 2008-11-27
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream.h>
#include <string.h>
//using namespace std;
static int c;
static int n;
static float *w=new float[n];
static float *v=new float[n];
static float *x=new float[n];
static float value;
static int *sortResult=new int[n];
void input()
{

cout<<"输入背包容量:";
cin>>c;
cout<<endl;

cout<<"输入物品数量: ";
cin>>n;
cout<<endl;
//float *w=new float[n];
//float *v=new float[n];
//float *x=new float[n];
//static float value;
for(int j=1;j<=n;j++)
{
float a,b;
cout<<"输入第"<<j<<"种物品重量:";
cin>>a;
w[j]=a;
cout<<"价值(大于0):";
cin>>b;
v[j]=b;
x[j]=v[j]/w[j];
sortResult[j]=j;
cout<<"x:"<<x[j]<<endl;
}
}

static void Greedy()
{
int cu=c;
int i;
int temp=0;
for(i=0;i<n;i++){
temp=sortResult[i];
if(w[temp]>cu){
break;
}
x[temp]=1;
cu-=(int)w[temp];
value=value+v[temp];
}
if(i<=n)
{
x[temp]=cu/w[temp];
value=value+x[temp]*v[temp];
}
return;
}

static void sortPerValue()
{
int i,j,index=0,k=0;
float temp;
for(i=0;i<n;i++)
sortResult[i]=0;
for(i=0;i<n;i++)
{
temp=x[i];
index=i;
for(j=0;j<=n;j++)
{
if((temp<x[j])&&(x[j]!=0))
index=j;
}
sortResult[k++]=index;
x[index]=0;
}
return;
}

static void output()
{
int i;
cout<<"可能方案之一为:"<<endl;
cout<<"总价值="<<value<<endl;
for(i=0;i<n;i++)
cout<<x[i]<<" "<<endl;
return;
}


void main(int argc, char* argv[])
{
input();
Greedy();
output();
}

请问,我改成这样行不行啊?
zxianrong 2008-11-27
  • 打赏
  • 举报
回复
static void Greedy()
{
int cu=c;
int i;
int temp=0;
for(i=0;i <n;i++){
temp=sortResult[i];
if(w[temp]>cu){
break;
}

这里 用到C了,编译的时候并不能认出他是个静态变量,因为
void input()
{
static int c;
cout < <"输入背包容量:";
cin>>c;
...
}
你把他定义在函数内部了,你把static int c;移到函数外就识别了。还有啊sortResult()这个函数没定义啊。。
还很多问题。。建议LZ认真看看
xhs_lh04 2008-11-27
  • 打赏
  • 举报
回复
函数里的局部变量作用域仅仅只在此函数内,代码块内{}的定义变量作用域仅仅在块内
xhs_lh04 2008-11-27
  • 打赏
  • 举报
回复
static void Greedy()
{
int cu=c; //c<--no define
int i;
int temp=0;
for(i=0;i <n;i++){
temp=sortResult[i]; //sortResult <-- no define
if(w[temp]>cu){
break;
}

还有很多很多,楼主先要弄清变量作用域,再来改此程序,那不用别人帮忙了'
luogailong 2008-11-27
  • 打赏
  • 举报
回复
static void Greedy()
{
int cu=c;
int i;
int temp=0;
for(i=0;i <n;i++){
temp=sortResult[i];
if(w[temp]>cu){
break;
}
以上面这一小段程序为例,你的c和n都没有定义,编译当然错
  • 打赏
  • 举报
回复
囧~~
static void Greedy()
{
int cu=c; 这个没定义
int i;

65,211

社区成员

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

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