i am getting a bunch of errors after trying to compile with your corrections. I am not near familiar enough with pointers to be able to fix this. Can you please walk me through the code so I can fix the problem myself?
||=== Switches, Debug ===|
G:\CodeBlocks-Programs\Switches\main.cpp||In constructor 'device::device(toggle)':|
G:\CodeBlocks-Programs\Switches\main.cpp|15|error: cannot convert 'toggle' to 'toggle*' in initialization|
G:\CodeBlocks-Programs\Switches\main.cpp||In member function 'bool device::call()':|
G:\CodeBlocks-Programs\Switches\main.cpp|16|error: request for member 'x' in '((device*)this)->device::pt', which is of non-class type 'toggle*'|
G:\CodeBlocks-Programs\Switches\main.cpp||In function 'int main()':|
G:\CodeBlocks-Programs\Switches\main.cpp|24|error: no matching function for call to 'device::device(toggle*)'|
G:\CodeBlocks-Programs\Switches\main.cpp|15|note: candidates are: device::device(toggle)|
G:\CodeBlocks-Programs\Switches\main.cpp|13|note: device::device(const device&)|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 2 seconds) ===|
Here is my revised code(in case you need it):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
using namespace std;
class toggle//the switch
{
public:
void set(bool b){x = b;}
bool call(){return x;}
private:
bool x;
};
class device//controlled by toggle(switch)
{
public:
device(toggle t): pt(t){}// constructor
bool call(){return *pt.x;}
private:
toggle* pt;// pointer to a toggle
};
int main()
{
toggle tlight;
device light( &tlight);// associate toggle with device
tlight.set(1);
cout << light.call() << " ";// light is on
tlight.set(0);
cout << light.call() << " ";// light is off
cout << endl;
return 0;
}
|