プロが教えるわが家の防犯対策術!

初心者で申し訳なくおもいます。
下記のソースでコンパイルエラーが出ます。

// CnstDst.cpp : アプリケーション用のエントリ ポイントの定義
//

#include "stdafx.h"

//CnstDst クラス定義
class CnstDst {
char sDat[80];
public:
CnstDst(char *s);
~CnstDst();
};

CnstDst cd1("テストです");

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: この位置にコードを記述してください。
MessageBox(NULL, "WinMainの入口", "WinMain", MB_OK);
CnstDst cd2("テスト1");
MessageBox(NULL, "テスト2", "WinMain", MB_OK);

return 0;
}

CnstDst::CnstDst(char *s)
{
lstrcpy(sDat, s);
MessageBox(NULL, sDat, "コンストラクタ", MB_OK);
}
CnstDst::~CnstDst()
{
MessageBox(NULL, sDat, "デストラクタ", MB_OK);
}

コンパパイルエラーは下記です。

プロジェクト 'CnstDst - Win32 (WCE MIPSII_FP) Debug' 用の中間ファイルおよび出力ファイルを削除しています。
--------------------構成 : CnstDst - Win32 (WCE MIPSII_FP) Debug--------------------
リソースをコンパイル中...
コンパイル中...
StdAfx.cpp
コンパイル中...
CnstDst.cpp
C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(22) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [14]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(24) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [9]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(31) : error C2664: 'wcscpy' : cannot convert parameter 1 from 'char [80]' to 'unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(32) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [80]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(36) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [80]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
clmips.exe の実行エラー

CnstDst.exe - エラー 5、警告 0

Microsoft eMbedded Visuai C++ Win32 WCE で行っております。

A 回答 (5件)

文字の問題です。

文字列はワイドバイトで扱われます。ですので、それに対応した形でコーディングする必要があります。

MessageBox(NULL, L"WinMainの入口", L"WinMain", MB_OK);
TCHAR sDat[80];
TCHAR *s

などとしてみてください。

#eMbedded Visual C++ は少ししか使ったことないんで、
#事情が違ってたらすみません。

この回答への補足

ソース下記のように変更しました。
パソコン環境ですがWIN XPで行っております。

// CnstDst.cpp : アプリケーション用のエントリ ポイントの定義
//

#include "stdafx.h"

//CnstDst クラス定義
class CnstDst {
TCHAR sDat[80];
public:
CnstDst(TCHAR *s);
~CnstDst();
};

CnstDst cd1("テストです");

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: この位置にコードを記述してください。
MessageBox(NULL, L"WinMainの入口", L"WinMain", MB_OK);
CnstDst cd2(L"テスト1");
MessageBox(NULL, L"テスト2", L"WinMain", MB_OK);

return 0;
}

CnstDst::CnstDst(TCHAR *s)
{
lstrcpy(sDat, s);
MessageBox(NULL, sDat, L"コンストラクタ", MB_OK);
}
CnstDst::~CnstDst()
{
MessageBox(NULL, sDat, L"デストラクタ", MB_OK);
}

コンパイルエラーは1つになりました。
すみませんが最後のエラーは?
御教授お願いします。

--------------------構成 : CnstDst - Win32 (WCE x86) Debug--------------------
コンパイル中...
CnstDst.cpp
C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(14) : error C2664: '__thiscall CnstDst::CnstDst(unsigned short *)' : cannot convert parameter 1 from 'char [11]' to 'unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
cl.exe の実行エラー

CnstDst.exe - エラー 1、警告 0

補足日時:2008/08/26 15:44
    • good
    • 0
この回答へのお礼

アドバイス誠に有難う御座いました。
MessageBox(NULL, L"WinMainの入口", L"WinMain", MB_OK);
TCHAR sDat[80];
TCHAR *s
等修正でエラーが取れました。
今後とも宜しくお願い致します。

お礼日時:2008/08/26 17:59

既に回答が幾つかでていますが、


これはUnicode版である関数をリンクしようとしている為に、
Const文字列として渡している文字列がワイドキャラクタに
変換できないというエラーです。

const文字列を直接指定する場合はTEXTマクロを使用します。
> MessageBox(NULL, sDat, TEXT("コンストラクタ"), MB_OK);
TEXTマクロはTCHAR型などと同様、
マルチバイト環境・ユニコード環境をそれぞれマクロで
スイッチできるマクロです。
    • good
    • 0
この回答へのお礼

アドバイス誠に有難う御座いました。
2バイト文字の件でした。
今後とも宜しくお願い致します。

お礼日時:2008/08/26 17:51

>>ANo.2


あ、ちょっと間違えた。
エラーの意味をよく考えてください。

> CnstDst.cpp
> C:\Documents and Settings\中野\デスクトップ\テストフォルダ\CnstDst\CnstDst.cpp(14) : error C2664: '__thiscall CnstDst::CnstDst(unsigned short *)' : cannot convert parameter 1 from 'char [11]' to 'unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

CnstDst.cppの14行目の型が間違っているといっています。

> CnstDst cd1("テストです");

CnstDst cd1(L"テストです");
    • good
    • 0
この回答へのお礼

有難うございました
コンパイルはエラーが取れました。
pick52さんのご回答の様に修正いたしました。
今後とも宜しくお願い致します。

お礼日時:2008/08/26 17:48

 こんにちは。



>>すみませんが最後のエラーは?
 ↓此処では?
 ×CnstDst cd2("テスト1");
 ○CnstDst cd2(L"テスト1");
    • good
    • 0
この回答へのお礼

エラーが取れました。
アドバイス誠に有難う御座いました。
今後とも宜しくお願い致します。

お礼日時:2008/08/26 17:53

ワイドキャラクタを使用する場合は型はcharではなくwchar_tです。


基本的にはchar型やwchar_t型を直接使用するのではなく設定によって
自動で切り替えられるLPTSTRというマクロを使用した方がいいでしょう。

参考URL:http://www.usefullcode.net/2006/11/tcharlpctstrl …
    • good
    • 0
この回答へのお礼

アドバイス誠に有難う御座いました。
参考になります。
今後とも宜しくお願い致します。

お礼日時:2008/08/26 17:55

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!