Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » shows error when creating class DATA in Xcode for c project

This thread is locked; no one can reply to it. rss feed Print
shows error when creating class DATA in Xcode for c project
xtech45
Member #15,430
December 2013

#include <stdio.h>
#include <allegro5/allegro.h>

class DATA {
public :
ALLEGRO_MUTEX *mutex;
ALLEGRO_COND *cond;
float posiX;
float posiY;
bool modi_x;
bool ready;
DATA() :
mutex(al_create_mutes()),
cond(al_create_cond()),
posiX(0);
posiY(0);
modi_X(false),
ready(false){}
-DATA()
{
al_destroy_mutex(mutex);
al_destroy_cond(cond);
}
};

const float FPS =30;
const int SCREEN_H =640;

int main()
{

return 0;
}

the following error appears when i type this in Xcode and the language is c

1) Excepted ';' after top level deflator
2) unknown type name class

why is this happening and should i need to include any header or is there any work around for this ??? ??? ???

Edgar Reynaldo
Member #8,592
May 2007
avatar

xtech45
Member #15,430
December 2013

I have changed that and i still get the same error and it shows an error at the starting itself stating UNKNOWN TYPE NAME and EXPECTED SEMICOLON

under which header does the class comes ????????????

Edgar Reynaldo
Member #8,592
May 2007
avatar

xtech45
Member #15,430
December 2013

#SelectExpand
1 2#include <stdio.h> 3#include <allegro5/allegro.h> 4 5class DATA { 6 7 public : 8 9 ALLEGRO_MUTEX *mutex; 10 ALLEGRO_COND *cond; 11 float posiX; 12 float posiY; 13 bool modi_x; 14 bool ready; 15 DATA() : 16 mutex(al_create_mutes()), 17 cond(al_create_cond()), 18 posiX(0), 19 posiY(0), 20 modi_X(false), 21 ready(false){} 22 -DATA() 23 { 24 al_destroy_mutex(mutex); 25 al_destroy_cond(cond); 26 } 27}; 28 29const float FPS =30; 30const int SCREEN_H =640; 31 32int main() 33{ 34 35 return 0; 36}

this is actually a tutorial from the allegro site itself and
http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Threads
this is the link of the code

Elias
Member #358
May 2000

It's "~DATA" not "-DATA". What if you just copy and paste the complete tutorial?

--
"Either help out or stop whining" - Evert

Thomas Fjellstrom
Member #476
June 2000
avatar

Also make sure you created the file as a c++ file, rather than c.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Member #8,592
May 2007
avatar

