不能在这里初始化一个班级成员! [英] cannot initialize a class member here!

查看:85
本文介绍了不能在这里初始化一个班级成员!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问了一个问题:未定义的符号c ++问题 [ ^ ]

i接受了建议,我改变了代码。但仍有错误: http://upcity.ir/images2/65645793150446125725.jpg [ ^ ]



i asked question here:undefined symbol c++ problem[^]
i took the advice and i changed the code. but there is still errors: http://upcity.ir/images2/65645793150446125725.jpg[^]

#include<graphics.h>
#include<dos.h>
#include<conio.h>
class car{
int spX[20]={0};
int spY[20]={0};
void Drawcar(){
setcolor(15);
line(spX[1],spY[1],spX[2],spY[2]);
line(spX[2],spY[2],spX[3],spY[3]);
line(spX[3],spY[3],spX[4],spY[4]);
line(spX[4],spY[4],spX[4],spY[4]);
line(spX[1],spY[1],spX[4],spY[4]);
pieslice(100,400,40,10,25);
arc(spX[0],spY[0]=75;,90,190,30);
circle(100,400,25);
}

void move_sp(int a){
for(int i=0;i<=20;i++){
spX[i]+=a;
spY[i]+=a;
Drawcar();
}
};

}
int main(){
int g=DETECT,m;
initgraph(&g,&m,"c:\\turboc3\\bgi");


spX[0]=75;
spY[0]=375;
spX[1]=75;
spY[1]=345;
spX[2]=125;
spY[2]=300;
spX[3]=225;
spY[3]=330;
spX[4]=275;
spY[4]=345;
spX[5]=300;
spY[5]=350;

move_sp(1);


getch();
closegraph();
return 0;
}

推荐答案

我刚看了你原来的错误陈述。因为你正在使用''C''AND_NOT''C ++'',(试着给一个人一个提示,hmmph)摆脱:''class car {''并制作一个更有序的结构,包括只有常量和变量,没有函数。



赞:

I just looked at your original error statement. Since you are working in ''C'' AND_NOT ''C++'',(try and give a guy a hint, hmmph)get rid of the: ''class car{'' and make a more ordered struct, including only the constants and variables, and no functions.

Like:
struct spCar{
              int spX;
              int spY;
            }
struct edgeLine{
              spCar strtPT1;
              spCar strtPT2;
               }

struct theShape{
             edgeLine line1;
             edgeLine line2;
             edgeLine line3;
             edgeLine line4;
               }

enum Shape{line, angle, triangle, rectangle, square, ...};

    edgeLine L1, L2, L3, L4, L5, L6, L7, L8;
    spCar pt1, pt2, pt3, pt4, pt5, pt6, pt7, pt8;


bool SetPoints(int nShape)
{
    switch(nShape)
   {
     case square:
     pt1.spX = 75;
     pt1.spY = 375;
     pt2.spX = 75;
     pt2.spY = 345;
     pt3.spX = 125;
     pt3.spY = 300;
     pt4.spX = 225; ///There is some kind of error in your///
     pt4.spY = 330; ///Original Drawcar function.///
     pt5.spX = 275;
     pt5.spY = 345;
     pt6.spX = 300;
     pt6.spY = 350;
     break;

    ....
   }

    return true;
}

void drawShape(int shpNum, int nColor)
{
    setcolor(nColor);
    switch(shpNum) 
   {
      case square://4 sided object
      SetPoints(4, square);
      L1={pt1,pt2}; L2={pt2,pt3}; L3={pt3,pt4}; L4={pt4,pt1}; 
        line(L1);
        line(L2);
        line(L3);
        line(L4);
      break;
      case 1:
        line();
        line();
        line();
        line();
      break;
   }
}



然后你可以将其他变量或静态const类型数据放入其中,如果需要的话。

然后是图形函数:


Then you could put other variables or static const type data in it as if needed.
Then next a Graphics function:

void DrawCar(spCar car, int nColor, int shpNum)
{
    setcolor(nColor);
    pieslice(100,400,40,10,25);
    arc((car.spX, car.spY = 75),90,190,30);//not sure of your arc function type//
    circle(100,400,25);
    drawShape(shpNum,nColor);

}





重写你的移动功能:



Rewrite your move function:

void move_sp(int a, spCar car, int shpNum)
{
    for(int i = 0; i < = 20; i++)
    {
       car.spX += a;
       car.spY +=a;
       DrawCar(car, setcolor(15), shpNum);
    }
}





那么你可以做你的主要功能:





then you can do your ''main'' function:

int main()
{
    int g=DETECT,m; spCar Car; int Shape; int Color;  
    //to do set Color to a number that works, and decide on a shape(square for the example)//
    //and set the initial point 'Car'.     
    initgraph(&g,&m,"c:\\turboc3\\bgi");
    DrawCar(car, Color, Shape);
    move_sp(1, car, Shape);
    getch();
    closegraph();
    return 0;
}





或类似的东西......



Or something like that...


当我说将定义spX和spY移到Main函数之外我并不是指另一个函数!

它们需要在任何函数之外,以便它们中的任何一个都可以访问相同的数据。您在函数中定义的变量仅对该函数是本地的。
When I said "move the definition spX and spY outside the Main function" I didn''t mean into a different function!
They need to be outside any function, so that any of them can access the same data. And variable you define within a function is local to that function only.


这篇关于不能在这里初始化一个班级成员!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