简体   繁体   English

从main中的类访问数组

[英]Access Array from class in main

I would like to find the array size of an object within a class, inside my main() . 我想在我的main()内部找到一个类中对象的数组大小。

main()

 Class1 ojbect1;

    short int arraySize;
    arraySize = sizeof(object1.myArray)/sizeof(object1.myArray[0]);

Class1

{
public:
    static string myArray[];
.....
};

static string myArray[10];

However i am getting this error: 但是我收到此错误:

error: invalid application of 'sizeof' to incomplete type 'std::string []' 错误:“ sizeof”对不完整类型“ std :: string []”的无效应用

In this statement: 在此语句中:

static string myArray[10];

You are defining a new array, not the Class1::myArray . 您正在定义一个新数组,而不是Class1::myArray

Do it as follows: 如下进行:

string Class1::myArray[10];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM