
初心者で申し訳なくおもいます。
下記のソースでコンパイルエラーが出ます。
// 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で質問しましょう!
似たような質問が見つかりました
- その他(プログラミング・Web制作) python fbprophetについて 1 2022/09/29 19:44
- C言語・C++・C# c言語の問題の説明、各所ごとに 5 2023/07/26 11:03
- C言語・C++・C# const char** p;のとき、free(p)でC4090エラーとなるのはなぜですか 3 2023/03/31 16:28
- 戦争・テロ・デモ ウクライナ、メル友に五千円要求されてさ 1 2022/04/02 09:38
- 英語 ニーチェの名言の文の構造について 3 2022/07/31 21:21
- MySQL 参考書に従って入力したつもりでしたが、最後はエラーがでました。 1 2022/09/28 03:45
- 大学受験 英作文の添削をお願いしたいです。 2 2022/08/19 20:37
- C言語・C++・C# [至急]Project Euler:#18Maximum path sum Iコード、入力出力、解説 4 2022/09/24 02:49
- PHP php テーブルが作成できない 1 2022/11/17 23:41
- 公的扶助・生活保護 保証人? 1 2022/05/17 22:42
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
文字列から空白を取り除きたい...
-
c++ 文字列を入力して、一文字...
-
バイナリファイルをコピーする...
-
fgetsなどのときのstdinのバッ...
-
型変換
-
テキストデータをそのままバイ...
-
C言語 文字列操作
-
charってなんですか??
-
配列をnビットシフトする
-
決まった文字列幅でのスクロール
-
C言語でのRS232Cとの通信
-
WORDをcharに変換する方法
-
enumの記述について。
-
”あいうえお” を ”おえういあ...
-
c言語配列の結合についてです。...
-
atoi( ) の反対をやりたい
-
charでの計算?
-
C言語のfor文です。 繰り返しの...
-
C#からC言語で作成したDLLに文...
-
ソケット通信において、送信相...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
文字列から空白を取り除きたい...
-
fgetsなどのときのstdinのバッ...
-
テキストデータをそのままバイ...
-
charからLPTSTRへの変換方法
-
C言語のfor文です。 繰り返しの...
-
ネットワークにつながっている...
-
絶対パスからのファイル名の切...
-
型変換
-
'const char *' 型は 'char *' ...
-
CStringをwchar_tに変換したい
-
atoi( ) の反対をやりたい
-
charでの計算?
-
Win32APIでのエディットボック...
-
str系関数を使わずに二つの文字...
-
間接操作のレベルとは
-
strncpyと_tcsncpy_sのヌルの扱...
-
c++ 文字列を入力して、一文字...
-
配列をnビットシフトする
-
switch文で文字を比較すること...
-
-'0'の意味について
おすすめ情報