【问题标题】:How can I write a function that returns to array inside the header?如何编写一个返回标题内数组的函数?
【发布时间】:2021-03-02 03:46:50
【问题描述】:

我正在尝试创建一个函数,它在头文件和 .cpp 文件中的 main.cpp 中返回并在主函数中运行它。

这个过程我在 main 上工作。

#include <iostream>
#include <sstream>
#include "Cards.h"

using namespace std;

//this function returns array
int *function1(){
    int a=12;
    int b=13;
    int c=14;
    static int list[3]={a,b,c};
    return list;
}

int main(int argc, const char * argv[]) {
    
    int *list;
    list=function1();
    cout<<list[1]<<endl;
    return 0;
}

但是,我不能在标头和单独的 cpp 文件中执行这些操作。

我有一个卡片标题

#ifndef Cards_H
#define Cards_H
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

class Cards{
public:
    char suit; //A,H,D,C,S. A is empty card
    int number; //00-13
    int visibilty;//0 - 1. O invisible 1 is visible
    int * function2();
};
#endif

这是类cpp文件

#include "Cards.h"

using namespace std;
//function
int Cards:: function2(){
    int a=12;
    int b=13;
    int c=14;
    int list[3]={a,b,c};
    return list; // error code Cannot initialize return object of type 'int Cards::*' with an lvalue of type 'int [3]'
}

如何解决这个问题并在 main 中运行它?

【问题讨论】:

  • c++ 中有std::array&lt;int,3&gt;,您可以从函数中返回它。您不能返回 c 数组。相关:https://stackoverflow.com/questions/3473438/return-array-in-a-function
  • 该代码应该在它看到int Cards:: array() 的那一刻就吐了——标题中的声明表明它返回int *,而不是int。解决这个问题,您仍然会返回一个自动地址,因此您还有很多 UB 需要处理。如果结果是动态的,则使用std::vector&lt;int&gt;,如果是固定的N,则使用std::array&lt;int,N&gt;
  • 但是,我不能在标头和单独的 cpp 文件中执行这些操作当您从第一个示例移到标头 + 实现示例。话虽如此,如果你总是想返回一个包含 3 个项目的数组,最好只使用正确的 c++ 容器来完成任务,在这种情况下是 std::array
  • 在你的第二个答案中,int Cards:: function2(){ 必须是 int* Cards:: function2(){int list[3]={a,b,c}; 必须是 static int list[3]={a,b,c}; 但是再次 std::array&lt;int,3&gt; 更好。您可以像返回 int 一样从函数中返回它。您根本不需要静态部分,也不需要指针。
  • 实际上,您的错误看起来好像返回类型是指向成员的指针(int Cards::* 是指向 Cardsint 成员的指针)。你确定函数签名不是int Cards::* function2(){

标签: c++ arrays function class header


【解决方案1】:

正如 cmets 中指出的,已经有一个 SO 线程

Return array in a function

处理您的问题。

如果你真的想使用C 数组,那么你的程序应该是这样的:

Cards_CStyle.h:

    #ifndef Cards_CStyle_H
    #define Cards_CStyle_H

    using namespace std;
    
    class Cards {
    public:
        int* function2(int arr[]);
    };
    #endif

Cards_CStyle.cpp:

    #include "Cards_CStyle.h"

    using namespace std;
    //function
    int* Cards::function2(int arr[]){
        int a=12, b=13, c=14;
        arr[0] = a;
        arr[1] = b;
        arr[2] = c;
        return arr;
    }

main_CStyle.cpp:

    #include <iostream>
    #include "Cards_CStyle.h"
    
    using namespace std;
    
    int main(int argc, const char * argv[]) {
        
        int arr[3]; // Take care that all your functions use size <= 3
        Cards cards;
        int* list=cards.function2(arr);
        cout<<list[1]<<endl;
        return 0;
    }

按照 cmets 的建议,您应该使用 STL 的容器,例如array 用于固定长度或vector 用于可变长度。假设 3 的固定长度对您来说没问题,那么您的代码将如下所示:

Cards_STLStyle.h:

    #ifndef Cards_STLStyle_H
    #define Cards_STLStyle_H
    #include<array>

    using namespace std;
    typedef array<int, 3> my_array;

    class Cards {
    public:
        my_array function2();
    };
    #endif

Cards_STLStyle.cpp:

    #include "Cards_STLStyle.h"

    using namespace std;
    //function
    my_array Cards::function2(){
        int a=12, b=13, c=14;
        return my_array { a,b,c};
    }

main_STLStyle.cpp:

    #include <iostream>
    #include <array>
    #include "Cards_STLStyle.h"
    
    using namespace std;
    
    int main(int argc, const char * argv[]) {

        Cards cards;
        my_array list=cards.function2();
        cout<<list[1]<<endl;
        return 0;
    }

请在此处找到更多信息:

array

【讨论】:

    猜你喜欢
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多