C++ - 类中的私有变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5613143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:31:46  来源:igfitidea点击:

C++ - Private variables in classes

c++classprivate

提问by Mr. Giggums

I am trying to create a class in seperate files with private variables. So far my classes code is:

我正在尝试在带有私有变量的单独文件中创建一个类。到目前为止,我的课程代码是:

In TestClass.h

在 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

在 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:

当我尝试在 Code::Blocks 中构建时,它说:

  • ...\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
  • ...\TestClass.cpp:在函数“void set(std::string)”中:
  • ...\TestClass.cpp:12: 错误:'hi' 未在此范围内声明
  • ...\TestClass.cpp: 在函数 'void print(int)' 中:
  • ...\TestClass.cpp:17: 错误:'hi' 未在此范围内声明
  • ...\TestClass.cpp:19: 错误:'hi' 未在此范围内声明
  • ...\TestClass.cpp:21: 错误:'hi' 未在此范围内声明
  • ...\TestClass.cpp:23: 错误:'hi' 未在此范围内声明

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

但是当我运行它(而不是构建它)时,一切正常。

回答by Nawaz

You forgot to write TestClass::as shown below:

你忘记写了TestClass::,如下图:

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

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

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

这是必要的,以便编译器可以知道set并且print是 class 的成员函数TestClass。一旦你编写了它,使它们成为成员函数,它们就可以访问类的私有成员。

Also, without TestClass::, setand printfunction would become free functions.

此外,没有识别TestClass ::setprint功能将成为免费的功能。

回答by EboMike

Use

void TestClass::set(string x){

and

void TestClass::print(int x){

回答by bgporter

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

在您的.cpp文件中,您需要使 set 和 print 成员函数明确成为类的一部分,例如:

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

回答by wkl

You don't scope resolve your printand setfunctions with the class name.

您不使用类名解析您的printset函数。

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

回答by Adam

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.

打印()相同。您将它们声明为全局函数而不是 TestClass 的成员函数。

回答by Achim

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

您的方法未定义为类的方法。尝试使用 TestClass::set 和 TestClass::print。

回答by CyberDem0n

-void set(string x) {
+void TestClass:set(string x) {