I've managed to use classes in separate files before, but I can't seem to get it to work here. I was just programming some very basic classes for a simple game I wanted to make. Here is the main.cpp file:

#include <iostream>
#include "energySystem.h"
#include "healthSystem.h"
#include "intelligenceSystem.h"
using namespace std;

int main(){
	energySystem energySystem;
	healthSystem healthSystem;
	intelligenceSystem intelligenceSystem;
	energySystem.setEnergyLevel(750);
	healthSystem.setHealthLevel(100);
	intelligenceSystem.setIntelligenceLevel(0);
	cout << energySystem.getEnergyLevel() << endl;
	cout << healthSystem.getHealthLevel() << endl;
	cout << intelligenceSystem.getIntelligenceLevel() << endl;
	energySystem.decreaseEnergy(100);
	cout << energySystem.getEnergyLevel() << endl;
	return 0;
}

Here is one of the class headers. All 3 of the classes are identical at the moment apart from the name, so I'll just post one.

energySystem.h file:

#ifndef ENERGYSYSTEM_H
#define ENERGYSYSTEM_H

class energySystem{
    private:
        int energyLevel;
	public:
        energySystem();
		int getEnergyLevel();
		void setEnergyLevel(int x);
		void decreaseEnergy(int x);
};

#endif // ENERGYSYSTEM_H

And energySystem.cpp (this is probably the file causing the error) :

#include <iostream>
#include "energySystem.h"
#include "healthSystem.h"
#include "intelligenceSystem.h"
using namespace std;

int energyLevel;

energySystem::energySystem(){

}

int getEnergyLevel(){
    return energyLevel;
}

void setEnergyLevel(int x){
    energyLevel = x;
}

void decreaseEnergy(int x){
    if((energyLevel - x) >= 0){
        energyLevel -= x;
    }
}

Here are the errors I get:

C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|11|undefined reference to `energySystem::setEnergyLevel(int)'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|12|undefined reference to `healthSystem::setHealthLevel(int)'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|13|undefined reference to `intelligenceSystem::setIntelligenceLevel(int)'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|14|undefined reference to `energySystem::getEnergyLevel()'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|15|undefined reference to `healthSystem::getHealthLevel()'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|16|undefined reference to `intelligenceSystem::getIntelligenceLevel()'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|17|undefined reference to `energySystem::decreaseEnergy(int)'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|18|undefined reference to `energySystem::getEnergyLevel()'|

Thanks for any help.

From what I can see, you seem to have done your headers and implementation files correctly.

The big problem i see is actually Lines 8-10 of main.cpp. Change the names of those variables/objects so that they aren't hiding the names of your classes with local identifiers. When you hide identifiers like that, the compiler gets very confused.

Edited 5 Years Ago by Fbody: n/a

From what I can see, you seem to have done your headers and implementation files correctly.

The big problem i see is actually Lines 8-10 of main.cpp. Change the names of those variables/objects so that they aren't hiding the names of your classes with local identifiers. When you hide identifiers like that, the compiler gets very confused.

I changed the main.cpp file to this:

#include <iostream>
#include "energySystem.h"
#include "healthSystem.h"
#include "intelligenceSystem.h"
using namespace std;

int main(){
	energySystem energy;
	healthSystem health;
	intelligenceSystem intelligence;
	energy.setEnergyLevel(750);
	health.setHealthLevel(100);
	intelligence.setIntelligenceLevel(0);
	cout << energy.getEnergyLevel() << endl;
	cout << health.getHealthLevel() << endl;
	cout << intelligence.getIntelligenceLevel() << endl;
	energy.decreaseEnergy(100);
	cout << energy.getEnergyLevel() << endl;
	return 0;
}

But I still get the same errors :/

I wrote this code at school today using a crappy online compiler, with the classes in the same file. I just wanted them in different files so that it's more organised. When I compiled it at school, it all worked (with the object names the same as the class names), and it worked using Code::Blocks as well. But when I tried to put the classes in separate files, that's when I started getting these errors.

Well, the errors that are being returned are in main.cpp on Lines 11-18 so the problem is either in that file or in your overall project. Specifically, they are implying that the objects you are trying to use somehow don't exist.

It's acting like it can't find the support files (the other *.h and *.cpp files). Are your headers in the same folder as your main.cpp? Are your *.cpp files part of your project?

EDIT:
Oh wait, I see what's going on now. You forgot to resolve your system/class names' scopes when you implemented the member functions. As a result, you have declarations for the methods, but no implementations.
This:

int getEnergyLevel(){
    return energyLevel;
}

should be this:

int energySystem::getEnergyLevel(){  //notice the extra syntax "energySystem::"
    return energyLevel;
}

You've done similar for your other methods as well.

Edited 5 Years Ago by Fbody: n/a

Comments
Helpful information about classes.

Well, the errors that are being returned are in main.cpp on Lines 11-18 so the problem is either in that file or in your overall project. Specifically, they are implying that the objects you are trying to use somehow don't exist.

It's acting like it can't find the support files (the other *.h and *.cpp files). Are your headers in the same folder as your main.cpp? Are your *.cpp files part of your project?

EDIT:
Oh wait, I see what's going on now. You forgot to resolve your system/class names' scopes when you implemented the member functions. As a result, you have declarations for the methods, but no implementations.
This:

int getEnergyLevel(){
    return energyLevel;
}

should be this:

int energySystem::getEnergyLevel(){  //notice the extra syntax "energySystem::"
    return energyLevel;
}

You've done similar for your other methods as well.

Ok thanks, that worked :)

I thought I only needed to do that for constructors/destructors. I guess I need to revise classes.

Edited 5 Years Ago by ben1996123: n/a

Ok thanks, that worked :)

I thought I only needed to do that for constructors/destructors. I guess I need to revise classes.

Nope, you need to resolve the scope for any and all member functions and/or member variables (especially static ones) you are doing anything with outside of the declaration of the class.

Glad you got it working. :)

Edited 5 Years Ago by Fbody: n/a

Hmm... I got another "error" just now, I called the functions:

energySystem.setEnergyLevel(750);
    healthSystem.setHealthLevel(100);
    intelligenceSystem.setIntelligenceLevel(0);

But when I try to do this:

energySystem energySystem2;
healthSystem healthSystem2;
intelligenceSystem intelligenceSystem2;

void switchForDestinations::switchCaseOne(){
    cout << "Energy: " << energySystem2.getEnergyLevel() << endl;
    cout << "Health: " << healthSystem2.getHealthLevel() << endl;
    cout << "Intelligence: " << intelligenceSystem2.getIntelligenceLevel() << endl;
}

in a separate class, it always prints:

Energy: 0
Health: 0
Intelligence: 0

:/

#include <iostream>
#include <cmath>
#include "energySystem.h"
#include "healthSystem.h"
#include "intelligenceSystem.h"
#include "printingText.h"
#include "switchForDestinations.h"
#include "experienceSystem.h"
using namespace std;

string playerName;
int possibleDestinationChoice;

void doSwitchForDestinations();

energySystem energySystem;
healthSystem healthSystem;
intelligenceSystem intelligenceSystem;
printingText printingText;
switchForDestinations switchForDestinations;

int main(){
    energySystem.setEnergyLevel(750);
    healthSystem.setHealthLevel(100);
    intelligenceSystem.setIntelligenceLevel(0);
	cout << "Please enter your name: ";
	getline(cin, playerName);
	printingText.printPossibleDestinations();
	cout << endl;
	cin >> possibleDestinationChoice;
	cout << endl;
	doSwitchForDestinations();
	return 0;
}

void doSwitchForDestinations(){
    switch(possibleDestinationChoice){
        case 1:
            switchForDestinations.switchCaseOne();
            break;
        case 2:
            switchForDestinations.switchCaseTwo();
            break;
    }
}

I set the values straight away, then get them after, so I don't see why this matters.

Also, how would I set the values within a class?

Edited 5 Years Ago by ben1996123: n/a

If you have 2 instances of a class, the only link between the two instances is the class description. Each instance is its own entity with its own copies of the member variables and its own unique data set. Changes you make to one instance's data are not reflected in the other instance.

The type of behavior you seem to be expecting only applies to static member variables.

Edited 5 Years Ago by Fbody: n/a

If you have 2 instances of a class, the only link between the two instances is the class description. Each instance is its own entity with its own copies of the member variables and its own unique data set. Changes you make to one instance's data are not reflected in the other instance.

The type of behavior you seem to be expecting only applies to static member variables.

So how would I get it to work? If I try to use the same object, it doesn't work.

Well, I'm not real sure how your program is supposed to work, or what it's trying to accomplish, so I can't say for sure.

Based on what I can glean from your code though, I would suggest adding some reference arguments/parameters to to the appropriate member functions in your switchForDestinations class.