xtech45
Member #15,430
December 2013

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3 4class DATA{ 5 6 public: 7 8 ALLEGRO_MUTEX *mutex; 9 ALLEGRO_COND *cond; 10 float posiX; 11 float posiY; 12 bool modi_X; 13 bool ready; 14 15 DATA() : mutex(al_create_mutex()), 16 cond(al_create_cond()), 17 posiX (0), 18 posiY (0), 19 modi_X(false), 20 ready (false) {} 21 22 ~DATA(){ 23 24 al_destroy_mutex(mutex); 25 al_destroy_cond(cond); 26 27 } 28 29}; 30 31const float FPS = 30; 32const int SCREEN_W = 640; 33const int SCREEN_H = 480; 34const int BOUNCER_SIZE = 32; 35 36static void *Func_Thread(ALLEGRO_THREAD *thr, void *arg); 37 38int main(int argc, char **argv){ 39 40 ALLEGRO_DISPLAY *display = NULL; 41 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 42 ALLEGRO_TIMER *timer = NULL; 43 ALLEGRO_BITMAP *bouncer = NULL; 44 ALLEGRO_THREAD *thread_1 = NULL; 45 ALLEGRO_THREAD *thread_2 = NULL; 46 47 bool redraw = true; 48 49 if(!al_init()) { 50 fprintf(stderr, "failed to initialize allegro!\n"); 51 return -1; 52 } 53 54 if(!al_install_mouse()) { 55 fprintf(stderr, "failed to initialize the mouse!\n"); 56 return -1; 57 } 58 59 timer = al_create_timer(1.0 / FPS); 60 if(!timer) { 61 fprintf(stderr, "failed to create timer!\n"); 62 return -1; 63 } 64 65 display = al_create_display(SCREEN_W, SCREEN_H); 66 if(!display) { 67 fprintf(stderr, "failed to create display!\n"); 68 al_destroy_timer(timer); 69 return -1; 70 } 71 72 bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE); 73 if(!bouncer) { 74 fprintf(stderr, "failed to create bouncer bitmap!\n"); 75 al_destroy_display(display); 76 al_destroy_timer(timer); 77 return -1; 78 } 79 80 al_set_target_bitmap(bouncer); 81 al_clear_to_color(al_map_rgb(255, 0, 255)); 82 al_set_target_bitmap(al_get_backbuffer(display)); 83 event_queue = al_create_event_queue(); 84 85 if(!event_queue) { 86 fprintf(stderr, "failed to create event_queue!\n"); 87 al_destroy_bitmap(bouncer); 88 al_destroy_display(display); 89 al_destroy_timer(timer); 90 return -1; 91 } 92 93 al_register_event_source(event_queue, al_get_display_event_source(display)); 94 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 95 al_register_event_source(event_queue, al_get_mouse_event_source()); 96 al_clear_to_color(al_map_rgb(0,0,0)); 97 al_flip_display(); 98 al_start_timer(timer); 99 100 DATA data; 101 102 thread_1 = al_create_thread(Func_Thread, &data); 103 al_start_thread(thread_1); 104 105 al_lock_mutex(data.mutex); 106 while (!data.ready){ 107 108 al_wait_cond(data.cond, data.mutex); 109 110 } 111 al_unlock_mutex(data.mutex); 112 113 al_lock_mutex(data.mutex); 114 data.modi_X = true; 115 data.ready = false; 116 al_unlock_mutex(data.mutex); 117 118 thread_2 = al_create_thread(Func_Thread, &data); 119 al_start_thread(thread_2); 120 121 al_lock_mutex(data.mutex); 122 while (!data.ready){ 123 124 al_wait_cond(data.cond, data.mutex); 125 126 } 127 al_unlock_mutex(data.mutex); 128 129 130 while(1) 131 { 132 ALLEGRO_EVENT ev; 133 al_wait_for_event(event_queue, &ev); 134 135 if(ev.type == ALLEGRO_EVENT_TIMER) { 136 redraw = true; 137 } 138 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 139 break; 140 } 141 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { 142 break; 143 } 144 if(redraw && al_is_event_queue_empty(event_queue)) { 145 redraw = false; 146 147 al_lock_mutex(data.mutex); 148 float X = data.posiX; 149 float Y = data.posiY; 150 al_unlock_mutex(data.mutex); 151 152 al_draw_bitmap(bouncer, X, Y, 0); 153 154 al_flip_display(); 155 } 156 } 157 al_destroy_thread(thread_1); 158 al_destroy_thread(thread_2); 159 160 al_destroy_bitmap(bouncer); 161 al_destroy_timer(timer); 162 al_destroy_display(display); 163 al_destroy_event_queue(event_queue); 164 165 return 0; 166} 167 168 static void *Func_Thread(ALLEGRO_THREAD *thr, void *arg){ 169 170 DATA *data = (DATA*) arg; 171 float num = 0.1; 172 173 al_lock_mutex(data->mutex); 174 175 bool modi_X = data->modi_X; 176 data->ready = true; 177 al_broadcast_cond(data->cond); 178 179 al_unlock_mutex(data->mutex); 180 181 while(!al_get_thread_should_stop(thr)){ 182 183 al_lock_mutex(data->mutex); 184 if(modi_X) 185 data->posiX += num; 186 else 187 data->posiY += num; 188 al_unlock_mutex(data->mutex); 189 190 al_rest(0.01); 191 192 } 193 194 195 return NULL; 196 }

this is entire code and even after saving it as c++ file i get the same error
can anyone sort this out

Arthur Kalliokoski
Second in Command
February 2005
avatar

I don't get any errors compiling that. Which compiler are you using, what options or command line etc.?

[EDIT]

nevermind

xtech45 said:

when i type this in Xcode and the language is c

It has to be compiled as C++. I don't know what Xcode is, try g++.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

xtech45
Member #15,430
December 2013

hmm okay will try it from that , but you need to include all the library into the g++ compiler before compiling it right

Arthur Kalliokoski
Second in Command
February 2005
avatar

xtech45 said:

you need to include all the library into the g++ compiler before compiling it right

Which library? Allegro? You have to link it in somewhere to get an executable, even with C.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

xtech45
Member #15,430
December 2013

ya allegro only and how do i link it and is there any links or guidance for how to link it to get an executable

gezegond
Member #14,762
December 2012
avatar

xcode is for iOS development afaik.

https://en.wikipedia.org/wiki/Xcode

Go to: