C ++ - Private variables in classes

I am trying to create a class in separate files with private variables. So far, the code for my class is:

In TestClass.h

#ifndef TESTCLASS_H #define TESTCLASS_H #include <string> using namespace std; class TestClass { private: string hi; public: TestClass(string x); void set(string x); void print(int x); }; #endif 

In TestClass.cpp

 #include "TestClass.h" #include <iostream> #include <string> using namespace std; TestClass::TestClass(string x) { cout << "constuct " << x << endl; } void set(string x){ hi = x; } void print(int x){ if(x == 2) cout << hi << " x = two\n"; else if(x < -10) cout << hi << " x < -10\n"; else if(x >= 10) cout << hi << " x >= 10\n"; else cout << hi << " x = " << x << endl; } 

When I try to build Code :: Blocks code, it says:

  • ... \ TestClass.cpp: In the function 'void set (std :: string)':
  • ... \ TestClass.cpp: 12: error: "hi" was not declared in this area
  • ... \ TestClass.cpp: In the function 'void print (int)':
  • ... \ TestClass.cpp: 17: error: "hi" was not declared in this area
  • ... \ TestClass.cpp: 19: error: "hi" was not declared in this area
  • ... \ TestClass.cpp: 21: error: "hi" was not declared in this area
  • ... \ TestClass.cpp: 23: error: "hi" was not declared in this area

But when I run it (and not build), everything works.

+8
c ++ private class
source share
7 answers

You forgot to write TestClass:: as shown below:

 void TestClass::set(string x) //^^^^^^^^^^^this void TestClass::print(int x) //^^^^^^^^^^^this 

This is necessary so that the compiler can know that set and print are member functions of the TestClass class. And once you write this by making them member functions, they can close private members of the class.

In addition, without functions TestClass ::, set and print will become free functions.

+18
source share

Using

 void TestClass::set(string x){ 

and

 void TestClass::print(int x){ 
+4
source share

In your .cpp file, you need to explicitly include the set and print member functions in the class, for example:

 void TestClass::set(string x){ hi = x; } void TestClass::print(int x){ if(x == 2) cout << hi << " x = two\n"; else if(x < -10) cout << hi << " x < -10\n"; else if(x >= 10) cout << hi << " x >= 10\n"; else cout << hi << " x = " << x << endl; } 
+3
source share

You do not allow print and set functions with the class name.

 void TestClass::set(string x){ hi = x; } void TestClass::print(int x){ if(x == 2) cout << hi << " x = two\n"; else if(x < -10) cout << hi << " x < -10\n"; else if(x >= 10) cout << hi << " x >= 10\n"; else cout << hi << " x = " << x << endl; } 
+2
source share

they say

 void TestClass::set(string x){ 

instead

 void set(string x){ 

for print(). You declared them global functions instead of TestClass member functions.

+1
source share

Your methods are not defined as class methods. Try using TestClass :: set and TestClass :: print.

0
source share
 -void set(string x) { +void TestClass:set(string x) { 
-one
source share

All Articles