class SomeOtherSystem;

class SomeSystem {
 public:
   void ModifySomeOtherSystemObject (SomeOtherSystem const &OtherSystemObject);
};

class SomeOtherSystem {
  //...
};

This is a sample definition/declaration only. You'll still have to implement the function.

Also, you'll notice that I made this a constant reference. If you need to modify the original object, you will need to leave out the "const".

Edited 5 Years Ago by Fbody: n/a

Well, I'm not real sure how your program is supposed to work, or what it's trying to accomplish, so I can't say for sure.

Based on what I can glean from your code though, I would suggest adding some reference arguments/parameters to to the appropriate member functions in your switchForDestinations class.

class SomeOtherSystem;

class SomeSystem {
 public:
   void ModifySomeOtherSystemObject (SomeOtherSystem const &OtherSystemObject);
};

class SomeOtherSystem {
  //...
};

This is a sample definition/declaration only. You'll still have to implement the function.

Also, you'll notice that I made this a constant reference. If you need to modify the original object, you will need to leave out the "const".

I don't really understand how that code works...

What I wanted to do was basically this code that I wrote from a tutorial, but with the class in a different file.

#include <iostream>
#include <string>

using namespace std;

class testClass{
    public: //create a public function to access the private variable. cant access the variable directly
        void setString1(string x){ //change string1
            string1 = x;
        }
        string getString1(){ //access string1
            return string1;
        }
    private: //private - cant access the variable in name. make variables private. can only be used by other stuff in the class
        string string1;
};

int main(){
    testClass test;
    test.setString1("this is string1."); // set string 1
    cout << test.getString1(); // print it out
    return 0;
}

The only part of the code in my previous post that you really need to be concerned about is Line 5.

As far as your example code it would become:

//testClass.cpp
#ifndef TEST_CLASS_H
#define TEST_CLASS_H

#include <string>

class testClass{
 public: //create a public function to access the private variable. cant access the variable directly
   void setString1(std::string x); //change string1
   std::string getString1();       //access string1

 private: //private - cant access the variable in name. make variables private. can only be used by other stuff in the class
   std::string string1;
};

#endif //TEST_CLASS_H
//testClass.cpp
#include "testClass.h"
#include <string>  //not technically required because it's already in your testClass header, but it's good practice to be explicit

void testClass::setString1(std::string x) { //change string1
  string1 = x;
}

std::string testClass::getString1() { //access string1
  return string1;
}
//main.cpp

#include <iostream>
#include <string>
#include "testClass.cpp"

using namespace std;

int main() {
  testClass test;
  test.setString1("this is string1."); // set string 1
  cout << test.getString1(); // print it out
  return 0;
}

The thing you need to remember is your #includes need to be set up correctly and your *.cpp files need to be part of your project so that they get compiled. If they don't get compiled, the linker can't find things it needs to create a finished working executable.

Edited 5 Years Ago by Fbody: n/a

So... From what I can see, if the function has parameters, the "std::" goes in the parameters before the variable type, otherwise it goes before the return type of the function.

I changed the header file to this:

#ifndef ENERGYSYSTEM_H
#define ENERGYSYSTEM_H

class energySystem{
    private:
        std::int energyLevel;
	public:
		std::int getEnergyLevel();
		void setEnergyLevel(std::int x);
		void decreaseEnergy(std::int x);
};

#endif // ENERGYSYSTEM_H

and the cpp file to this:

#include <iostream>
#include <cmath>
#include "energySystem.h"
using namespace std;

std::int energySystem::getEnergyLevel(){
    return energyLevel;
}

void energySystem::setEnergyLevel(std::int x){
    energyLevel = x;
}

void energySystem::decreaseEnergy(std::int x){
    if((energyLevel - x) >= 0){
        energyLevel -= x;
    }
}

But now I get errors on lines 6, 8, 9 and 10 in the header file... :/

Sorry for being such a nub :3

Edited 5 Years Ago by ben1996123: n/a

No.

The "std::" is a scope resolution, it is only required under certain circumstances. Unless you have used an appropriate "using" statement, it is required when you try to access a member of the std namespace, which the "string" class is. This early in your education, you don't normally see it written the way I wrote it. You would normally see it as:

#include <iostream>
#include <string>

using namespace std;

int main() {
  string myString = "Some string.";
  int anInt = 15;
  cout << myString << endl;
  cout << "The int " << anInt << endl;
  return 0;
}

