class Switch
{
public:
Switch();
~Switch();
bool On_off; //Can the switch be turned back off? 0 = no 1 = yes
bool Active; //Whether the switch is on or off. 0 = off 1 = on
std::string OnSound; //Words given to the player upon pulling the switch.
std::string OffSound; //Words given the player upon turning off the switch
void Pull();
};
void Pull()
{
if(Active != 1)
{
std::cout << OnSound << std::endl;
Active = 1;
std::cin.get();
}
else if(On_off = 1)
{
std::cout << OffSound << std::endl;
Active = 0;
std::cin.get();
}
}
So.... I wish to be able to call SwitchName.Pull() and turn on/off bool Active, as well as a few other things. When compiling, I get 'bool Active undefined', 'bool On_off undefined', etc... Andwhen I change void Pull() to void Switch::Pull(), I get a multiple definition error.
How might I do this correctly? Do I init my variables outside of the class as globals or something? I'm not too good of a OO programmer, pointers and advanced classes give me problems.
Thanks, ~S of the L~
Trouble in my class
I'm making some blantantly obvious mistake somewhere, but I can't figure this out.
void Switch::Pull(){ if(Active != 1) { std::cout << OnSound << std::endl; Active = 1; std::cin.get(); } else if(On_off = 1) { std::cout << OffSound << std::endl; Active = 0; std::cin.get(); }}
You have to tell the compiler you're defining Switch's pull, since its in a class namespace. Otherwise it'll think you want a pull function in the global namespace, causing the confusion you encountered.
Change the definition of Pull() function to:
void Switch::Pull()
{
...
}
EDIT: Never mind, it looks like you already tried that. Can you post the entire error you are getting? Are you putting that in a header file or something because it compiles fine on my machine.
void Switch::Pull()
{
...
}
EDIT: Never mind, it looks like you already tried that. Can you post the entire error you are getting? Are you putting that in a header file or something because it compiles fine on my machine.
With this code:
I get these errors:
C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_CPP.o(.text+0x0)
[Warning] In function `ZN6Switch4PullEv':
48 C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_H.h
multiple definition of `Switch::Pull()'
48 C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\main_CPP.o(.text+0x0):C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_H.h
first defined here
[Linker error] undefined reference to `WinMain@16'
C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\Makefile.win
[Build Error] ["Dark] Error 1
The undefined reference to 'WinMain@16' is due to no int main(); at this point, but my class should still compile correctly.
class Switch{ public: Switch(); ~Switch(); bool On_off; //Can the switch be turned back off? 0 = no 1 = yes bool Active; //Whether the switch is on or off. 0 = off 1 = on std::string OnSound; //Words given to the player upon pulling the switch. std::string OffSound; //Words given the player upon turning off the switch void Pull();};void Switch::Pull(){ if(Active != 1) { std::cout << OnSound << std::endl; Active = 1; std::cin.get(); } else if(On_off = 1) { std::cout << OffSound << std::endl; Active = 0; std::cin.get(); }}
I get these errors:
C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_CPP.o(.text+0x0)
[Warning] In function `ZN6Switch4PullEv':
48 C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_H.h
multiple definition of `Switch::Pull()'
48 C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\main_CPP.o(.text+0x0):C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_H.h
first defined here
[Linker error] undefined reference to `WinMain@16'
C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\Makefile.win
[Build Error] ["Dark] Error 1
The undefined reference to 'WinMain@16' is due to no int main(); at this point, but my class should still compile correctly.
Quote:Original post by Servant of the Lord
Andwhen I change void Pull() to void Switch::Pull(), I get a multiple definition error.
If your error is a multiple definition one, you should put the class function defenitions in a .cpp file and put include gards on you header :
switch.h
#ifndef SWITCH_H_INCLUDED#define SWITCH_H_INCLUDEDclass Switch{ //[...]};#endif
switch.cpp
#include "switch.h"void Switch::Pull(){ //[...] }
When you want to use this class, include the .h header
You should put declarations in a header file and definitions in a cpp file. And don't forget include guards.
//classes.h#pragma onceclass Switch{ public: Switch(); ~Switch(); bool On_off; //Can the switch be turned back off? 0 = no 1 = yes bool Active; //Whether the switch is on or off. 0 = off 1 = on std::string OnSound; //Words given to the player upon pulling the switch. std::string OffSound; //Words given the player upon turning off the switch void Pull();};//classes.cpp#include "switch.h"void Switch::Pull(){ if(Active != 1) { std::cout << OnSound << std::endl; Active = 1; std::cin.get(); } else if(On_off = 1) { std::cout << OffSound << std::endl; Active = 0; std::cin.get(); }}
Okay. I had the entire thing in a header(class.h); but I forgot that functions cant go in a header. I'll fix it and see if it works.
Still doesn't work!
I still get these errors:
C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_CPP.o(.text+0x0)
[Warning] In function `ZN6Switch4PullEv':
5 C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_CPP.cpp
multiple definition of `Switch::Pull()'
4 C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\main_CPP.o(.text+0x0):C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_CPP.cpp
first defined here
C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\Makefile.win
[Build Error] ["Dark] Error 1
The 'undefined winmain@16' one is gone, because I added a quick int main(), so to stop getting that error.
I notice in [ source] tags 'Switch' is highlighted blue. Perhaps that is a built in c++ class already. Should I name mine something else?
//classes_CPP.cpp#include "classes_H.h"void Switch::Pull(){ if(Active != 1) { std::cout << OnSound << std::endl; Active = 1; std::cin.get(); } else if(On_off = 1) { std::cout << OffSound << std::endl; Active = 0; std::cin.get(); }}
//classes_H.h#ifndef CLASSES_H#define CLASSES_H#include <iostream>class Room{ public: Room(); ~Room(); void Description(); //Description of the room given to player std::string Area; //The name of the area the room is in (example:'Central cave') bool NORTH; bool EAST; bool SOUTH; //Can you go south? 0 = NO 1 = YES bool WEST; bool UP; bool DOWN;};class Item{ public: Item(); ~Item(); std::string Name; void Use(); //Function to use the item void Loc(); //Location of the item if left somewhere or not yet obtained};class Switch{ public: Switch(); ~Switch(); bool On_off; //Can the switch be turned back off? 0 = no 1 = yes bool Active; //Whether the switch is on or off. 0 = off 1 = on std::string OnSound; //Words given to the player upon pulling the switch. std::string OffSound; //Words given the player upon turning off the switch void Pull();};#endif
I still get these errors:
C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_CPP.o(.text+0x0)
[Warning] In function `ZN6Switch4PullEv':
5 C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_CPP.cpp
multiple definition of `Switch::Pull()'
4 C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\main_CPP.o(.text+0x0):C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\classes_CPP.cpp
first defined here
C:\My Documents\C++ Programs\My Programs\Text based Rpgs\Dark Cave\Makefile.win
[Build Error] ["Dark] Error 1
The 'undefined winmain@16' one is gone, because I added a quick int main(), so to stop getting that error.
I notice in [ source] tags 'Switch' is highlighted blue. Perhaps that is a built in c++ class already. Should I name mine something else?
Quote:Original post by Servant of the Lord
I notice in [source] tags 'Switch' is highlighted blue. Perhaps that is a built in c++ class already. Should I name mine something else?
No, since you started yours with a capital letter 'S', it won't conflict with the "switch" keyword. GDNet uses a crappy regex for code highlighting, is all :P
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement