0

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?

CKE
  • 1,533
  • 19
  • 18
  • 29
b219
  • 33
  • 4
  • 1
    In `c++` there is `std::array` which you can return from a function. You can not return c arrays. Related: [https://stackoverflow.com/questions/3473438/return-array-in-a-function](https://stackoverflow.com/questions/3473438/return-array-in-a-function) – drescherjm Nov 18 '20 at 20:02
  • 3
    That code should have puked the moment it saw `int Cards:: array()` - the declaration in the header states it returns `int *`, not `int`. Fixing that, you still return an automatic address, so you have plenty of UB left to contend with. If the result is dynamic, use a `std::vector`, if it's fixed `N` then use a `std::array` – WhozCraig Nov 18 '20 at 20:03
  • 1
    ***However, I cannot do these in a header and a separate cpp file*** You made a few changes when you moved from your first example to the header + implementation example. With that said its best to just use the correct c++ container for the task which in this case is std::array if you always want to return an array of 3 items. – drescherjm Nov 18 '20 at 20:07
  • 1
    In your second answer `int Cards:: function2(){` needs to be `int* Cards:: function2(){` and `int list[3]={a,b,c};` must be `static int list[3]={a,b,c};` however again `std::array` is better. You can just return that from a function in the same way as you return an int. You don't need the static part at all and no pointer. – drescherjm Nov 18 '20 at 20:16
  • 3
    actually your error reads as if the return type is pointer to member (`int Cards::*` is a pointer to an `int` member of `Cards`). Are you sure that the function signature is not `int Cards::* function2(){` – 463035818_is_not_an_ai Nov 18 '20 at 20:18

1 Answers1

0

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, e.g. 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

CKE
  • 1,533
  • 19
  • 18
  • 29