我哪里弄错了?

时间:2017-04-12 10:57:50

标签: c++

这是一项学校计划,用于计算计算区域和周边的计划 编译器/语法错误"期望;在void"和"期望的primary-expression在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

1 个答案:

答案 0 :(得分:2)

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

您需要在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;
    }
}