Aaargh C++ and pointers... Used to code for Nokia with C++ for 6 years. Lucky for me the subcontractor company i worked in went bankcrupt and i was able to move back to fun stuff. So if you'll help me with the scavenger things, I'll help you with the C++...
Holy smokes. Dihm=heroic. I can splint/ bandage broken and bleeding humans. Dispatch Police. Fire and EMS while on the 911 getting info/relaying instructions to the caller and entering in information in our OUTFUCKINGDATED MS-DOS based dispatch software.. but this shit makes my brain hurt. You guys are seriously skilled. this mortal bows to you!
I'm 100% sure it will, just haven't had TIME Setting aside some tonight to work on it while I'm in my class. Massive work deadlines this Friday have been eating in to my life and patience.
God, this is driving me insane. So, I've got an enum function that I'm calling from another .cpp file. This is located in the Hardware.h file Code: enum State {ON, OFF}; void ControlColdWater(State state); // Turn on/off cold water to washer void ControlHotWater(State state); // Turn on/off hot water to washer void ControlDrum(State state); // Turn on/off washer’s drum void ControlDrain(State state); // On – open drain, Off – close drain void ControlDoorLock(State state); // Lock the washer door And this is in the Software.cpp file, which calls that Hardware.h header. Code: hardwareSW.ControlDrum(Hardware::State::OFF); I have no clue what I'm doing, I've tried all of the below. Code: hardwareSW.ControlDrum(OFF); hardwareSW.ControlDrum() = "OFF"; hardwareSW.ControlDrum() = OFF; strcpy(hardwareSW.ControlDrum, OFF); etc etc etc I'm getting this error though. Code: Software.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::ControlDrum(enum Hardware::State)" (?ControlDrum@Hardware@@QAEXW4State@1@@Z) referenced in function "void __cdecl PauseWasher(int)" (?PauseWasher@@YAXH@Z)
Have you defined the methods in hardware.h as public? Like: Public: Methods used in other classes. Private: Internal stuff And your call in software.cpp might be like: hardwareSW.ControlDrum(Hardware:State.OFF). Edit: or rather hardwareSW.ControlDrum(hardwareSW.OFF); And as the first value of the enum is 0 and second is 1. You could just put the integer value as parameter into the method call. More support tomorrow when i'm back in the office.
Bleh. Yeah, didn't fix it. Don't laugh at how bad my code is! I'm working to clean some shit up. Code: // // Hardware.h // #ifndef _Project1_Hardware_ #define _Project1_Hardware_ #include "stdafx.h" #include "Washer.h" class Washer; class Hardware { public: enum State {ON, OFF}; bool IsColdWaterOn(); // Is cold water “turned on” bool IsHotWaterOn(); // Is hot water “turned on” bool IsDetergentAvailable(); // Did user supply detergent? bool IsDoorClosed(); // Is the washer door closed? void ControlColdWater(State state); // Turn on/off cold water to washer void ControlHotWater(State state); // Turn on/off hot water to washer void ControlDrum(State state); // Turn on/off washer’s drum void ControlDrain(State state); // On – open drain, Off – close drain void ControlDoorLock(State state); // Lock the washer door void DispenseDetergent(); void DispenseSoftener(); void DisplayMessage(char* text); // Display an alphanumeric message to the user (to indicate status or error) void SoundBuzzer(); // Sound the buzzer void InitializeTimer(); // Initialize the timer to zero void InitializeHardware(); void SetHardware(std::string washButton, std::string tempButton, bool detergentAdded, bool doorShut, bool coldWaterAvailable, bool hotWaterAvailable, int timeStopButtonPressed); int TimeElapsed(); // How much time (in minutes) has elapsed int timeStopButtonPushed; // Buttons are "latched" when pushed // When they are pushed, they will report "ON" the next time // their status is read, after reading they will revert to "OFF" State GetStartButton(); State GetStopButton(); State GetRegularButton(); State GetDelicateButton(); State GetColdColdButton(); State GetHotColdButton(); State startButton; State stopButton; State regularButton; State delicateButton; State coldColdButton; State hotColdButton; }; #endif /* defined(_Project1_Hardware_) */ Code: // // Software.cpp // #include "stdafx.h" #include "Software.h" #include "Hardware.h" enum State {ON, OFF}; State On = ON; std::string washType; std::string tempButton; int timeElapsed; int timeWash; int timeRinse; int remainingTime; Hardware hardwareSW; Washer washerSW; void Error(char* text){ hardwareSW.DisplayMessage(text); } void ScheduleWashAndRinse(std::string washType, std::string tempButton) { if (washType == "Regular"){ timeWash = 15; timeRinse = 15;} else if (washType == "Delicate"){ timeWash = 5; timeRinse = 5;} else Error("Please select a Wash Type"); } void PauseWasher(int time){ hardwareSW.ControlDrum(hardwareSW.OFF); hardwareSW.ControlDoorLock(hardwareSW.OFF); std::cout << "Washer paused: " << remainingTime << " minutes remaining." << std::endl; } void StopRinse(int time){ hardwareSW.ControlDrain(hardwareSW.ON); hardwareSW.ControlDrum(hardwareSW.OFF); hardwareSW.ControlDrain(hardwareSW.OFF); hardwareSW.ControlDoorLock(hardwareSW.OFF); &Hardware::SoundBuzzer; } void StartRinse(int time){ timeElapsed = 0; std::cout << "Rinse Started" << std::endl; hardwareSW.ControlColdWater(hardwareSW.ON); hardwareSW.ControlDrum(hardwareSW.ON); hardwareSW.ControlColdWater(hardwareSW.OFF); while (timeElapsed <= timeWash){ if (timeElapsed == hardwareSW.timeStopButtonPushed){ PauseWasher(timeElapsed); } else if (timeElapsed == timeWash){ break; } else{ &Hardware::TimeElapsed; timeElapsed++; remainingTime = (timeWash + timeRinse) - timeElapsed; } StopRinse(timeElapsed); } } void StopWash(int time){ hardwareSW.ControlDrain(hardwareSW.ON); hardwareSW.ControlDrum(hardwareSW.OFF); StartRinse(time); } void StartWash(){ timeElapsed = 0; std::cout << "Wash Started" << std::endl; hardwareSW.ControlDoorLock(hardwareSW.ON); hardwareSW.ControlDrain(hardwareSW.OFF); if (tempButton == "Cold/Cold") hardwareSW.ControlColdWater(hardwareSW.ON); else hardwareSW.ControlColdWater(hardwareSW.ON); hardwareSW.DispenseDetergent(); hardwareSW.DispenseSoftener(); hardwareSW.ControlDrum(hardwareSW.ON); hardwareSW.ControlColdWater(hardwareSW.OFF); while (timeElapsed <= timeWash){ if (timeElapsed == hardwareSW.timeStopButtonPushed){ PauseWasher(timeElapsed); } else if (timeElapsed == timeWash){ break; } else{ &Hardware::TimeElapsed; timeElapsed++; remainingTime = (timeWash + timeRinse) - timeElapsed; } StopWash(timeElapsed); } } Errors Code: 1>------ Build started: Project: CompSci308 Project1, Configuration: Debug Win32 ------ 1> Software.cpp 1>Hardware.obj : error LNK2019: unresolved external symbol "public: void __thiscall Software::Error(char *)" (?Error@Software@@QAEXPAD@Z) referenced in function "void __cdecl SetHardware(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,bool,bool,bool,int)" (?SetHardware@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0_N111H@Z) 1>Washer.obj : error LNK2001: unresolved external symbol "public: void __thiscall Software::Error(char *)" (?Error@Software@@QAEXPAD@Z) 1>Operator.obj : error LNK2019: unresolved external symbol "public: void __thiscall Washer::RunWasher(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,bool,bool,bool,int)" (?RunWasher@Washer@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0_N111H@Z) referenced in function "int __cdecl mainCRTStartup(void)" (?mainCRTStartup@@YAHXZ) 1>Software.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::ControlColdWater(enum Hardware::State)" (?ControlColdWater@Hardware@@QAEXW4State@1@@Z) referenced in function "void __cdecl StartRinse(int)" (?StartRinse@@YAXH@Z) 1>Software.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::ControlDrum(enum Hardware::State)" (?ControlDrum@Hardware@@QAEXW4State@1@@Z) referenced in function "void __cdecl PauseWasher(int)" (?PauseWasher@@YAXH@Z) 1>Software.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::ControlDrain(enum Hardware::State)" (?ControlDrain@Hardware@@QAEXW4State@1@@Z) referenced in function "void __cdecl StartWash(void)" (?StartWash@@YAXXZ) 1>Software.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::ControlDoorLock(enum Hardware::State)" (?ControlDoorLock@Hardware@@QAEXW4State@1@@Z) referenced in function "void __cdecl PauseWasher(int)" (?PauseWasher@@YAXH@Z) 1>Software.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::DispenseDetergent(void)" (?DispenseDetergent@Hardware@@QAEXXZ) referenced in function "void __cdecl StartWash(void)" (?StartWash@@YAXXZ) 1>Software.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::DispenseSoftener(void)" (?DispenseSoftener@Hardware@@QAEXXZ) referenced in function "void __cdecl StartWash(void)" (?StartWash@@YAXXZ) 1>Software.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::DisplayMessage(char *)" (?DisplayMessage@Hardware@@QAEXPAD@Z) referenced in function "void __cdecl Error(char *)" (?Error@@YAXPAD@Z) 1>Washer.obj : error LNK2001: unresolved external symbol "public: void __thiscall Hardware::DisplayMessage(char *)" (?DisplayMessage@Hardware@@QAEXPAD@Z) 1>Washer.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Hardware::IsColdWaterOn(void)" (?IsColdWaterOn@Hardware@@QAE_NXZ) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>Washer.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Hardware::IsHotWaterOn(void)" (?IsHotWaterOn@Hardware@@QAE_NXZ) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>Washer.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Hardware::IsDetergentAvailable(void)" (?IsDetergentAvailable@Hardware@@QAE_NXZ) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>Washer.obj : error LNK2019: unresolved external symbol "public: bool __thiscall Hardware::IsDoorClosed(void)" (?IsDoorClosed@Hardware@@QAE_NXZ) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>Washer.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::InitializeTimer(void)" (?InitializeTimer@Hardware@@QAEXXZ) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>Washer.obj : error LNK2019: unresolved external symbol "public: void __thiscall Hardware::SetHardware(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,bool,bool,bool,int)" (?SetHardware@Hardware@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0_N111H@Z) referenced in function "void __cdecl RunWasher(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,bool,bool,bool,int)" (?RunWasher@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0_N111H@Z) 1>Washer.obj : error LNK2019: unresolved external symbol "public: void __thiscall Software::ScheduleWashAndRinse(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?ScheduleWashAndRinse@Software@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>Washer.obj : error LNK2019: unresolved external symbol "public: void __thiscall Software::StartWash(void)" (?StartWash@Software@@QAEXXZ) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>Washer.obj : error LNK2019: unresolved external symbol "public: void __thiscall Software::StopWash(void)" (?StopWash@Software@@QAEXXZ) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>Washer.obj : error LNK2019: unresolved external symbol "public: void __thiscall Software::StartRinse(void)" (?StartRinse@Software@@QAEXXZ) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>Washer.obj : error LNK2019: unresolved external symbol "public: void __thiscall Software::StopRinse(void)" (?StopRinse@Software@@QAEXXZ) referenced in function "void __cdecl Start(void)" (?Start@@YAXXZ) 1>F:\Users\Stephen\Documents\visual studio 2012\Projects\CompSci308 Project1\Debug\CompSci308 Project1.exe : fatal error LNK1120: 20 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I guess I should include one of the functions that's giving me fits. This is in the hardware.cpp Code: void ControlDoorLock(State state) { if (state == ON) { std::cout << "Door lock turned on" << std::endl; } else { std::cout << "Door lock turned off" << std::endl; } } edit: le sigh. Changing ControlDoorLock from an enum to an int made no difference, same error code.
ARG. Okay, so... void DisplayMessage(char* text); That isn't actually initiating the fucking constructor. I had to get rid of the ; and make it look like so: void DisplayMessage(char* text) {} Now I'm getting "error C4716: 'Hardware::IsDoorClosed' : must return a value" for some of them.
Code: // // Hardware.h // #ifndef _Project1_Hardware_ #define _Project1_Hardware_ #include "stdafx.h" class Hardware { public: enum State {ON, OFF}; bool IsColdWaterOn() {} // Is cold water “turned on” bool IsHotWaterOn() {} // Is hot water “turned on” bool IsDetergentAvailable() {} // Did user supply detergent? bool IsDoorClosed() {} // Is the washer door closed? void ControlColdWater(State state) {} // Turn on/off cold water to washer void ControlHotWater(State state) {} // Turn on/off hot water to washer void ControlDrum(State state) {} // Turn on/off washer’s drum void ControlDrain(State state) {} // On – open drain, Off – close drain void ControlDoorLock(State state) {} // Lock the washer door void DispenseDetergent() {} void DispenseSoftener() {} void DisplayMessage(char* text) {} // Display an alphanumeric message to the user (to indicate status or error) void SoundBuzzer() {} // Sound the buzzer void InitializeTimer() {} // Initialize the timer to zero void InitializeHardware() {} void SetHardware(std::string washButton, std::string tempButton, bool detergentAdded, bool doorShut, bool coldWaterAvailable, bool hotWaterAvailable, int timeStopButtonPressed) {} int TimeElapsed() {} // How much time (in minutes) has elapsed int timeStopButtonPushed; // Buttons are "latched" when pushed // When they are pushed, they will report "ON" the next time // their status is read, after reading they will revert to "OFF" /*State GetStartButton(); State GetStopButton(); State GetRegularButton(); State GetDelicateButton(); State GetColdColdButton(); State GetHotColdButton(); State startButton; State stopButton; State regularButton; State delicateButton; State coldColdButton; State hotColdButton; State doorLock; State washerDrum; State washerDrain;*/ }; #endif /* defined(_Project1_Hardware_) */ Code: // // Hardware.cpp // #include "stdafx.h" #include "Hardware.h" #include "Software.h" // State of washer hardware // By changing these, can test different washer operating conditions enum State {ON, OFF}; State startButton; State stopButton; State regularButton; State delicateButton; State coldColdButton; State hotColdButton; State doorLock; State washerDrum; State washerDrain; bool coldWaterOn; bool hotWaterOn; bool detergentAvailable; bool doorClosed; int time; int timeStopButtonPushed; Software softwareHW; // Initializes hardware to a known state void InitializeHardware() { startButton = OFF; stopButton = OFF; regularButton = OFF; delicateButton = OFF; coldColdButton = OFF; hotColdButton = OFF; doorLock = OFF; washerDrum = OFF; washerDrain = OFF; coldWaterOn = false; hotWaterOn = false; detergentAvailable = false; doorClosed = false; time = 0; timeStopButtonPushed = -1; } void DisplayMessage(char* text) { std::cout << "Washer displays: " << text << std::endl; } void SetHardware(std::string washButton, std::string tempButton, bool detergentAdded, bool doorShut, bool coldWaterAvailable, bool hotWaterAvailable, int timeStopButtonPressed) { InitializeHardware(); if (washButton == "Regular") regularButton = ON; else delicateButton = ON; if (tempButton == "Cold/Cold") coldColdButton = ON; else hotColdButton = ON; if (detergentAdded == true) detergentAvailable = true; else detergentAvailable = false; if (doorShut == true) doorClosed = true; else doorClosed = false; if (coldWaterAvailable == true) coldWaterOn = true; else coldWaterOn = false; if (hotWaterAvailable == true) hotWaterOn = true; else hotWaterOn = false; timeStopButtonPushed = timeStopButtonPressed; } // Following is simulation of washer hardware bool IsColdWaterOn() { return coldWaterOn; } bool IsHotWaterOn() { return hotWaterOn; } bool IsDetergentAvailable() { return detergentAvailable; } bool IsDoorClosed() { return doorClosed; } void ControlColdWater(State state) { if (state == ON) { std::cout << "Cold water turned on" << std::endl; coldWaterOn = true; } else { std::cout << "Cold water turned off" << std::endl; coldWaterOn = false; } } void ControlHotWater(State state) { if (state == ON) { std::cout << "Hot water turned on" << std::endl; hotWaterOn = true; } else { std::cout << "Hot water turned off" << std::endl; hotWaterOn = false; } } void ControlDrum(State state) { if (state == ON) { std::cout << "Drum turned on" << std::endl; washerDrum = ON; } else { std::cout << "Drum turned off" << std::endl; washerDrum = OFF; } } void ControlDrain(State state) { if (state == ON) { std::cout << "Drain turned on" << std::endl; washerDrain = ON; } else { std::cout << "Drain turned off" << std::endl; washerDrain = OFF; } } void ControlDoorLock(State state) { if (state == ON) { std::cout << "Door lock turned on" << std::endl; doorLock = ON; } else { std::cout << "Door lock turned off" << std::endl; doorLock = OFF; } } void DispenseDetergent() { std::cout << "Detergent added" << std::endl; detergentAvailable = false; } void DispenseSoftener() { std::cout << "Softener added" << std::endl; } void SoundBuzzer() { std::cout << "Bzzzz" << std::endl; } void InitializeTimer() { time = 0; } int TimeElasped() { return ++time; } /* State GetStartButton() { State value = startButton; startButton = OFF; return value; } State GetStopButton() { State value = stopButton; stopButton = OFF; if (time == timeStopButtonPushed) value = ON; return value; } State GetRegularButton() { State value = regularButton; regularButton = OFF; return value; } State GetDelicateButton() { State value = delicateButton; delicateButton = OFF; return value; } State GetColdColdButton() { State value = coldColdButton; coldColdButton = OFF; return value; } State GetHotColdButton() { State value = hotColdButton; hotColdButton = OFF; return value; } */ Any clue? Looks to me like it IS returning a fucking bool value.
OMFG I GOT TO THE CMD PROMPT :disturbed: Edit: Oh wait, I guess that's "normal"... my code just isn't executing properly. WOOHOO... Fucking thing only executes my main, doesn't execute any of the functions I'm calling from the other .cpp files.
I think you might have circular reference between Washer and Hardware classes. I got 130k instances created of those when I ran your application Though it was not that straight forward to get the app running in Mac when it's created with windows. C++ for some reason likes to add platform specific stuff there. In the constructor of Washer you create new instance of Hardware and in the constructor of Hardware you create new instance of Washer. So the application keeps looping there until the memory runs out. Not 100% sure if this is the cause, but might cause serious problems. But the application ran after I removed all the instantiations from the constructors. So maybe you could pass the instance of the class into the new ones after you've created them. For example: Washer.cpp Washer::Washer(){ this->hardware = new Hardware; this->harware->washer = this; } hardware.cpp leave the construtor empty or at least don't instantiate the washer there.