[英]Passing private static array as an argument to public member function in c++?
Online documents seem to suggest that a private member variable of a class that is passed as an argument to a public function in the same class needs to be declared as static. 在线文档似乎建议将作为参数传递给同一类中的公共函数的类的私有成员变量声明为静态。 Still, I get a compilation error: 仍然,我得到一个编译错误:
class C{
private:
static std::string table1[50];
public:
bool try (){
helper(&table1);
return true;
}
bool helper (std::string * table){
return true;
}
But I am getting this compilation error: 但我收到此编译错误:
./c:72:31: error: cannot initialize a parameter of type 'std::string *' (aka
'basic_string<char, char_traits<char>, allocator<char> > *') with an rvalue of type
'std::string (*)[50]'
Is there something else that I missed? 还有其他我想念的吗?
Your helper
function takes as a parameter a pointer to std::string
. 您的helper
函数将指向std::string
的指针作为参数。 You are passing to it a pointer to an array of 50 std::string
. 您将向其传递指向50 std::string
数组的指针。 Instead, pass the first element of the array (in this case the array decays to a pointer), like 而是传递数组的第一个元素(在这种情况下,数组衰减为指针),例如
helper(table1);
or 要么
helper(&table1[0]);
I have serious doubts though that that's what you need. 我很怀疑,但这就是您所需要的。 Pointers to std::string
look a bit fishy here. 指向std::string
指针在这里看起来有点可疑。 Better use a std::vector<std::string>
, or std::array<std::string, 50>
. 最好使用std::vector<std::string>
或std::array<std::string, 50>
。
Side note: don't call your member function try()
, as try
is a reserved C++ keyword. 注意:不要调用成员函数try()
,因为try
是保留的C ++关键字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.