#include <iostream> #include<math.h> #include <vector> //后续使用容器vector需要 #include <math.h>//进行数学运算需要 #include <fstream> using namespace std; class Goods{ string name; int num; double prices; public: Goods(string a,int b,double c){ name=a;num=b;prices=c; } void shows(){ cout <<"序号"<<num<<"名称"<<name<<"价格"<<prices<<"\n"; } }; void jie(){ printf("1 登录 \n"); printf("2 商品信息管理 \n"); printf("3 权限管理 \n"); printf("4 退出 \n"); } void jie2(){ printf("1 增加商品 \n"); printf("2 修改商品 \n"); printf("3 删除商品 \n"); printf("4 退出 \n"); } void add(){ cout<<"请输入增加商品数量 \n"; int a;string q;int w;double e; cin>>a; Goods g[a]; for(int i=0;i<a;i++){ cout <<"请输入商品名字,序号,价格 \n"; cin >>q>>w>>e; g[a].Goods(q,w,e); }帮我查错
时间: 2023-08-06 09:07:57 浏览: 56
在您的 add 函数中,您尝试使用一个变量 `a` 来定义一个数组的大小,但是在 C++ 中,数组的大小必须是一个常量表达式,也就是在编译时就能确定的值,不能使用变量。如果您需要动态确定数组大小,可以使用 `vector` 来代替数组。另外,在您的循环中,应该使用 `i` 作为下标,而不是 `a`。修改后的代码如下:
```c++
void add(){
cout << "请输入增加商品数量:\n";
int a;
cin >> a;
vector<Goods> goodsList;
for (int i = 0; i < a; i++){
string name;
int num;
double prices;
cout << "请输入商品名字、序号和价格:\n";
cin >> name >> num >> prices;
Goods g(name, num, prices);
goodsList.push_back(g);
}
cout << "增加商品成功!\n";
}
```
然后,您可以在其他函数中以类似的方式使用 `vector` 来存储商品信息。同时,为了更好地维护商品信息,建议将 `Goods` 类中的成员变量设置为私有,并提供相应的公有成员函数来访问和修改商品信息。
相关问题
我的头文件是这样写的#include <stdio.h> #include <math.h> #include <fstream> #include <iostream> #include <string>//getline #include <sstream> #include "stb_image.h" #pragma comment(lib, "openGL32.lib") #pragma comment(lib, "glu32.lib") #pragma comment(lib, "glaux.lib") #include <gl/glaux.h> #include "glew.h" #include "glut.h" #include <vector>,请问如何避免报错gl.h included before glew.h呢
在你的头文件中,应该将 `glew.h` 的引入放在 `gl.h` 的引入之前。因此,你应该将 `glew.h` 的引入语句移到 `gl.h` 的引入语句之前,就像这样:
```
#include <stdio.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <string>//getline
#include <sstream>
#include "stb_image.h"
#include <gl/glaux.h>
#include <vector>
#include "glew.h"
#include <gl/glut.h>
#pragma comment(lib, "openGL32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
```
这样就能够避免 `gl.h included before glew.h` 的错误了。
请帮我找出一下代码的错误#include <iostream> #include<math.h> #include <vector> //后续使用容器vector需要 #include <math.h>//进行数学运算需要 #include <fstream> using namespace std; class Goods{ string name; int num; double prices; public: goods(string a,int b,double c){ name=a;num=b;prices=c; } void shows(){ cout <<"序号"<<num<<"名称"<<name<<"价格"<<prices<<"\n"; } }; void jie(){ printf("1 登录 \n"); printf("2 商品信息管理 \n"); printf("3 权限管理 \n"); printf("4 退出 \n"); } void jie2(){ printf("1 增加商品 \n"); printf("2 修改商品 \n"); printf("3 删除商品 \n"); printf("4 退出 \n"); } void add(){ cout<<"请输入增加商品数量 \n"; int a;string q;int w;double e; cin>>a; Goods g[a]; for(int i=0;i<a;i++){ cout <<"请输入商品名字,序号,价格 \n"; cin >>q>>w>>e; g[a].goods(q,w,e); } } int main(){ int o; jie(); cin>>o; switch(o){ case 1:{ } break; case 2:{ jie2(); int o1; cin>>o1; if(o1=1){ add(); //TODO } }jie2(); break ; case 3:{ } break ; case 4:{ printf("再见 \n"); } break ; } int a; cout<<"多少商品 \n"; cin>>a; Goods g[a]; }
这段代码中存在多个错误,下面我将逐一列举并解释:
1. 类名和构造函数名不一致:在类的定义中,将类名定义为Goods,但是在构造函数中将函数名定义为goods。需要将类名和构造函数名保持一致,改为Goods。
2. 数组大小不能在运行时动态输入:在add函数中,代码尝试动态地定义一个大小为a的数组,但是数组大小必须在编译时确定,不能在运行时动态输入。需要将数组大小改为一个固定的值。
3. 数组下标越界:在add函数中,代码定义了一个大小为a的数组g,但是在循环中却使用了g[a],这将导致数组下标越界。因为数组下标是从0开始的,所以需要将g[a]改为g[i]。
4. 条件判断错误:在main函数中,代码使用了条件判断语句if(o1=1),但是应该使用双等号判断相等,而不是单等号赋值。应该改为if(o1==1)。
5. 函数定义多余:在main函数中,代码在case 2中定义了两次jie2函数,其中一次是多余的,需要删除。
下面是修改后的代码,可以参考一下:
```c++
#include <iostream>
#include <math.h>
#include <vector>
#include <fstream>
using namespace std;
class Goods {
string name;
int num;
double prices;
public:
Goods(string a, int b, double c) {
name = a;
num = b;
prices = c;
}
void shows() {
cout << "序号" << num << "名称" << name << "价格" << prices << "\n";
}
};
void jie() {
printf("1 登录 \n");
printf("2 商品信息管理 \n");
printf("3 权限管理 \n");
printf("4 退出 \n");
}
void jie2() {
printf("1 增加商品 \n");
printf("2 修改商品 \n");
printf("3 删除商品 \n");
printf("4 退出 \n");
}
void add() {
const int a = 10; // 固定数组大小为10
Goods g[a];
for (int i = 0; i < a; i++) {
cout << "请输入商品名字,序号,价格 \n";
string q;
int w;
double e;
cin >> q >> w >> e;
g[i] = Goods(q, w, e);
}
}
int main() {
int o;
jie();
cin >> o;
switch (o) {
case 1: {
// TODO
break;
}
case 2: {
jie2();
int o1;
cin >> o1;
if (o1 == 1) {
add();
}
break;
}
case 3: {
// TODO
break;
}
case 4: {
printf("再见 \n");
break;
}
}
int a;
cout << "多少商品 \n";
cin >> a;
Goods g[a];
}
```