-
January 23rd, 2013, 08:01 AM
#16
Re: Conflicting declaration of objects. Need Solution Please
Well, I have to tell you again (and it is the last time!):
Please, post properly formatted code with indentations and within Code tags!
And not just a couple of lines but your actual code (or code snippet) that we will be able to compile and test.
Note that the code you have posted in the post#12 has nothing to do with the one in your OP!
Victor Nijegorodov
-
January 23rd, 2013, 10:03 AM
#17
Re: Conflicting declaration of objects. Need Solution Please
Code after edit
Code:
#include <iostream>
#include <typeinfo>
using namespace std;
/*the Command interface*/
class Command
{
public:
template <typename t>
virtual void execute(t obj)=0;
template <typename T>
friend bool operator ==( T left, T right );
};
template <typename T>
bool operator==( T left, T right ){
return (left == right);
}
/*Receiver class*/
class Light {
public:
Light() { }
void turnOn() {
cout << "The light is on" << endl;
}
void turnOff() {
cout << "The light is off" << endl;
}
};
class Fan {
public:
Fan() { }
void turnOn() {
cout << "The Fan is on" << endl;
}
void turnOff() {
cout << "The Fan is off" << endl;
}
};
/*the Command for turning on the light*/
class FlipUpCommand: public Command {
public:
template <typename t>
FlipUpCommand(t myobject) {
if (myobject == theLight){
theLight=myobject;
execute(theLight);
}
if (myobject = theFan){
theFan = myobject;
execute(theLight);
}
}
template <typename t>
virtual void execute(t obj){
obj.turnOn();
}
public:
Light theLight;
Fan theFan;
};
class FlipDownCommand: public Command {
public:
template <typename t>
FlipUpCommand(t myobject){
if (myobject == theLight){
theLight=myobject;
execute(theLight);
}
if (myobject = theFan){
theFan = myobject;
execute(theLight);
}
}
template <typename t>
virtual void execute(t obj) {
obj.turnOff();
}
public:
Light theLight;
Fan theFan;
};
class Switch {
public:
Switch(Command& flipUpCmd, Command& flipDownCmd):flipUpCommand(flipUpCmd),flipDownCommand(flipDownCmd)
{
}
void flipUp()
{
flipUpCommand.execute();
}
void flipDown()
{
flipDownCommand.execute();
}
public:
Command& flipUpCommand;
Command& flipDownCommand;
};
/*The test class or client*/
int main()
{
void *myobj = NULL;
Light *myLight;
Fan *myFan;
int choice;
cout<<"Select Light (1): ";
cout<<"Select Fan (2): ";
cin>>choice;
if (choice ==1) {
Light *myLight = new Light();
myobj = (void*)myLight;
}
if (choice ==2){
Fan *myFan = new Fan();
myobj = (void*)myFan;
}
FlipUpCommand switchUp(*myobj);
FlipUpCommand switchUp(*myobj);
FlipDownCommand switchDown(*myobj);
Switch s(switchUp, switchDown);
cout<<" ON (1), OFF(2) :";
cin>>choice;
if(choice ==1){
s.flipUp();
}
if(choice==2){
s.flipDown();
}
getch();
}
-
January 23rd, 2013, 10:26 AM
#18
Re: Conflicting declaration of objects. Need Solution Please
Well, I put your code into file temp.cpp and tried to compile it in VS2010:
Code:
temp.cpp
d:\2010 projects\temp\temp\temp.cpp(11): error C2898: 'void Command::execute(t)' : member function templates cannot be virtual
d:\2010 projects\temp\temp\temp.cpp(74): error C2898: 'void FlipUpCommand::execute(t)' : member function templates cannot be virtual
d:\2010 projects\temp\temp\temp.cpp(88): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\2010 projects\temp\temp\temp.cpp(101): error C2898: 'void FlipDownCommand::execute(t)' : member function templates cannot be virtual
d:\2010 projects\temp\temp\temp.cpp(121): error C2780: 'void Command::execute(t)' : expects 1 arguments - 0 provided
d:\2010 projects\temp\temp\temp.cpp(11) : see declaration of 'Command::execute'
d:\2010 projects\temp\temp\temp.cpp(126): error C2780: 'void Command::execute(t)' : expects 1 arguments - 0 provided
d:\2010 projects\temp\temp\temp.cpp(11) : see declaration of 'Command::execute'
d:\2010 projects\temp\temp\temp.cpp(160): error C2100: illegal indirection
d:\2010 projects\temp\temp\temp.cpp(161): error C2100: illegal indirection
d:\2010 projects\temp\temp\temp.cpp(161): error C2374: 'switchUp' : redefinition; multiple initialization
d:\2010 projects\temp\temp\temp.cpp(160) : see declaration of 'switchUp'
d:\2010 projects\temp\temp\temp.cpp(162): error C2100: illegal indirection
d:\2010 projects\temp\temp\temp.cpp(162): error C2664: 'FlipDownCommand::FlipDownCommand(const FlipDownCommand &)' : cannot convert parameter 1 from 'void *' to 'const FlipDownCommand &'
Reason: cannot convert from 'void *' to 'const FlipDownCommand'
No constructor could take the source type, or constructor overload resolution was ambiguous
d:\2010 projects\temp\temp\temp.cpp(175): error C3861: 'getch': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So, please, fix these errors, then we could talk about your further design.
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|