<解決しました>関数のポインタの配列がうまくいかない

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
残念
記事: 36
登録日時: 8年前

<解決しました>関数のポインタの配列がうまくいかない

#1

投稿記事 by 残念 » 8年前

こんにちは。この度質問をさせていただく「残念」と申します。よろしくお願いします。
Source.cpp

コード:

#include "DxLib.h"
#include "Ztaizen.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{
	//宣言
	int x, y;
	int MouseX, MouseY;
	int key;
	int handle, grhandle;
	int red[4][1920][1080];
	int green[4][1920][1080];
	int blue[4][1920][1080];
	int window;
	int mode[4] = { 0, 0, 0, 0 };




	if (DxLib_Init() == -1)        
	{
		return -1;        
	}


	//設定
	window = 0;
	handle = MakeARGB8ColorSoftImage(1920, 1080);
	SetDrawScreen(DX_SCREEN_BACK);
	SetMouseDispFlag(TRUE);

	//実行
	while (ProcessMessage() == 0 && CheckHitKey(KEY_INPUT_ESCAPE) == 0){
		ClearDrawScreen();
		GetMousePoint(&MouseX, &MouseY);
		windowsp[window][mode[window]](red, blue, green);
		for (x = 0; x < 1920; x++)
		{
			for (y = 0; y < 1080; y++)
			{
				DrawPixelSoftImage(handle,x,y,red[window][x][y],green[window][x][y],blue[window][x][y],0);
			}
		}
		grhandle = CreateGraphFromSoftImage(handle);
		DeleteSoftImage(handle);
		DrawGraph(0, 0, grhandle, TRUE);
		key = GetJoypadInputState(DX_INPUT_KEY_PAD1);
		if (CheckHitKey(KEY_INPUT_ESCAPE) == 1)
		{
			break;
		}
		if (CheckHitKey(KEY_INPUT_F1) == 1){
		
			window = 1;
		}
		if (CheckHitKey(KEY_INPUT_F2) == 1){

			window = 2;
		}
		if (CheckHitKey(KEY_INPUT_F3) == 1){

			window = 3;
		}
		if (CheckHitKey(KEY_INPUT_BACK) == 1){

			window = 0;
		}
		if (CheckHitKey(KEY_INPUT_LSHIFT) == 1){

			if (mode[window]>=4){

				mode[window] += 1;
			}
			else
			{
				mode[window] = 0;
			}
		}
	
		
		ScreenFlip();
		

	}
	
	DxLib_End();            
	return 0;            
} 
Ztaizan.h

コード:

