Sign in to follow this  
Servant of the Lord

Trouble in my class

Recommended Posts

I'm making some blantantly obvious mistake somewhere, but I can't figure this out.
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~

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
With this code:
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.

Share this post


Link to post
Share on other sites
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_INCLUDED

class Switch
{
//[...]
};
#endif


switch.cpp

#include "switch.h"

void Switch::Pull()
{
//[...]
}


When you want to use this class, include the .h header

Share this post


Link to post
Share on other sites
You should put declarations in a header file and definitions in a cpp file. And don't forget include guards.


//classes.h
#pragma once

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();
};

//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();
}
}

Share this post


Link to post
Share on other sites
Still doesn't work!
//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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
If you were to put the constant on the left side of the equality test, then the compiler would let you know when you omit that second =.

Like so:

else if (1 == On_off) // valid

whereas:

else if (1 = On_off) // compiler will complain that you can't assign value to a constant

Share this post


Link to post
Share on other sites
!!!

IT COMPILED!

The problem was a mixture of me putting the function in a header, as deathkrush and Trillian showed me, and some error in Dev C++ itself where it still thinks I want Pull(std::string OnSound, std::string OffSound); even after I replaced that function and hit 'Rebuiled all' a few times.

So I changed Switch::Pull() to Switch::pull() and it now works!

Many thanks for everyone's help, and closing Dev C++ and reopening it should clear the bug.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this