Как передать ссылку на массив в качестве параметра из сборки в функцию C ++

У меня есть два отдельных файла в двух проектах Visual Studio 2012. Один из них MASM, а другой — C ++. Программа MASM должна вызывать функцию DisplayBoard в программе на C ++ и должна передавать ссылку на отображаемый массив. Я не могу понять, что именно мне нужно сделать, чтобы сделать эту работу. Программа была полностью создана как программа C ++ и работает так, как должна, но мы должны выполнять большую часть кодирования в MASM и иметь минимальные функции C ++, поэтому мы пытаемся заставить эти два файла говорить, но у нас есть проблемы , Вот скелетные коды для моих файлов MASM и C ++. Я не уверен, нужен ли файл C ++ main, но он компилируется без него. Кроме того, нужно ли объявить массив board в файле C ++, если он передан в качестве параметра? Я думаю, что нет, но я не уверен. Я не знаю, правильно ли указан параметр массива в файле C ++ или нет.

КОД СБОРКИ:

TITLE HexAssemblyTest    (HexAssemblyTest.asm)
.586
.model flat,C

includelib kernel32.lib
includelib Irvine32.lib

ShowBoard PROTO near C, hexBoard:SDWORD

.data

board SDWORD 121 DUP (0)        ;array to hold the hex board.code

main PROC

INVOKE ShowBoard, ADDR board    ;display board

Retn
main ENDP

END main

C ++ КОД:

#include "stdafx.h"#include<iostream>
#include<iomanip>
#include<Windows.h>
#include<stack>

using namespace std;

extern "C" void showBoard(int hex_array[]);//Class DFS definition
class DFSAlgorithm{

public:
int board[121]; //board array//function to display the board
void showBoard(int hex_array[]){

//code here...

}
//other functions...removed
}

};//end DFSAlgorithm class

Это ошибка, которую мы получаем:

—— Начата сборка: Проект: HexAssembly, Конфигурация: Отладка Win32 ——
1> Сборка HexAssemblyTest.asm …
1> HexAssemblyTest.obj: ошибка LNK2019: неразрешенный внешний символ _ShowBoard, указанный в функции _main
1> C: \ Irvine \ examples \ Assembly Hex-программирование \ Debug \ HexAssembly.exe: фатальная ошибка LNK1120: 1 неразрешенная внешняя программа
========== Построение: 0 выполнено, 1 не выполнено, 0 обновлено, 0 пропущено ===========

Я думаю, что теперь все работает правильно … Я изменил DFSAlgorithm.cpp и DFSAlgorithm.h, скомпилировал файл C ++ и добавил файл DFSAlsogrithm.obj в проект, содержащий файл сборки. Теперь они связываются, но я получаю сообщение об ошибке «deque iterator not dreference» теперь, когда выполняется поиск C ++ DFS. Он работал нормально, пока вся программа была на C ++, поэтому я не уверен, что мне нужно изменить, чтобы заставить его работать правильно, теперь, когда к массиву обращаются из файла сборки. Проходя через мой отладчик, я вижу, что он генерирует массивы смежности, но я не думаю, что к массиву действительно обращаются …

TITLE HexAssemblyTest    (HexAssemblyTest.asm)

INCLUDE Irvine32.inc

printSomething PROTO C ;displays "GoobersX"DFS PROTO C, color:BYTE, bptr:PTR DWORD, index:SDWORD

PDWORD TYPEDEF PTR DWORD

.data

bptr PDWORD board
board SDWORD 121 DUP (0)        ;array to hold the hex board
arrayIndex SDWORD 0         ;variable to hold arrayIndex.code

main PROC

INVOKE printSomething   ;tests if MASM and C++ are talking

Start:
CALL PlaceRed       ;prompt user to place a red stone
CALL clrscr
CALL crlf
CALL ShowBoard      ;redraw the board

;check if there is a valid path using C++ DFS
PUSH EDX
PUSH EBX
PUSH ECX
INVOKE DFS, 1, ADDR board, 0    ;color red, board address, arrayIndex 0
POP ECX
POP EBX
POP EDX
CMP EAX,1       ;if eAx == 1 winning path found
JNE Continue        ;eAx != 1 no valid path...continue game

;the rest of this code removed for brevity

END_GAME:

Retn
main ENDP

Мой заголовочный файл C ++ выглядит так:

C++ header file DFSAlgorithm.h

