Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

c++ - Invalid conversion from int* to int even though I delimit the pointer

I've received the following error:

NQueens.cpp:35:19: error: invalid conversion from 'int*' to 'int' [-fpermissive]
    solution[i].y = *(possibilities + i);

I believe I delimited the pointer so that I can apply the value being pointed to the y coordinate for the n-queens problem. Am I missing anything, or is my compiler being dumb?

My header file:

#pragma once
#ifndef QUEEN
#define QUEEN

struct Queen {
    int x, y;
};
#endif // !QUEEN

My C++ code:

#include "Queen.h"
#include <iostream>
#include <vector>

using namespace std;

bool isDiag(int, int, int, int);
bool solutionFinder(int, int, vector<int*>);

int main()
{
    int n;
    cout << "Enter a number n for the n-queen problem:
";
    cin >> n;
    vector<int*> possibilities;
    for (int i = 0; i < n; i++)
    {
        int arrayInt[n];
        arrayInt[0] = i + 1;
        for (int j = 1; j < n; j++)
        {
            arrayInt[j] = 0;
        }
        possibilities.push_back(arrayInt);
    }
    if (!solutionFinder(1, n, possibilities))
    {
        cout << "No Solution.
";
    }

    Queen solution[n];
    for (int i = 0; i < n; i++)
    {
        solution[i].x = i + 1;
        solution[i].y = *(possibilities.begin() + i);
    }
    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;
}

UPDATE: I've tried the fix that mch suggested and now it compiles fine. However, I've ran into a new problem. When trying to print the coordinates for the queens, I'm getting what I believe are the locations of the values in memory. Example:

Enter a number n for the n-queen problem:
4
Solution: [(1,1163548928), (2,32764), (3,1163548704), (4,32764)]
question from:https://stackoverflow.com/questions/65927316/invalid-conversion-from-int-to-int-even-though-i-delimit-the-pointer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 Queens 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);
    }
    ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...