C++/scope

Advertisement


Question
QUESTION: Hi,

How are you? Thanks for taking questions. In my *.h file I have this class

class LargeFile {
int x;  
public:
void mergeFiles();
};

Why cannot I in my mergeFile() do this:

void mergeFiles()
{
x=5;
}

The compiler says that x was not defined in this scope. But why? The mergeFiles() is a member of class LargeFile, so why cannot I use its variables. Is it because I didn't specify the instance?

I'm actually trying to achieve this: define variables within a class so that all members of that class could directly and simply  use those variables. I don't want the class to do anything fancy. I don't want to give everything a global scope. How to do it properly? Thanks so much! Andres

ANSWER: Hi Andres.

The mergeFiles function you have written is not part of the LargeFile class. To make it part of the class, write it like this:

void LargeFile::mergeFiles()
{
  x=5;
}

Every method that is defined outside of the class body must have the class name, followed by ::, followed by the method name.

Best regards
Zlatko


---------- FOLLOW-UP ----------

QUESTION: Hi,

Thanks very much. However, when I make the change you suggested, the program won't compile. I get this error:

main.cpp: error: ‘mergeFiles’ was not declared in this scope

In my main.cpp I call the function simply like this:
mergeFiles();

Thanks for your continued help.
Regards,
Tom

Answer
Hi Andres

To call a method on an object, you need to have created an instance of the object.

For example, in main.cpp you would need something like this:

int main()
{
  LargeFile lf;
  lf.mergeFiles();
}

It would make sense to have a constructor in your LargeFile so that variables are initialized when an instance is made. For example:

class LargeFile {
  int x;
public:
  LargeFile() { x = 0; }
  // plus other methods
};

Sometimes, you may want methods which don't operate on a particular object instance. In that case, you can declare a static method. A static method can operate only on static data. Static data does not belong to any particular object instance. For example, consider a case where you want to count how many LargeFiles have been made. You could have a static counter in the LargeFile class.

class LargeFile {
  static int count;
public:
  // constructor and destructor
  LargeFile() { ++count; }
  ~LargeFile() { --count; }

  static int getCount() { return count; }
};
int LargeFile::count = 0;

int main()
{
  // access static method
  LargeFile::getCount(); // should return 0
  LargeFile lf;
  LargeFile::getCount(); // should return 1
}
Count does not belong to any particular object instance. It belongs to the class. Notice that you do not call static methods with an object instance. Instead you specify the class name and double colon ::


Best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.