简体   繁体   English

如何在C ++中使用常量数组的成员函数?

[英]How do I use member functions of constant arrays in C++?

Here is a simplified version of what I have (not working): 这是我所拥有的简化版本(不起作用):

prog.h: 编:

...
const string c_strExample1 = "ex1";
const string c_strExample2 = "ex2";
const string c_astrExamples[] = {c_strExample1, c_strExample2};
...

prog.cpp: prog.cpp:

...
int main()
{
    int nLength = c_astrExamples.length();
    for (int i = 0; i < nLength; i++)
        cout << c_astrExamples[i] << "\n";
    return 0;
}
...

When I try to build, I get the following error: error C2228: left of '.length' must have class/struct/union 当我尝试构建时,出现以下错误:错误C2228:'.length'的左侧必须具有class / struct / union

The error occurs only when I try to use member functions of the c_astrExamples. 仅当我尝试使用c_astrExamples的成员函数时,才会发生该错误。 If I replace "c_astrExamples.length()" with the number 2, everything appears to work correctly. 如果我将“ c_astrExamples.length()”替换为数字2,则一切似乎都能正常工作。

I am able to use the member functions of c_strExample1 and c_strExample2, so I think the behavior arises out of some difference between my use of strings vs arrays of strings. 我能够使用c_strExample1和c_strExample2的成员函数,所以我认为行为是由于我使用的字符串与字符串数组之间的某些差异引起的。

Is my initialization in prog.h wrong? 我在prog.h中的初始化错误吗? Do I need something special in prog.cpp? 我是否需要prog.cpp中的特殊内容?

Arrays in C++ don't have member functions. C ++中的数组没有成员函数。 You should use a collection like vector<string> if you want an object, or compute the length like this: 如果需要对象,则应使用诸如vector<string>类的集合,或按如下方式计算长度:

int nLength = sizeof(c_astrExamples)/sizeof(c_astrExamples[0]);

Arrays in C++ are inherited from C, which wasn't object-oriented. C ++中的数组是从C继承的,C不是面向对象的。 So they aren't objects and don't have member functions. 因此它们不是对象,也没有成员函数。 (In that they behave like int , float and the other built-in types.) From that ancestry stem more problems with array, like the fact that they easily (eg, when passed into a function) decay into a pointer to the first element with no size information left. (因为它们的行为类似于intfloat和其他内置类型。)从这个祖先中,数组产生了更多的问题,例如它们很容易(例如,传递给函数时)衰减为指向第一个元素的指针的事实没有剩余尺寸信息。

The usual advice is to use std::vector instead, which is a dynamically resizable array. 通常的建议是使用std::vector代替,它是一个可动态调整大小的数组。 However, if you the array size is known at compile-time and you need a constant, then boost's array type ( boost::array , if your compiler supports the TR1 standard extensions also available as std::tr1::array , to become std::array in the next version of the C++ standard) is what you want. 但是,如果您在编译时知道数组大小并且需要一个常量,则使用boost的数组类型boost::array ,如果您的编译器支持TR1标准扩展名,也可以作为std::tr1::array来使用)您想要的是C ++标准下一版本中的std::array

Edit 1 : 编辑1

A safe way to get the length of an array in C++ involves an incredible combination of templates, function pointers and even a macro thrown into the mix: 在C ++中获取数组长度的一种安全方法涉及到模板,函数指针甚至宏的混合:

template <typename T, std::size_t N>
char (&array_size_helper(T (&)[N]))[N];

#define ARRAY_SIZE(Array_) (sizeof( array_size_helper(Array_) ))

If you (like me) think this is hilarious, look at boost::array . 如果您(像我一样)认为这很有趣,请查看boost::array

Edit 2 : 编辑2

As dribeas said in a comment, if you don't need a compile-time constant, this 就像dribeas在评论中说的那样,如果不需要编译时常量,这

template <typename T, std::size_t N>
inline std::size_t array_size(T(&)[N])
{return N;}

is sufficient (and much easier to read and understand). 足够(并且更容易阅读和理解)。

Just use STL vector of strings instead of array: 只需使用字符串的STL向量而不是数组即可:

#include <string>
#include <vector>
using namespace std;

const string c_strExample1 = "ex1";
const string c_strExample2 = "ex2";
vector<string> c_astrExamples;
c_astrExamples.push_back(c_strExample1);
c_astrExamples.push_back(c_strExample2);

int main()
{
    int nLength = c_astrExamples.size();

c_astrExamples是一个数组,其中没有“ length()”方法。

In C++ arrays are not objects and have no methods on it. 在C ++中,数组不是对象,并且没有方法。 If you need to get the length of the array you could use the following macro 如果您需要获取数组的长度,则可以使用以下宏

#define COUNTOF( array ) ( sizeof( array )/sizeof( array[0] ) )
int nLength = COUNTOF(c_astrExamples);

Also, beware of initialisation in a header file. 另外,要注意头文件中的初始化。 You risk offending the linker. 您可能会冒犯链接程序的风险。
You should have: 你应该有:

prog.h: 编:

extern const string c_strExample1;
extern const string c_strExample2;
extern const string c_astrExamples[];

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

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