我哪里犯了错误

Where am I making a mistake?

本文关键字:错误      更新时间:2023-10-16

这是一个学校的工作,用于制作一个计算面积和周长的程序编译器/语法错误"预期;在 void 之前"和"在 void 之前预期的主要表达式"。我不知道问题是否在头文件中。

#include"shape.h"
#include<iostream>
using namespace std;

Shapes::Shapes()//base class
   {status=true;}
     void Shapes::launch()
         {cout<<"Which figure are we working on ?"<<endl;
         cout<<"Square(1),Triangle(2),Rectangle(3);"<<endl;
         cout<<"Circle(4) or regular polygons(5)"<<endl;
         cin>>a;}
     void Shapes::aime()//mistake here.
         {             //switch to decide shape
        if (status) switch(a){
         case'1':{Square::Square()
        void Square::readSide()//mistake too.
        {cout<<"The length of one side in metres : t";
        cin>>side;}
    void Square::getsPerimeter()//another mistake.
        {cout<<"The perimeter is "<<(side*4)<<" metres."<<endl;}
        void Square::getsArea(){cout<<"The Area is "<<(side*side)<<" squared 
        metres."<<endl;}//again.
        }

 case'2':{Triangle::Triangle()
    void Triangle::readData()//mistake
     {cout<<"The length of the base in metres : t";
  cin>>base;
  cout<<"The length of the height in metres : t";
   cin>>height;}
   void Triangle::gettArea()//error
  {cout<<"The Area is "<<(height*base/2)<<endl;}
  }
case'3':{Rectangle::Rectangle()
void Rectangle::readDatar()//error
{cout<<"The length in metres : t";
cin>>length;
cout<<"The width in metres : t";
cin>>width;}
void Rectangle::getrArea()//error
{cout<<"Area is "<<(length*width)<<" squared metres."<<endl;}
void Rectangle::getrPerimeter()
{cout<<"Perimeter is "<<(2*(length+width))<<" metres."<<endl;}
}
case'4':{Circle::Circle()
void Circle::readRadius(){cout<<"The length of the radius in metres : t";
cin>>radius;}
void Circle::getCirc()//error
{cout<<"Circumference is "<<(2*3.14159*radius)<<" metres."<<endl;}
Circle::getcArea()//error
{cout<<"Area is "<<(radius*radius*3.14159)<<" squared metres."<<endl;}
}

case'5':{Polygon::Polygon()
void Polygon::readDatap(){cout<<"The number of sides : t";//error
cin>>num;
cout<<"The length of one side in metres : t";
cin>>pside;
cout<<"The length of the apothem in metres : t";
cin>>apothem;}
void Polygon::getpPerimeter()//error
{cout<<"The perimeter is "<<(num*pside)<<" metres."<<endl;}
void Polygon::getpArea()//another error
{cout<<"The area is "<<(num*pside*apothem/2)<<" squared metres."<<endl;}
}   
}   //switch() terminated
}   //aime() terminated
bool Shapes::run(){return status;}
//to keep the app open
 //the classes are derived classes of the base class:Shapes

你在switch语句中定义了一个函数,这在C++中是不允许的(lambdas/函子除外,但它们是另一个时间的主题(。

您需要在 switch 语句之外定义函数,并从那里调用它们。下面是您正在做的事情的示例(这是错误的(:

#include <iostream>
int main(int argc, char** argv) {
    switch(argc) {
    case 1:
        void example(){ std::cout<< "The number of sides : t"; }
        break;
    }
}

这是您需要做的:

#include <iostream>
void example() { std::cout<< "The number of sides : t"; }
int main(int argc, char** argv) {
    switch(argc) {
    case 1:
        example();
        break;
    }
}