- 此问题在此处已有答案**:
Why can't variables be declared in a switch statement?(23个答案)
3天前关闭。
我有两个文件。一个是名为shape.h的头文件。我想通过头文件从类中调用形状。但是文件没有编译。
#include <iostream>
#include <graphics.h>
using namespace std;
class Shape{
protected:
int x;
int y;
public:
Shape(){
x =0;
y = 0;
int gd = DETECT, gm;
char pathtodriver[] = "";
initgraph(&gd, &gm, pathtodriver);
}
virtual void draw();
};
class Rectangle : public Shape{
protected:
int x1;
int y1;
public:
Rectangle(int x, int y,int x1,int y1){
this->x = x;
this->y = y;
this->x1 = x1;
this->y1 = y1;
};
void draw(){
rectangle(200,200,100,250);
closegraph();
};
};
class Circle : public Shape{
protected:
int x1;
public:
Circle(int x,int y,int x1){
this->x = x;
this->y = y;
this->x1 = x1;
};
void draw(){
circle(x,y,x1);
closegraph();
};
};
class Ellipse : public Shape{
protected:
int a;
int b;
int x1;
int y1;
public:
Ellipse(int x,int y,int a, int b,int x1,int y1){
this->x = x;
this->y = y;
this->a = a;
this->b = b;
this->x1 = x1;
this->y1 = y1;
};
void draw(){
ellipse(x,y,a,b,x1,y1);
closegraph();
};
};
和另一个要在形状中调用的文件。
#include "shape.h"
using namespace std;
int main(){
Shape *s1;
cout<<"1.Rectangle"<<
"2.Circle"<<
"3.Ellipse"<<
"4.Exit"<<endl;
int choice;
cout<<"Enter your choice :"<<endl;
cin >> choice;
switch(choice){
case 1:
Rectangle R1(100,100,50,60);
s1 = &R1;
s1->draw();
break;
case 2:
Circle c1(100,100,20);
s1 = &c1;
s1 ->draw();
break;
case 3:
Ellipse e1(100,100,0,360,30,40);
s1 = &e1;
s1->draw();
break;
case 4:
exit(0);
default:
cout<<"Error choice";
}
}
但它给出了以下错误:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\shape.h||In constructor 'Shape::Shape()':|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\shape.h|14|warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp||In function 'int main()':|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|13|error: expected ';' before 'R1'|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|14|error: 'R1' was not declared in this scope|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|20|error: jump to case label [-fpermissive]|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|17|note: crosses initialization of 'Circle c1'|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|21|error: expected ';' before 'e1'|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|22|error: 'e1' was not declared in this scope|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|24|error: jump to case label [-fpermissive]|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|17|note: crosses initialization of 'Circle c1'|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|27|error: jump to case label [-fpermissive]|
C:\Users\rbmoh\OneDrive\Desktop\Docs\C++\labb7.cpp|17|note: crosses initialization of 'Circle c1'|
||=== Build failed: 7 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
请帮帮忙
我试着改变类名,但是效果不好。代码没有编译。
1条答案
按热度按时间hmae6n7t1#
您需要将
case
语句与{
...}
块中的声明进行绑定。您还需要添加break
,以避免陷入下一个case
语句。您还需要接受用户的choice
输入。您当前使用的是未初始化的choice
。示例:
还要注意,
void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
将char*
作为最后一个参数,您需要提供一个const char*
。您需要更改它:Demo