简体   繁体   中英

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 . You are passing to it a pointer to an array of 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. Better use a std::vector<std::string> , or std::array<std::string, 50> .

Side note: don't call your member function try() , as try is a reserved C++ keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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