【问题标题】:Creating a array of class objects (C++) [duplicate]创建类对象数组(C++)[重复]
【发布时间】:2015-03-24 01:09:09
【问题描述】:

我一直在尝试制作一个让蚂蚁随机跑来跑去的模拟器(现在......)我想制作一个“蚂蚁”数组来包含我所有的蚂蚁信息和后来的功能。我该怎么做? (如果可能的话,以我的代码为例)(我对java非常了解,所以如果你能把它和java联系起来那就太好了

//inclusions
#include <GLUT/glut.h>
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//declarations

//class declarations


void keyboard (unsigned char key, int x, int y);
void keyboardUp (unsigned char key, int x, int y);
void mouseLocation(int,int);
int onMouse;
int rightClick;
int a = 0;
int mx;
int my;
float red=1.0, blue=0, green=0;

class Ant {
    int x;
    int y;
    int type;

    Ant() { } // private default constructor

public:
    Ant(int nx, int ny, int ntype)
    {
        x = nx;
        y = ny;
        type = ntype;
    }
    void SetX(int swagger)
    {
        x = swagger;
    }
    void SetY(int ny)
    {
        y = ny;
    }
    void SetType(int ntype)
    {
        type = ntype;
    }
    int GetX() { return x; }
    int GetY()  { return y; }
};

//mouse listener methods

void mouseLocation(int x,int y) {
    mx = x;
    my = y;
}
void mouseClicks(int button, int state, int x, int y) {
    mx = x;
    my = y;
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        onMouse = 1;
    }
    if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
        onMouse = 0;
    }
    if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
        rightClick = 0;
    }
    if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
        rightClick = 1;
    }

}

//keylistner methods

void keyboard (unsigned char key, int x, int y)
{
    if (key == 'a')
    {
        a = 1;
    }
}

void keyboardUp (unsigned char key, int x, int y)
{
    if(key == 'a')
    {
        a = 0;
    }
}

//ant methods
int randommove(int position,int speed)
{
    speed++;
    int random = rand() %speed  - speed/2 ;
    position = position + random;
    return position;
}
//drawing methods

Ant ant1(100,100,1);
Ant ant2(50,500,1);
//Here is the where I want to try the ant array something like
//Ant ants[x] and then I would fill in the values with the SetX, SetY... methods
void ant()
{
    int a2 = randommove(ant1.GetX(),2);
    ant1.SetX(a2);
    glLineWidth(1);
    glBegin(GL_LINES);
    glColor4f(1, 0, 0, 1);
    glVertex2i(ant1.GetX(),ant1.GetY());
    glVertex2i(ant1.GetX()+5,ant1.GetY()+5);
    glEnd();
}

void line1()
{

    if(a == 1)
    {
        glLineWidth(2.5);
        glBegin(GL_LINES);
        glVertex2i(80,20);
        glVertex2i(100,400);
        glEnd();
    }
}
void line2()
{
    if(onMouse == 1)
    {
        glLineWidth(2.5);
        glBegin(GL_LINES);
        glVertex2i(200,20);
        glVertex2i(100,400);
        glEnd();
    }
}
void rect1()
{
    if(mx >= 50 && my >= 50)
    {
        int x = 100;
        int y = 100;
        int w = 100;
        int h = 100;
        unsigned int rgba = 0xff0000ff; // red, no alpha
        glBegin(GL_QUADS);
        glColor4f(((rgba>>24)&0xff)/255.0f,
                  ((rgba>>16)&0xff)/255.0f,
                  ((rgba>>8)&0xff)/255.0f,
                  (rgba&0xff)/255.0f);
        glVertex3f(x,y,0);
        glVertex3f(x+w,y,0);
        glVertex3f(x+w,y+h,0);
        glVertex3f(x,y+h,0);
        glEnd();
        glColor4f(1, 1, 1, 1);
    }
}
void rect2()
{
    if(rightClick == 1)
    {
        int w = 100;
        int h = 100;
        unsigned int rgba = 0xff0000ff; // red, no alpha
        glBegin(GL_QUADS);
        glColor4f(((rgba>>24)&0xff)/255.0f,
                  ((rgba>>16)&0xff)/255.0f,
                  ((rgba>>8)&0xff)/255.0f,
                  (rgba&0xff)/255.0f);
        glVertex3f(mx,my,0);
        glVertex3f(mx+w,my,0);
        glVertex3f(mx+w,my+h,0);
        glVertex3f(mx,my+h,0);
        glEnd();
        glColor4f(1, 1, 1, 1);
    }
}
//render drawing methods
void renderScence(void){

    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f(red, green, blue);
    ant();
    line1();
    line2();
    rect1();
    rect2();
    glFlush();

}

//graphics init method
void init(void)
{
    glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
    glutInitWindowSize (500,500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Testing");
    glClearColor (0, 0, 0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 500, 0, 500);
}

//main/graphics calls
int main(int argc,char** argv)
{
    glutInit(&argc, argv);
    init();
    glutDisplayFunc(renderScence);
    glutIdleFunc(renderScence);
    glutMouseFunc(mouseClicks);
    glutPassiveMotionFunc(mouseLocation);
    glutKeyboardUpFunc (keyboardUp);
    glutKeyboardFunc (keyboard);
    glutMainLoop();
    return 0;
}

【问题讨论】:

  • 你的意思是你想要一个Ant对象的数组吗?
  • 是的,你能告诉我吗?
  • 很抱歉昨天没有看到您的评论。如果你还在苦苦挣扎,请在下面查看我的回答。
  • 这里有一个关于未来的提示:你的问题很简单:“我如何制作一个 'Ant' 的数组?”,但是你给了我们一个 226 行的程序,大约有 225 行与数组无关。学习只提取问题代码的相关部分很重要。没有人需要阅读整个程序来回答您的问题,那么为什么要添加它呢?

标签: c++ arrays class object


【解决方案1】:

您可以在main 中创建Ant 对象数组,这与创建任何其他数组的方式相同,但类型为Ant

 Ant ants[25]; // 25 Ant objects   

 /* to set them all the same, as an example */
 for(i=0; i<25; i++)
 { 
     ants[i].SetX(1); // x = 1
     ants[i].SetY(2); // y = 2
     ants[i].SetType(3); // type = 3
 }

【讨论】:

  • 感谢我自己尝试过的解决方案,但我得到(在 OS X 上的 Xcode 中)“调用 Ant 类的私有构造函数”
  • 尝试在Ant 类中将默认构造函数行Ant() { } // private default constructor 移动到public: 下。
猜你喜欢
  • 1970-01-01
  • 2011-10-22
  • 2022-07-20
  • 2018-07-15
  • 2013-12-28
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 2021-07-23
相关资源
最近更新 更多