error C2440: 'argument' : cannot convert from 'System::Object ^' to 'int' - visual-studio-2005

this is my code:
for (q = 0; q < Rows; q++)
{
for (r = 0; r < Columns; r++)
{
Array1[q, r] = combo1->Items[r];
Array2[q, r] = combo2->Items[r];
}
}
I want to add items in arrays from combo boxes but I am getting the error:
error C2440: 'argument' : cannot convert from 'System::Object ^' to 'int'
this is the code which is adding items in combo
for (int m = 0; m < Rows; m++)
{
array<String^> ^b = Aray1[m]->Split(gcnew array<Char> { ',' });
for each (String ^Column in b)
{
Combo1->Items->Add(Column);
}
}

Assuming that you're using C++/CLI.
Judging by the error it looks like the Array1 elements are typed to int and the combo box contains values wrapped in an ObjecT^. If the value is truly just an int being wrapped in an Object^ then you just need to unbox
Array1[q,r] = safe_cast<int>(combo1->Items[r]);
Array2[q,r] = safe_cast<int>(combo2->Items[r]);
This will fall if the Object^ is actually wrapping another type besides int

Related

multi_array boost library error?

I got this error in C++. I am trying to implement Strassen matrix multiplication with multi_array. I assign one array to another which they same dimension.
Like that A11[i][j][k] = A[i][j][k]. I think reason is that kind of lines.
Assertion failed: (size_type(idx - index_bases[0]) < extents[0]),
function access, file
/usr/local/Cellar/boost/1.65.1/include/boost/multi_array/base.hpp,
line 136. Abort trap: 6
Do you know the reason? What does this error mean?
typedef boost::multi_array<int_type, 3> array_type;
array_type::extent_gen extents;
array_type A(boost::extents[size][size][noc]);
array_type B(boost::extents[size][size][noc]);
array_type C(boost::extents[size][size][noc]);
std::fill( A.origin(), A.origin() + A.num_elements(), 0 );
std::fill( B.origin(), B.origin() + B.num_elements(), 0 );
std::fill( C.origin(), C.origin() + C.num_elements(), 0 );
array_type Strr(int size,int noc,array_type A,array_type B, array_type C) {
if(size == 2) { //2-order
C=Matrix_Multiply(size,noc, A, B, C);
} else {
//
for(int i=0; i<size/2; i++) {
for(int j=0; j<size/2; j++) {
for(int k=0; k<noc; j++) {
A11[i][j][k] = A[i][j][k] ;
A12[i][j][k] = A[i][j+size/2][k] ;
}
}
}
My code is like that: I do not know what the problem is.
Error:Assertion failed: (size_type(idx - index_bases[0]) < extents[0]), function access, file /usr/local/Cellar/boost/1.65.1/include/boost/multi_array/base.hpp, line 136.
In the inner most loop you have:
for (int k = 0; k < noc; j++) {
You must have meant ++k instead of ++j:
for (int k = 0; k < noc; ++k) {
I'd simplify main too:
int dim[] = {size,size,noc};
array_type A(dim), B(dim), C(dim);
Value-initialization is done by default.
The idea of multi_array is that the arrays self-describe, instead of you passing separate parameters (size and noc e.g.):
array_type Strr(array_type A, array_type B) {
static_assert(array_type::dimensionality == 3, "static invariant");
size_t size = A.shape()[0];
size_t noc = A.shape()[2];
assert(A.shape()[0] == A.shape()[1]);
assert(std::equal_range(A.shape(), A.shape()+3, B.shape()));
assert(std::equal_range(A.shape(), A.shape()+3, C.shape()));

Copy element of nested std::vector to std::vector

I have encountered problem in copying the element of nested std::vector to another std::vector.
Example 1
std::vector<std::vector<int>> foo;
std::vector<int> temp;
std::vector<int> goo;
temp.push_back(12345);
foo.push_back(temp);
goo = foo[0]; //error
Example 2
temp.clear();
for(int i = 0; i<foo[0].size(); i++) {temp.push_back(foo[0][i])};
goo = temp; //error
Thus, can i know where is the problem and what should i do to copy the element of a nested vector to another vector??
EDIT:
The actual scenario would be i have nested vector of cv::Point
std::vector<std::vector<cv::Point>> found_contour;
and would like to copy the element inside a std::vector<cv::Point> in a struct.
struct Contours
{
std::vector<cv::Point> contour;
cv::RotatedRect minRect;
cv::RotatedRect minEllipse;
}
Code Snippet:
cv::findContours(result,found_contour,found_hierachy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,cv::Point(0,0));
std::vector<Contours> contour_struct;
contour_struct.reserve(found_contour.size());
for (size_t i = 0; i < found_contour.size(); i++)
{
contour_struct[i].contour = found_contour[i];
contour_struct[i].minRect = cv::minAreaRect(cv::Mat(found_contour[i]));
}
cv::findContours(result,found_contour,found_hierachy,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,cv::Point(0,0));
std::vector<Contours> contour_struct;
contour_struct.reserve(found_contour.size()); //<-----problem
for (size_t i = 0; i < found_contour.size(); i++)
{
contour_struct[i].contour = found_contour[i];
contour_struct[i].minRect = cv::minAreaRect(cv::Mat(found_contour[i]));
}
vector::reserve only aquires space internally so that push_back does not run out of space. It does not actually add more objects into the vector. You can use this line instead:
contour_struct.resize(found_contour.size());
which will make sure that contour_struct is the right size.

Why does the sort not work?

the error is:
/usr/include/c++/4.8/bits/stl_algo.h:2159:29: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
if (__comp(*__i, *__first))
I am passing 2 strings X and Y. Now the sort should compare XY and YX and then return the X if XY>YX or return Y. X and Y will have values like - 33 or 9999.
string Solution::largestNumber(const vector<int> &A) {
int i,n;
vector<string> B;
string str;
for(i=0; i<A.size(); i++)
{
B[i]=to_string(A[i]);
}
sort(A.begin(), A.end(),[](const string lhs, const string rhs){
return rhs+lhs < lhs+rhs;
});
for(i=0; i<A.size(); i++)
{
str+= to_string(A[i]);
}
return str;}
You are calling std::sort on a range from A which is a std::vector<int>, so the compare function should compare int-s.
You could fill B initially with 0,1, .... A.size()-1 then, given two indexes i1 and i2 to compare, construct the strings and compare them.
the error is: /usr/include/c++/4.8/bits/stl_algo.h:2159:29: error: invalid conversion from 'int' to 'const char*' [-fpermissive] if (__comp(*__i, *__first))
sort(A.begin(), A.end(),[](const string lhs, const string rhs){...});
Your sort comparator is expecting to compare two std::strings. The element type of A is int. You created B to be a conversion of A's elements to B's element type of std::string. Use B.
Other notes:
string Solution::largestNumber(const vector<int> &A) {
int i,n; // n???
// vector<string> B; // B is empty. Calling B[i] will cause a
// segfault for attempting to access memory
// out of bounds.
vector<string> B(A.size()); // Fix: pass in the size. Now creates B
// with size elements that can be accessed.
string str;
// Consider using algorithms like std::transform
for(i=0; i<A.size(); i++)
{
B[i]=to_string(A[i]); // You won't segfault here now.
}
// Corrected your sort.
sort(B.begin(), B.end(),[](const string lhs, const string rhs){
return rhs+lhs < lhs+rhs;
});
// Appending should be done on the sorted string vector. Consider using
// a ranged-based for loop here.
for(i=0; i<B.size(); i++)
{
str+= B[i];
}
return str;
}

Sorting Structures

Basically I need to sort arrays of structs by value from highest to lowest.
I must read from file into structure and then sort it.
Initial information:
6
m k 250
f k 280
m p 240
f p 290
m s 63
f s 45
My attempt: (sorting part might be incorrect)
using namespace std;
struct clothes
{
char gender;
char type;
int price;
};
int main()
{
ifstream file("duomenys.txt");
int amount;
int end;
file >> amount;
end = amount;
clothes robe[amount];
for(int x = 0; x<amount; x++)
{
file >> robe[x].gender >> robe[x].type >> robe[x].price;
}
for(int x = amount - 1; x>0; x--)
{
for(int i = 0; i < end; i++)
{
if(robe[i].price > robe[i+1].price)
{
???
}
}
end--;
}
return 0;
}
I'm pretty new in programming so please keep your answer as beginner-friendly as possible as I don't know much.
How do I swap information between struct robe[0] and robe[1] and sort them after checking if price is higher ?
#Beta
So I came up with something that worked, I declared additional struct which I used for holding values while swapping, and then swapped each value (gender, type, price) seperately into holder seperately.
robeH.genderH = robe[i].gender;
robe[i].gender = robe[i+1].gender;
robe[i+1].gender = robeH.genderH;
robeH.typeH = robe[i].type;
robe[i].type = robe[i+1].type;
robe[i+1].type = robeH.typeH;
robeH.priceH = robe[i].price;
robe[i].price = robe[i+1].price;
robe[i+1].price = robeH.priceH;
Thanks for help
P.S. Is it good practice to do it like I did or is there better way

Swift - [Int] is not a subtype of #lvalue$T2

I'm trying to do a simple sort function in a swift playground but I get the following error:
Playground execution failed: error: <EXPR>:20:22: error: '[Int]' is not a subtype of '#lvalue $T2'
swap(&a,k,j) //Here in &a
I have written the following code:
let arr:[Int] = [1,3,53,24,52,1,234,5,3]
func swap(inout a:[Int], num1:Int, num2:Int){
var value = a[num1]
a[num1] = a[num2]
a[num2] = value
}
func sort(a:[Int]){
var j = 0
var k = 0
for (k = j ; k<=a.count; k++){
if(k != a.count){
if(a[k] < a[j]){
swap(&a,k,j)
}
}else{
j++;
}
}
}
print ("\(sort(arr)) is the array")
Any idea of why this does not work? Am I referencing the array incorrectly, thanks!
UPDATE:
As Martin R pointed out the errors, here is the corrected code:
var arr:[Int] = [1,3,53,24,52,1,234,5,3]
And the sort function:
func sort(inout a:[Int]) -> [Int]{
var j = 0
var k = 0
for (k = j ; k <= a.count; k++){
if(k != a.count){
if(a[k] < a[j]){
swap(&a,j,k)
}
}else{
j++;
k = j
}
}
return a
}
Finally:
print ("\(sort(&arr)) is the array")
The local parameter a in
func sort(a:[Int])
is by default a constant. Since you want to modify the passed array, you have
to declare that parameter as inout in the same way as in the swap() function:
func sort(inout a:[Int])
Moreover, the passed array has to be variable:
var arr:[Int] = [1,3,53,24,52,1,234,5,3]
and you have to prefix it with the ampersand when it is passed as an argument
for an in-out parameter, to indicate that it can be modified by the function:
sort(&arr)
println(arr)
Note also that
print ("\(sort(arr)) is the array")
does not print the array result because sort() has no return value.

Resources