Error in Code executuion( Compile time)

P

Pallav singh

Hi

i am getting error while executing the following code

#include <iostream>
using namespace std;

class Gaint
{
public :
Gaint(){ id = next++; }
void func1(){ cout<<" Inside Function 1 "<<endl; }
void func2(){ cout<<" Inside Function 2 "<<endl; }
void func3(){ cout<<" Inside Function 3 "<<endl; }

private :
int id;
static int next;
};
int Gaint::next = 0;

class Command
{
public :
typedef void (Gaint:: *Action)();

Command( Gaint * _object , Action _method )
{ object = _object;
method = _method;
}

void execute()
{ (object->*method)(); }

private :
Gaint * object;
Action * method;
};


template<typename T>
class queue
{
public :
enum { SIZE = 8 };
T * array[SIZE];
int add , remove;

queue(){ add = remove = 0;}

void enqueue(T * c)
{
array[add] = c;
add = (add + 1) % SIZE;
}

T * dequeue()
{
int tmp = remove;
remove =(remove + 1) % SIZE;
return array[tmp];
}
};

int main()
{

queue<Command > que;

Command * input[] = {
new Command(new Gaint(), &Gaint::func1),
new Command(new Gaint(), &Gaint::func2),
new Command(new Gaint(), &Gaint::func3),
new Command(new Gaint(), &Gaint::func1),
new Command(new Gaint(), &Gaint::func2),
new Command(new Gaint(), &Gaint::func3)
};

for( int i =0 ; i< 6 ; i++)
que.enqueue(input);

for( int i =0 ; i< 6 ; i++)
que.dequeue()->execute();

return 0;
}
 
R

red floyd

Pallav said:
You have serious problems that should prevent compilation.
How are you getting execution errors?
i am getting error while executing the following code

#include <iostream>
using namespace std;

class Gaint
{
public :
Gaint(){ id = next++; }
void func1(){ cout<<" Inside Function 1 "<<endl; }
void func2(){ cout<<" Inside Function 2 "<<endl; }
void func3(){ cout<<" Inside Function 3 "<<endl; }

private :
int id;
static int next;
};
int Gaint::next = 0;

class Command
{
public :
typedef void (Gaint:: *Action)();

Command( Gaint * _object , Action _method )
{ object = _object;
method = _method;
}
Prefer an initialization list.
Command(Gaint* _object, Action _method)
: object(_object) method(_method)
{
}
void execute()
{ (object->*method)(); }

private :
Gaint * object;
Action * method;
This is wrong, it shouldn't even compile.
Action method;
};


template<typename T>
class queue
{
public :
enum { SIZE = 8 };
T * array[SIZE];
int add , remove;

queue(){ add = remove = 0;}

void enqueue(T * c)
{
array[add] = c;
add = (add + 1) % SIZE;
}

T * dequeue()
{
int tmp = remove;
remove =(remove + 1) % SIZE;
return array[tmp];
}
};

int main()
{

queue<Command > que;

Command * input[] = {
new Command(new Gaint(), &Gaint::func1),
new Command(new Gaint(), &Gaint::func2),
new Command(new Gaint(), &Gaint::func3),
new Command(new Gaint(), &Gaint::func1),
new Command(new Gaint(), &Gaint::func2),
new Command(new Gaint(), &Gaint::func3)
Note that you have a memory leak -- who deletes the Gaints that you
new'ed?
};
Command input[] = {
Command(new Gaint(), &Gaint::func1),
etc...
};This shouldn't compile. You have a ueue of Commands, not of Command*.
Learn the difference between objects and pointers.
for( int i =0 ; i< 6 ; i++)
que.enqueue(input);

for( int i =0 ; i< 6 ; i++)
que.dequeue()->execute();

return 0;
}
 
T

tristan

class Gaint
{
private:
int m_id;
static int m_next;

public:
Gaint()
{
m_id = m_next++;
}
void func1()
{
cout << "Inside Function 1" << endl;
}
void Func2()
{
cout << "Inside Function 2" << endl;
}
void Func3()
{
cout << "Inside Function 3" << endl;
}
};
int Gaint::m_next = 0;

