There are multiple issues with this code, but your immediate problem stems from this:
vector<int*> possibilities;
...
for (...)
{
int arrayInt[n];
...
possibilities.push_back(arrayInt);
}
...
solution[i].y = *(possibilities.begin() + i);
You have a vector of int*
pointers, which are pointing at int[]
arrays which you are creating inside a loop (and will be destroyed at the end of each loop iteration, thus leaving the vector holding dangling pointers to invalid memory).
You are then iterating through the vector
, extracting each int*
pointer and trying to assign it as-is to a single int
, thus the compiler error. You would need to instead dereference the int*
to get individual int
values from the array, eg:
int *arr = *(possibilities.begin() + i);
solution[i].y = arr[someIndex];
Based on what you have shown, I think you have implemented your solutionFinder()
incorrectly (but you didn't show how you actually implemented it). Rather than outputting a vector
of int[]
arrays, it would be better to have it output a vector of Queen
s instead, eg:
#include "Queen.h"
#include <iostream>
#include <vector>
using namespace std;
bool isDiag(int, int, int, int);
bool solutionFinder(int, int, vector<Queen>&);
int main()
{
int n;
cout << "Enter a number n for the n-queen problem:
";
cin >> n;
vector<Queen> solution;
solution.reserve(n);
if (!solutionFinder(1, n, solution))
{
cout << "No Solution.
";
return 0;
}
cout << "Solution: [(" << solution[0].x << "," << solution[0].y << ")";
for (int i = 1; i < n; ++i)
cout << ", (" << solution[i].x << "," << solution[i].y << ")";
cout << "]
";
return 0;
}
bool isDiag(int, int, int, int)
{
// ...
}
bool solutionFinder(int, int, vector<Queen> &solution)
{
// populate solution with Queen's as needed...
for (...)
{
Queen q;
q.x = ...;
q.y = ...;
solution.push_back(q);
}
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…