
初心者で申し訳なくおもいます。
下記のソースでコンパイルエラーが出ます。
// 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 で行っております。
No.1ベストアンサー
- 回答日時:
文字の問題です。
文字列はワイドバイトで扱われます。ですので、それに対応した形でコーディングする必要があります。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
アドバイス誠に有難う御座いました。
MessageBox(NULL, L"WinMainの入口", L"WinMain", MB_OK);
TCHAR sDat[80];
TCHAR *s
等修正でエラーが取れました。
今後とも宜しくお願い致します。
No.5
- 回答日時:
既に回答が幾つかでていますが、
これはUnicode版である関数をリンクしようとしている為に、
Const文字列として渡している文字列がワイドキャラクタに
変換できないというエラーです。
const文字列を直接指定する場合はTEXTマクロを使用します。
> MessageBox(NULL, sDat, TEXT("コンストラクタ"), MB_OK);
TEXTマクロはTCHAR型などと同様、
マルチバイト環境・ユニコード環境をそれぞれマクロで
スイッチできるマクロです。
No.4
- 回答日時:
>>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"テストです");
有難うございました
コンパイルはエラーが取れました。
pick52さんのご回答の様に修正いたしました。
今後とも宜しくお願い致します。
No.2
- 回答日時:
ワイドキャラクタを使用する場合は型はcharではなくwchar_tです。
基本的にはchar型やwchar_t型を直接使用するのではなく設定によって
自動で切り替えられるLPTSTRというマクロを使用した方がいいでしょう。
参考URL:http://www.usefullcode.net/2006/11/tcharlpctstrl …
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
配列をnビットシフトする
-
Visual Studio strcpyについて
-
コンパイルエラー invalid ope...
-
atoi( ) の反対をやりたい
-
文字列から空白を取り除きたい...
-
入力された文字列から整数だけ...
-
TCHAR文字列?の特定部分の数字...
-
DxLibについて質問です
-
Win32APIでのエディットボック...
-
for next文、do while文、if文...
-
fgetsなどのときのstdinのバッ...
-
2進数でのシリアル通信
-
引数の渡し方と受け方(argv)
-
最早開始時間と最遅完了時刻を...
-
【C++】関数ポインタの使い方
-
既定のコンストラクタがありま...
-
Aの値からBの値を除するとは??
-
「Aに対するBの割合」と「Aに対...
-
信頼区間の1.96や1.65ってどこ...
-
a^2の√=a が成り立たない場合
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
文字列から空白を取り除きたい...
-
C言語のfor文です。 繰り返しの...
-
CStringをwchar_tに変換したい
-
テキストデータをそのままバイ...
-
ネットワークにつながっている...
-
fgetsなどのときのstdinのバッ...
-
double型の値をchar配列に変換...
-
atoi( ) の反対をやりたい
-
charからLPTSTRへの変換方法
-
'const char *' 型は 'char *' ...
-
間接操作のレベルとは
-
c言語プログラミング実行時エラ...
-
c++ 文字列を入力して、一文字...
-
【C言語】構造体内の領域解放(...
-
間接参照のレベルが異なっています
-
C言語の入力した文字を反転させ...
-
構造体のアライメント調整
-
strncpyと_tcsncpy_sのヌルの扱...
-
TCP/IP通信型大文字・小文字変...
-
char型からのバイト数取得
おすすめ情報