C++ - Private variables in classes

59,124

Solution 1

You forgot to write TestClass:: as shown below:

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

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

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

Also, without TestClass::, set and print function would become free functions.

Solution 2

Use

void TestClass::set(string x){

and

void TestClass::print(int x){

Solution 3

In your .cpp file, you need to make the set and print member functions explicitly part of the class, like:

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

Solution 4

You don't scope resolve your 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;
}

Solution 5

say

void TestClass::set(string x){

instead of

void set(string x){

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

Share:
59,124

Related videos on Youtube

Mr. Giggums
Author by

Mr. Giggums

Updated on July 09, 2022

Comments

  • Mr. Giggums
    Mr. Giggums almost 2 years

    I am trying to create a class in seperate files with private variables. So far my classes code 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 in Code::Blocks it says:

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

    But when I run it (and don't build it) everything is working.

    • Kiran Kumar
      Kiran Kumar
      Slightly unrelated, but using namespace std; in the header file is considered a bad practice as it pollutes the namespace.
  • scone
    scone about 8 years
    That's Objective-C

Related