But as you learn more, you will also see it written:

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;
//notice there is no resolution for the int dataType...

int main() {
  string myString = "Some string.";
  int anInt = 15;
  cout << myString << endl;
  cout << "The int " << anInt << endl;
  return 0;
}

Or even as:

#include <iostream>
#include <string>

int main() {
  std::string myString = "Some string.";
  int anInt = 15;
  std::cout << myString << std::endl;
  std::cout << "The int " << anInt << std::endl;
  return 0;
}

There is no such thing as a std::int because the int dataType is not part of the std namespace.

The important thing you need to take away from my example is that when you are implementing a class' member function outside of the class declaration, you need to resolve the scope of the class' name so that the compiler knows that the function is part of that class:

#include <iostream>

class Sample {
 public:
   void SomeFunc();
}; //end declaration of class Sample

//implement the SomeFunc member of the Sample class
void Sample::SomeFunc() {  //notice the resolution of the scope of "Sample" in the function name ("Sample::").
  std::cout << "In Sample::SomeFunc()..." << std::endl;
}

Edited 5 Years Ago by Fbody: n/a

So, I already have energySystem:: at the start of the functions, is there anywhere else that I need to put it, like before variable names or anything?

So, I already have energySystem:: at the start of the functions, is there anywhere else that I need to put it, like before variable names or anything?

Also if I didn't make it clear enough already, I am calling energySystem.setEnergyLevel(750); in main, but I need to know how to access this from within a different class.

Off topic: Sorry about not editing the above post, it won't let me for some reason.

Edited 5 Years Ago by ben1996123: n/a

"energySystem::" should be at the start of all implementations for functions that are members of the "energySystem" class; "healthSystem::" should be at the start of all implementations for functions that are members of the "healthSystem" class; etc...

There shouldn't be anywhere else, unless you are using static member variables.

It may be a good idea for you to post your current code.

Also if I didn't make it clear enough already, I am calling energySystem.setEnergyLevel(750); in main, but I need to know how to access this from within a different class.

Yep, I understand that.

Because energySystem::setEnergyLevel() isn't static (and you don't want it to be) you won't be able to access it directly from user code (i.e. another class) without an object that is an instance of the energySystem class. Based on your current design, references are your best way. Have a look at this tutorial. Then look at this post again.

Another option is friend classes, but it is preferable to avoid them if you can. They're used to give a "foreign" class' code access to the private members of another class. I really don't see a program like this one needing them. They're mostly for extremely complicated APIs and GUIs.

>>Off topic: Sorry about not editing the above post, it won't let me for some reason.
You're only allowed to edit posts for 30-minutes after posting. After that, you'll have to go through a mod. But only if it's an extremely grievous error.

Edited 5 Years Ago by Fbody: n/a

Ok, I made the int x be passed by reference to the functions, so here is the code I currently have:

main.cpp:

#include <iostream>
#include <cmath>
#include "energySystem.h"
#include "switchForDestinations.h"
using namespace std;

string playerName;
int possibleDestinationChoice;

void doSwitchForDestinations();

energySystem energySystem;
switchForDestinations switchForDestinations;

int main(){
    energySystem.setEnergyLevel(750);
	cout << "Please enter your name: ";
	getline(cin, playerName);
   	cout << "Welcome, " << playerName << "." << endl;
	doSwitchForDestinations();
	return 0;
}

void doSwitchForDestinations(){
    switch(possibleDestinationChoice){
        case 1:
            switchForDestinations.switchCaseOne(); //this is the function that needs access to the 750
            break;
        case 2:
            switchForDestinations.switchCaseTwo();
            break;
    }
}

energySystem.cpp:

#include <iostream>
#include <cmath>
#include "energySystem.h"
#include "switchForDestinations.h"
using namespace std;

int energySystem::getEnergyLevel(){
    return energyLevel;
}

void energySystem::setEnergyLevel(int &x){
    energyLevel = x;
}

void energySystem::decreaseEnergy(int &x){
    if((energyLevel - x) >= 0){
        energyLevel -= x;
    }
}

energySystem.h:

#ifndef ENERGYSYSTEM_H
#define ENERGYSYSTEM_H

class energySystem{
    private:
        int energyLevel;
	public:
		int getEnergyLevel();
		void setEnergyLevel(int& x);
		void decreaseEnergy(int& x);
};

#endif // ENERGYSYSTEM_H