void windows(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows11(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows12(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows13(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows14(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows21(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows22(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows23(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows24(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows31(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows32(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows33(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
void windows34(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]);
static void(*windowsp[][4])(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080])
= {
	windows, windows, windows, windows,
	windows11, windows12, windows13, windows14,
	windows21, windows22, windows23, windows24,
	windows31, windows32, windows33, windows34
}; 
Ztaizenc.cpp

コード:

#include"Ztaizen.h"
//今のところそれぞれの関数には適当なことを書いています
void windows(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window11(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window12(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window13(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window14(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window21(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window22(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window23(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window24(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window31(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window32(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window33(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
void window34(int red[4][1920][1080], int green[4][1920][1080], int blue[4][1920][1080]){
	int x = 0, y = 0;
	for (x = 0; x < 1920; x++)
	{
		for (y = 0; y < 1080; y++)
		{
			red[3][x][y] = 1;
			blue[3][x][y] = 1;
			green[3][x][y] = 1;
		}
	}
}
 


このようなコードを作ってみたところ、

エラー 23 error LNK2001: 外部シンボル ""void __cdecl windows34(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows34@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 24 error LNK2001: 外部シンボル ""void __cdecl windows34(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows34@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 21 error LNK2001: 外部シンボル ""void __cdecl windows33(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows33@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 22 error LNK2001: 外部シンボル ""void __cdecl windows33(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows33@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 19 error LNK2001: 外部シンボル ""void __cdecl windows32(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows32@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 20 error LNK2001: 外部シンボル ""void __cdecl windows32(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows32@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 17 error LNK2001: 外部シンボル ""void __cdecl windows31(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows31@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 18 error LNK2001: 外部シンボル ""void __cdecl windows31(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows31@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 15 error LNK2001: 外部シンボル ""void __cdecl windows24(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows24@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 16 error LNK2001: 外部シンボル ""void __cdecl windows24(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows24@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test
エラー 13 error LNK2001: 外部シンボル ""void __cdecl windows23(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows23@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 14 error LNK2001: 外部シンボル ""void __cdecl windows23(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows23@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 11 error LNK2001: 外部シンボル ""void __cdecl windows22(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows22@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 12 error LNK2001: 外部シンボル ""void __cdecl windows22(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows22@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 9 error LNK2001: 外部シンボル ""void __cdecl windows21(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows21@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 10 error LNK2001: 外部シンボル ""void __cdecl windows21(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows21@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 7 error LNK2001: 外部シンボル ""void __cdecl windows14(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows14@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 8 error LNK2001: 外部シンボル ""void __cdecl windows14(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows14@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 5 error LNK2001: 外部シンボル ""void __cdecl windows13(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows13@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 6 error LNK2001: 外部シンボル ""void __cdecl windows13(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows13@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 3 error LNK2001: 外部シンボル ""void __cdecl windows12(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows12@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 4 error LNK2001: 外部シンボル ""void __cdecl windows12(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows12@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 1 error LNK2001: 外部シンボル ""void __cdecl windows11(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows11@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Source.obj Win32Project-test

エラー 2 error LNK2001: 外部シンボル ""void __cdecl windows11(int (* const)[1920][1080],int (* const)[1920][1080],int (* const)[1920][1080])" (?windows11@@YAXQAY1HIA@EDI@H00@Z)" は未解決です。 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Ztaizenc.obj Win32Project-test

エラー 25 error LNK1120: 12 件の未解決の外部参照 C:\Users\---\documents\visual studio 2013\Projects\Win32Project-test\Debug\Win32Project-test.exe Win32Project-test

という風にエラーがでました。
改善策を教えてくださればありがたいです。
最後に編集したユーザー 残念 on 2015年6月20日(土) 08:56 [ 編集 1 回目 ]

アバター
h2so5
副管理人
記事: 2212
登録日時: 13年前
住所: 東京
連絡を取る:

Re: 関数のポインタの配列がうまくいかない

#2

投稿記事 by h2so5 » 8年前

「window」と「windows」が混在していませんか?

残念
記事: 36
登録日時: 8年前

Re: 関数のポインタの配列がうまくいかない

#3

投稿記事 by 残念 » 8年前

h2so5さん、指摘してくださり有難うございます。関数の「window」を「windows」に直しておきます。
あと、恐らく解決した(チャット内で)ので、新たなコードを書き次第、投稿させていただきます。
有難うございました

残念
記事: 36
登録日時: 8年前

Re: 関数のポインタの配列がうまくいかない

#4

投稿記事 by 残念 » 8年前

チャット内で

コード:

 
#include <iostream>
using namespace std;

// sub.cpp
// printHoge() と printFoo() は隠ぺいする

static void printHoge()
{
	cout << "Hoge" << endl;
}

static void printFoo()
{
	cout << "Foo" << endl;
}

static void ( *funcPtr[3] )( void ) =
{
	printHoge, printFoo, NULL,
};

// 外部からアクセスできる唯一の関数
void print( int num )
{
	funcPtr[ num ]();
}

// main.cpp

int main()
{
	print(0);
	print(1);
	
	return 0;
}
「サブの実装ファイルに関数ポインタと処理を書き、ヘッダにprint関数のプロトタイプを、実装ファイルで実装します。」
とのことでした。教えてくださった「Rittai_3D」さん、わざわざ見本のコードまで書いてくださり有難うございました。
なお、これに沿って実行した結果、何故か「ハンドルされない例外が 0x0144F577 (Win32Project-test.exe) で発生しました: 0xC0000005: 場所 0x001F0000 の読み取り中」
となってしまったので(恐らくまた別の問題でしょう)また質問させていただきます。

Rittai_3D
記事: 525
登録日時: 11年前

Re: 関数のポインタの配列がうまくいかない

#5

投稿記事 by Rittai_3D » 8年前

チャットでサンプルコードを書いた3Dです。

現在スマホからなので確認でませんが、そのコードを実行したらエラーが出たのですか?
それとも、どこかいじりましたか?
初心者です

残念
記事: 36
登録日時: 8年前

Re: 関数のポインタの配列がうまくいかない

#6

投稿記事 by 残念 » 8年前

この度は有難うございました。残念です。
3Dさんのコードを参考(パクr)にさせていただき、自分のコードを変えてみたところこうなりました。何か別の問題があったのだと思われます。新たにトピックを立てましたので、(そちらにコードを書いてありますので)また添削(っていうのかな…)してくださるとありがたいです。

box
記事: 2002
登録日時: 13年前

Re: 関数のポインタの配列がうまくいかない

#7

投稿記事 by box » 8年前

こんな感じ?

コード:

#include <stdio.h>

typedef int (*f)(int a, int b);

int add(int a, int b)
{
    return a + b;
}

int sub(int a, int b)
{
    return a - b;
}

int mul(int a, int b)
{
    return a * b;
}

int div(int a, int b)
{
    return a / b;
}

int main(void)
{
    f func[] = { add, sub, mul, div };
    int i;

    for (i = 0; i < sizeof(func) / sizeof(func[0]); i++) {
        printf("%d\n", (*func[i])(10, 5));
    }
    return 0;
}
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

残念
記事: 36
登録日時: 8年前

Re: 関数のポインタの配列がうまくいかない

#8

投稿記事 by 残念 » 8年前

解決の際、投稿時に送信ボタンの隣の解決にチェックを入れる、ということを知らず放置していたため、書き込むとともに、チェックを入れておきます

閉鎖

“C言語何でも質問掲示板” へ戻る