简体   繁体   中英

How can I write a function that returns to array inside the header?

I am trying to create a function that returns in main.cpp in the header and .cpp file and run it in the main function.

This process I do works on 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;
}

However, I cannot do these in a header and a separate cpp file.

I have a Cards header

#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

This is the class cpp file

#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]'
}

How do I fix this problem and run it in main?

As pointed out in the comments, there is already a SO thread

Return array in a function

which handles your issue.

If your really want to use C arrays then your program shall look like:

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;
    }

As recommended in the comments, you should use the containers of the STL, eg array for fixed length or vector for variable length. Assuming that fixed length of 3 will be fine for you, then your code would be looking like this:

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;
    }

Please find more information here:

array

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