switchForDestinations.cpp (This needs to access main.cpp's energySystem.getEnergyLevel):

#include <iostream>
#include <cmath>
#include "energySystem.h"
#include "healthSystem.h"
#include "intelligenceSystem.h"
#include "printingText.h"
#include "switchForDestinations.h"
#include "experienceSystem.h"
using namespace std;

energySystem energySystem2;

void switchForDestinations::switchCaseOne(){
    cout << energySystem2.getEnergyLevel();
}

void switchForDestinations::switchCaseTwo(){
    cout << "text here.";
}

switchForDestinations.h:

#ifndef SWITCHFORDESTINATIONS_H
#define SWITCHFORDESTINATIONS_H

class switchForDestinations{
    public:
        void switchCaseOne();
        void switchCaseTwo();
};

#endif // SWITCHFORDESTINATIONS_H

When I try to compile this, I get this error saying that there is no matching function.

C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|16|error: no matching function for call to 'energySystem::setEnergyLevel(int)'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\energySystem.h|9|note: candidates are: void energySystem::setEnergyLevel(int&)|

The int is not what needs to be the reference. Your energySystem is not the problem. The problem is your switchForDestinations class. You need to pass a reference to your energySystem class to the appropriate method(s).

I don't understand how to do that. Can you post some example code please?

>>Can you post some example code please?
I have... In this post (clicky).

Here is another version that may be easier to understand:

//declare class A
class A {
 private:
   int A_PrvInt;
   //...
 public:
   void SetPrvInt(int);
   //...
};  //end class A

//declare class B
class B {
 private:
   //...
 public:
   void SomeFuncInB(A const &);
   //...
};  //end class B

//implement member functions
void A::SetPrvInt(int newPvtInt) {
  A_PvtInt = newPvtInt;
} //end A:SetPvrInt() function

void B::SomeFuncInB(const A &incomingA) {
  incomingA.SetPrvInt(10);
} //end B::SomeFuncInB() function

//implement the program's main()
int main() {
  A anAobject;  //declare an A object
  B aBobject;   //declare a B object

  aBobject.SomeFuncInB(anAobject);  //call the B object's member function, provide an A object as an argument

  //...
}

Notice how the member function in B (SomeFuncInB) has a constant reference to an A object as a parameter. This allows you to provide an A object as an argument to the function call.

You need to do the same thing with your "switchForDestinations::switchCase#" functions.

Edited 5 Years Ago by Fbody: n/a

Ok, I thought I understood that, so I added that, and yet again, I'm just getting different errors. Also, if I wanted to access more than 1 other class from within a class (eg. access energySystem and something else from within switchForDestinations), would I just add 2 parameters to the function in switchForDestinations, like this?

void switchCaseOne(energySystem const&, someOtherClass const&)

Also, here is the code so far, and the errors.

main.cpp:

#include <iostream>
#include <cmath>
#include "energySystem.h"
#include "switchForDestinations.h"
using namespace std;

string playerName;
int possibleDestinationChoice;

void doSwitchForDestinations();

energySystem energySystem;
switchForDestinations switchForDestinations;

int main(){
    energySystem.setEnergyLevel(750);
	cout << "Please enter your name: ";
	getline(cin, playerName);
   	cout << "Welcome, " << playerName << "." << endl;
	doSwitchForDestinations();
	return 0;
}

void doSwitchForDestinations(){
    switch(possibleDestinationChoice){
        case 1:
            switchForDestinations.switchCaseOne(energySystem);
            break;
        case 2:
            switchForDestinations.switchCaseTwo(energySystem);
            break;
    }
}

switchForDestinations.cpp:

#include <iostream>
#include <cmath>
#include "energySystem.h"
#include "switchForDestinations.h"
using namespace std;

void switchForDestinations::switchCaseOne(energySystem const&){
    cout << energySystem.getEnergyLevel(); //error here C:\Program Files (x86)\CodeBlocks\programs\Text based game\switchForDestinations.cpp|8|error: expected primary-expression before '.' token|
}

void switchForDestinations::switchCaseTwo(energySystem const&){
    cout << "text here.";
}

switchForDestinations.h:

#ifndef SWITCHFORDESTINATIONS_H
#define SWITCHFORDESTINATIONS_H

class switchForDestinations{
    public:
        void switchCaseOne(energySystem const&);
        void switchCaseTwo(energySystem const&);
};

#endif // SWITCHFORDESTINATIONS_H

energySystem files are the same as my last post.

Edited 5 Years Ago by ben1996123: n/a

Is this a declaration or a definition/implementation?

void switchCaseOne(energySystem const&, someOtherClass const&)

Either way, it's incomplete and it has 2 arguments, but this call (which is correct, so don't mess with it):

