suma
suma

Reputation: 728

how to port CArray (MFC) to linux

I am porting a windows app to linx. I am trying to port CArray MFC Method to linux. The CArray to be ported is

CArray<double,double> min;

I have made an equivalent of this like...

#include <iostream>
#include <vector>
#include <list>

int main ()
{
        struct values
        {
            double value1;
            double value2;
        };
        typedef std::vector<values> CArray;
        CArray min;
        CArray max;
    return 0;
}

but i am getting erros like...

vec1.cpp: In function ‘int main()’:
vec1.cpp:12:29: error: template argument for ‘template<class _Alloc> class std::allocator’ uses local type ‘main()::values’
vec1.cpp:12:29: error:   trying to instantiate ‘template<class _Alloc> class std::allocator’
vec1.cpp:12:29: error: template argument 2 is invalid
vec1.cpp:12:37: error: invalid type in declaration before ‘;’ token

Please if anybody has work on this porting of CArray to linux equivalent, please provide the solution.

Upvotes: 1

Views: 1709

Answers (3)

Marius Bancila
Marius Bancila

Reputation: 16338

The STL equivalent for MFC's CArray<double,double> is simply std::vector<double>.

Upvotes: 2

Nemanja Boric
Nemanja Boric

Reputation: 22177

You need to make your struct non-local type:

#include <iostream>
#include <vector>
#include <list>

struct values
{
   double value1;
   double value2;
};

int main ()
{
        typedef std::vector<values> CArray;
        CArray min;
        CArray max;
    return 0;
}

However, instead of using struct to hold pair of values, you should benefit from C++ and use std::pair:

#include <iostream>
#include <vector>
#include <list>
#include <utility>

typedef std::vector<std::pair<double, double> > CArray;

int main ()
{
    
    CArray min;
    CArray max;
    return 0;
}

Upvotes: 4

Nik Bougalis
Nik Bougalis

Reputation: 10613

The first line with error describes what the problem is:

template argument for ‘template<class _Alloc> class std::allocator’ uses local type ‘main()::values’

Now, let's think... if the compiler complains about values being local to main, how can we fix that? We could, perhaps make values not local to main? So the answer you're looking for is to move the declaration of values outside of main and at global scope.

Of course, the problem is that the question is wrong: it should be why you need a struct and two double values in your std::vector? After all, the CArray you showed us doesn't hold two double values. It holds one so std::vector<double> should do the trick. You may find the documentation for CArray helpful as you try to port this code.

Upvotes: 2

Related Questions