class Command
{
public:
typedef void (Gaint::*Action)();

private:
Gaint *m_obj;
Action m_method;

public:
Command(Gaint *obj, Action method)
: m_obj(obj), m_method(method)
{
}
void Execute()
{
(m_obj->*m_method)();
}
};

template<typename T>
class Queue
{
public:
enum {SIZE = 8};
T* array[SIZE];
int add, remove;

Queue()
{
add = remove = 0;
}

void Enqueue(T *c)
{
array[add] = c;
add = (add + 1) % SIZE;
}

T* Dequeue()
{
int temp = remove;
remove = (remove + 1) % SIZE;
return array[temp];
}
};


red floyd дµÀ:
Pallav said:
You have serious problems that should prevent compilation.
How are you getting execution errors?
i am getting error while executing the following code

#include <iostream>
using namespace std;

class Gaint
{
public :
Gaint(){ id = next++; }
void func1(){ cout<<" Inside Function 1 "<<endl; }
void func2(){ cout<<" Inside Function 2 "<<endl; }
void func3(){ cout<<" Inside Function 3 "<<endl; }

private :
int id;
static int next;
};
int Gaint::next = 0;

class Command
{
public :
typedef void (Gaint:: *Action)();

Command( Gaint * _object , Action _method )
{ object = _object;
method = _method;
}
Prefer an initialization list.
Command(Gaint* _object, Action _method)
: object(_object) method(_method)
{
}
void execute()
{ (object->*method)(); }

private :
Gaint * object;
Action * method;
This is wrong, it shouldn't even compile.
Action method;
};


template<typename T>
class queue
{
public :
enum { SIZE = 8 };
T * array[SIZE];
int add , remove;

queue(){ add = remove = 0;}

void enqueue(T * c)
{
array[add] = c;
add = (add + 1) % SIZE;
}

T * dequeue()
{
int tmp = remove;
remove =(remove + 1) % SIZE;
return array[tmp];
}
};

int main()
{

queue<Command > que;

Command * input[] = {
new Command(new Gaint(), &Gaint::func1),
new Command(new Gaint(), &Gaint::func2),
new Command(new Gaint(), &Gaint::func3),
new Command(new Gaint(), &Gaint::func1),
new Command(new Gaint(), &Gaint::func2),
new Command(new Gaint(), &Gaint::func3)
Note that you have a memory leak -- who deletes the Gaints that you
new'ed?
};
Command input[] = {
Command(new Gaint(), &Gaint::func1),
etc...
};This shouldn't compile. You have a ueue of Commands, not of Command*.
Learn the difference between objects and pointers.
for( int i =0 ; i< 6 ; i++)
que.enqueue(input);

for( int i =0 ; i< 6 ; i++)
que.dequeue()->execute();

return 0;
}
 
R

red floyd

tristan said:
class Gaint
{
private:
int m_id;
static int m_next;

public:
Gaint()
{
m_id = m_next++;
}
void func1()
{
cout << "Inside Function 1" << endl;
}
void Func2()
{
cout << "Inside Function 2" << endl;
}
void Func3()
{
cout << "Inside Function 3" << endl;
}
};
int Gaint::m_next = 0;

class Command
{
public:
typedef void (Gaint::*Action)();

private:
Gaint *m_obj;
Action m_method;

public:
Command(Gaint *obj, Action method)
: m_obj(obj), m_method(method)
{
}
void Execute()
{
(m_obj->*m_method)();
}
};

template<typename T>
class Queue
{
public:
enum {SIZE = 8};
T* array[SIZE];
int add, remove;

Queue()
{
add = remove = 0;
}

void Enqueue(T *c)
{
array[add] = c;
add = (add + 1) % SIZE;
}

T* Dequeue()
{
int temp = remove;
remove = (remove + 1) % SIZE;
return array[temp];
}
};

You did not address the issues with your main program, as far
as I can tell.

Now go and read FAQ 5.8
(http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8)

What is the error you are seeing? What output are you getting?
What were you expecting?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top