switchForDestinations.switchCaseOne(energySystem);

only has 1. See the problem? You've unintentionally either attempted to call or create a definition of an overloaded version of the function that's not defined.

You need to take the time to make sure things match up properly, and that you are using your own code, not stealing someone else's. There is no reason you should have any reference to "someOtherClass" in your code. That is something I made up for demonstration purposes, it is in no way relevant to your code, except to demonstrate a concept you need to figure out how to apply.

Edited 5 Years Ago by Fbody: n/a

Is this a declaration or a definition/implementation?

void switchCaseOne(energySystem const&, someOtherClass const&)

Either way, it's incomplete and it has 2 arguments, but this call (which is correct, so don't mess with it):

switchForDestinations.switchCaseOne(energySystem);

only has 1. See the problem? You've unintentionally either attempted to call or create a definition of an overloaded version of the function that's not defined.

You need to take the time to make sure things match up properly, and that you are using your own code, not stealing someone else's. There is no reason you should have any reference to "someOtherClass" in your code. That is something I made up for demonstration purposes, it is in no way relevant to your code, except to demonstrate a concept you need to figure out how to apply.

Yeah I know, I was just using that as an example. I don't have the code with 2 parameters that I posted anywhere in the program.

void switchForDestinations::switchCaseOne(energySystem const&){
    cout << energySystem.getEnergyLevel(); //error here C:\Program Files (x86)\CodeBlocks\programs\Text based game\switchForDestinations.cpp|8|error: expected primary-expression before '.' token|
}

Notice something missing here?

void switchForDestinations::switchCaseOne(energySystem const&){

You've declared the dataType of the parameter (a constant reference to an energySystem), but you haven't named it.

You have the same problem with your CaseTwo function.

Edited 5 Years Ago by Fbody: n/a

Notice something missing here?

void switchForDestinations::switchCaseOne(energySystem const&){

You've declared the dataType of the parameter (a constant reference to an energySystem), but you haven't named it.

You have the same problem with your CaseTwo function.

So I needed to add something after the const&? If so, I added 'a' in the cpp and header files.

void switchForDestinations::switchCaseOne(energySystem const& a){
    cout << energySystem.getEnergyLevel();
}

But now I get this...

error: expected primary-expression before '.' token

This one is kinda frustrating because seem to get that error a lot but I'm not really sure what causes it, then I usually rage quit :3

EDIT: Oh do I need to create an object? If I do, what do I need to call it? If I do this

energySystem energySystem;

I get a ton of errors because energySystem object already exists in main, but if I call it something else, it works, but the program still prints 0 as the energyLevel...

Edited 5 Years Ago by ben1996123: n/a

This is just part of why, as I mentioned before, your naming convention quite frankly sucks. You need to either change the names of your classes or your objects. You should never use the same identifier more than once.

This function:

void switchForDestinations::switchCaseOne(energySystem const& a){
    cout << energySystem.getEnergyLevel();
}

is technically valid syntax, but you have a scoping problem. The problem is you are trying to access energySystem::getEnergyLevel() from an object of type energySystem that is called "energySystem". Within that function, there is no object by that name that is in scope in this function, but you think there is because of the name.
The proper code is:

void switchForDestinations::switchCaseOne(energySystem const& a){
    cout << a.getEnergyLevel();
}

Now do you understand why I suggessted you change the names?

Edited 5 Years Ago by Fbody: n/a

Yeah I understand about the object name now, so I changed the one in main to

energySystem energy;

and I also get an error now that says:

error: passing 'const energySystem' as 'this' argument of 'int energySystem::getEnergyLevel()' discards qualifiers

Here's the code:

#include <iostream>
#include <cmath>
#include "energySystem.h"
#include "switchForDestinations.h"
using namespace std;

void switchForDestinations::switchCaseOne(energySystem const& a){
    cout << a.getEnergyLevel();
}

void switchForDestinations::switchCaseTwo(energySystem const& a){
    cout << "text here.";
}

Should I look up a tutorial about the word 'this'?

Edit: Just looked up the 'this' keyword, doesn't appear to be related to my problem... Although it might just be me not noticing something.

Edited 5 Years Ago by ben1996123: n/a

This article has been dead for over six months. Start a new discussion instead.