#ifndef DFSAlgorithm_H
#define DFSAlgorithm_H
extern "C" void printSomething();
extern "C" int DFS(int color, int hex_array[], int array_index);

#endif

И мой C ++ cpp файл (сокращенно) выглядит так:

#include "stdafx.h"#include<iostream>
#include<stack>
#include "DFSAlgorithm.h"//include definition of class DFSAlgorithm
using namespace std;

int adjacency[6];
stack<int> path; //stack to hold the last hex visited

//test printsomething
extern "C" void printSomething(){
cout<<"Goobers2014";
}

//First call of DFS always starts with array_index ==  0
extern "C" int DFS(int color, int hex_array[], int array_index){

if (hex_array[array_index] == color){ //if hex has an appropriately colored stone

hex_array[array_index] += 3;    //mark the hex as visited

path.push(array_index); //push hex onto path stack
}
if ((color == 1 && array_index % 11 == 10 && hex_array[array_index] == 4) ||
(color == 2 && array_index / 11 == 10 && hex_array[array_index] == 5)){

return 1; //winner base case==>reached the other side
}

//If a visited/unvisited hex has a stone of correct color==> search adjacent hexes
if ((color == 1 &&  hex_array[array_index] == 4)  ||
(color == 2  &&  hex_array[array_index] == 5)){

//get adjacencies
//removed from code for brevity
}

/*Initialize adjacentHexes to zero: if == 0 after all 6 adjacencies are
checked it is a dead end as there are no unvisited adjacent hexes with
the correct color stone*/
int adjacentHexes = 0;
for(int b = 0; b < 6; b++){//traverse adjacency array of passed in index

//if one of the adjacent hexes has a red/blue stone
if((color == 1 && hex_array[adjacency[b]] == color) ||
(color == 2 && hex_array[adjacency[b]] == color )){

adjacentHexes++;    //increment adjacentHexes count

hex_array[adjacency[b]] += 3;   //mark the hex as visited

path.push(adjacency[b]); //push visited adjacent hex onto path

//recursively call DFS with that adjacent hex index
return DFS(color, hex_array,adjacency[b]);

}
}
//If adjacentHexes == 0 ==> dead-end
if(adjacentHexes == 0 && path.size() > 1){

path.pop();//pop the top hex from the stack if stack > 1

//recursive call of DFS with the new top red/blue hex
return DFS(color, hex_array,path.top());
}
if(adjacentHexes == 0 && path.size() == 1){//back to Row 0/Column 0

//make the array_index = the top of the path stack
//+++++this line generates a "deque iterator not dereferenceable" error++++++++++++++
array_index = path.top();

//pop remaining element from the stack so path is now zero
path.pop();
}
}
//if checking for a red path and path is empty
if (color == 1 ){

//search remaining column 0 hexes for unvisited red hex
for(array_index ; array_index <= 99; ){

//recursively call DFS with next Column 0 hex
return DFS(color, hex_array, array_index + 11);
}
}

//if checking for a blue path and path is empty
if (color == 2){

//search remaining row 0 hexes for unvisted blue hex
for(array_index ; array_index <= 9; ){

//recursively call DFS with next Row 0 hex
return DFS(color, hex_array, array_index + 1);
}
}
//Traverse hex_array and reset all visited hexes to unvisited
for(int a = 0; a < 121; a++){
if(hex_array[a] >= 4)//if hex has been visited
hex_array[a] -= 3;//remove visited designation
}

return -1;//return false as no path exists

}

Я не уверен, почему происходит сбой в строке, где я устанавливаю array_index в path.top (), а затем вытаскиваю верх из стека, потому что он работал нормально, когда весь файл был в C ++, поэтому я не уверен, почему это так не работает сейчас. Я предполагаю, что это как-то связано с тем, как функция C ++ обращается к array_index.

0

Решение

Ошибка говорит вам о проблеме очень ясно; у вас нет определения глобальной функции ShowBoard,

Если бы вы ожидали DFSAlgorithm::showBoardЭто определение, то вы будете разочарованы по двум причинам:

  1. DFSAlgorithm::showBoard не глобальная функция, а функция-член (на которой экземпляр DFSAlgorithm это будет работать?);
  2. showBoard а также ShowBoard пишутся по-разному.

Что касается main, ваш файл C ++ не должен определять main потому что ваш сборочный файл делает, и вы хотите только один такое определение по всей вашей программе.

3

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]