Arrays 无法访问专用阵列

Arrays 无法访问专用阵列,arrays,c++11,private-members,Arrays,C++11,Private Members,我很难访问字符串数组。它被声明为私有数组,并填入类的构造函数中。我定义了一个Get函数。问题是,当我在编译时调用这个函数时,我得到一个错误,我无法访问类中声明的私有成员。我只是重新开始为YKS编码,因此我正处于一个预指针和预向量的阶段,所以我试图避免使用它们的情况 单词 #pragma once #include <string> #include <iostream> #include <array> class Words { Words();

我很难访问字符串数组。它被声明为私有数组,并填入类的构造函数中。我定义了一个Get函数。问题是,当我在编译时调用这个函数时,我得到一个错误,我无法访问类中声明的私有成员。我只是重新开始为YKS编码,因此我正处于一个预指针和预向量的阶段,所以我试图避免使用它们的情况

单词

#pragma once
#include <string>
#include <iostream>
#include <array>

class Words {
    Words();

    public:
        std::string GetNewWord(int);

    private:
         std::string WordList[23] = {};
};
main.cpp-包含一个无限循环,因此我可以快速测试数组是否已填充

#include <iostream>
#include <string>
#include "Words.h"

Words word;

int main() {

    do {
        std::cout << "choice: ";
        int choice;
        std::cin >> choice;
        std::cout << "\n" << word.GetNewWord(choice) << "\n";

    } while (true);

    return 0;
}
#包括
#包括
#包括“Words.h”
单词;
int main(){
做{
标准::cout>选择;

std::cout构造函数是私有的,因为默认情况下类的所有成员都是私有的。只需将其移动到公共部分。

私有int或其他类型与数组之间的区别是什么。大多数文本都说,属于类的任何静态变量或数据在默认情况下或通过使用私有标记被标记为私有然后定义get/set方法来访问它们。您的建议解决了这个问题,但提出了一个新的问题,即使用类定义的数组和其他类定义的数据的正确方式是什么。没有任何区别。您的问题与使用数组的成员或getter方法无关。您的类的构造函数是private,因此不可访问。这就是编译器抱怨的。
#include <iostream>
#include <string>
#include "Words.h"

Words word;

int main() {

    do {
        std::cout << "choice: ";
        int choice;
        std::cin >> choice;
        std::cout << "\n" << word.GetNewWord(choice) << "\n";

    } while (true);

    return 0;
}