如何从方法返回一个静态const int std :: array?

时间:2018-03-19 19:50:17

标签: c++ arrays c++11

我正在尝试从函数返回一个常量的int std :: array,但是我遇到错误而且我不知道如何修复它们。

refmatrix中的这些值不应该被更改。这就是我使用常量int的原因。

reference.h

#include <array>
#include <iostream>

using namespace std;

class Reference {
public:
    static const size_t NCOLS = 3;
    static const size_t NBITS = 4;
    static const size_t NROWS = 4;

private:
    static array<array<array<const int, NBITS>, NCOLS>, NROWS> refmatrix;

public:
    Reference();
    ~Reference();

    array<const int, NBITS> smaller(int *arry);
};

reference.cpp

#include "reference.h"

array<array<array<const int, Reference::NBITS>, Reference::NCOLS>, Reference::NROWS> Reference::refmatrix = {{{{{0, 1, 2, 6}, {6, 4, 1, 7}, {6, 7, 8, 3}}}, 
                                                                                                              {{{7, 0, 5, 2}, {1, 6, 9, 3}, {1, 4, 8, 0}}}, 
                                                                                                              {{{9, 3, 4, 6}, {0, 7, 2, 8}, {5, 3, 9, 4}}}, 
                                                                                                              {{{8, 9, 1, 4}, {7, 2, 6, 0}, {4, 0, 3, 7}}}}};

Reference::Reference() {}
Reference::~Reference() {}

array<const int, Reference::NBITS> Reference::smaller(int *arry) {
    int j;

    for(int i=0; i < NROWS; i++){
        for(j=0; j < NCOLS; j++){
            if(refmatrix[i][0][j] != arry[j]){
                j = (NCOLS + 1);
            }
        }
        if(j == NCOLS){
            return refmatrix[i][1];
        }
    }
}

的main.cpp

#include "reference.h"

int main() {
    Reference r;
    int arr[3] = {0, 1, 2};
    array<const int, Reference::NBITS> resp;

    resp = r.smaller( arr );

    // After get these values I will write it in a file.

    return 0;
}
main.cpp: In function ‘int main()’:

main.cpp:6:37: error: use of deleted function ‘std::array<const int, 4>::array()’
  array<const int, Reference::NBITS> resp;
                                     ^~~~

In file included from reference.h:1:0,
                 from main.cpp:1:

/usr/include/c++/7.3.0/array:94:12: note: ‘std::array<const int, 4>::array()’ is implicitly deleted because the default definition would be ill-formed:
     struct array
            ^~~~~

/usr/include/c++/7.3.0/array:94:12: error: uninitialized const member in ‘struct std::array<const int, 4>’

/usr/include/c++/7.3.0/array:110:56: note: ‘const int std::array<const int, 4>::_M_elems [4]’ should be initialized
       typename _AT_Type::_Type                         _M_elems;
                                                        ^~~~~~~~

main.cpp:9:33: error: use of deleted function ‘std::array<const int, 4>& std::array<const int, 4>::operator=(std::array<const int, 4>&&)’
  resp = r.smaller( arr );
                                 ^

In file included from reference.h:1:0,
                 from main.cpp:1:

/usr/include/c++/7.3.0/array:94:12: note: ‘std::array<const int, 4>& std::array<const int, 4>::operator=(std::array<const int, 4>&&)’ is implicitly deleted because the default definition would be ill-formed:
     struct array
            ^~~~~

/usr/include/c++/7.3.0/array:94:12: error: non-static const member ‘const int std::array<const int, 4>::_M_elems [4]’, can’t use default assignment operator

1 个答案:

答案 0 :(得分:1)

该错误与返回const没有任何关系。这与分配到resp

有关

您需要使用调用初始化int main() { Reference r; int arr[3] = {0, 1, 2}; array<const int, Reference::NBITS> resp = r.smaller( arr ); // After get these values I will write it in a file. return 0; } ,而不是默认初始化然后分配。

<?php



$servername = "localhost";
$username = "root";
$password = "";
$dbname = "user_db";

// Create connection
//var_name = new mysqli(host, useername, password, database); 
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
else{


}

?>
         <?php  require_once('session.php');
             if(isset($_POST['submit']))
                                    {
        //These three fields I am using:

                             $old_password=$_POST['old_password'];
                             $user_password = $_POST['user_password'];
                             $confirm_password = $_POST['confirm_password'];
                        $query = "Select * from user_tbl where user_id = '3'";
                   $q=$conn->query($query);
                    // I am not able to fetch and check old password with db

                    if($q->num_rows>0)
                        { $r1=$q->fetch_assoc();{

                        }


                    //$chg_pwd1= mysql_fetch_array($query);

                    $data_pwd=$chg_pwd1['password'];
            }
                                        if($data_pwd==$old_password)
            {
                                            if($user_password==$confirm_password)
            {
            $update="update user_tbl set password ='$user_password' where user_id ='3'";
    echo "<script>alert('Update Sucessfully'); window.location=''change_password.php'</script>";}
       else{
    echo "<script>alert('Your new and Retype Password is not match'); window.location=''change_password.php'</script>";}}
     else{
         echo "<script>alert('Your old password is wrong'); window.location='change_password.php'</script>";}}

                    ?> 

    //Html code
        <form method="post" >

                <label>Enter Old Password</label>
                    <input type="password" placeholder="Enter Password" name="old_password">
                <label>Enter Password</label>
                    <input type="password"  id="user_password" placeholder="Enter Password" name="user_password" >
                <label>Confirm Password</label>
                    <input type="password" id="Confirm_password" placeholder="Confirm Password" name="confirm_password"  >


                    <input type="submit"  name="submit" value="Update">
                </form> 

See it live