c++ / c混淆

Posted

技术标签:

【中文标题】c++ / c混淆【英文标题】:c++ / c confusion 【发布时间】:2010-06-18 11:15:17 【问题描述】:

我正在尝试用 C++ 制作一个小应用程序,用这个库保存 midifile。 http://musicnote.sourceforge.net/docs/html/index.html

首页给出的示例代码如下所示。

 #include "MusicNoteLib.h"
    void main()
    
        MusicNoteLib::Player player; // Create the Player Object
        player.Play("C D E F G A B"); // Play the Music Notes on the default MIDI output port
    

这段代码在 Visual Studio 2008 中无法编译,我收到很多错误,例如

MusicNoteLib.h(22):错误 C4430: 缺少类型说明符 - 假定为 int。 注意:C++ 不支持 default-int

我不明白错误或从哪里开始查找... 还有一些dll文件可以代替这个h文件。

    #ifndef __MUSICNOTE_LIB_H__EBEE094C_FF6E_43a1_A6CE_D619564F9C6A__
#define __MUSICNOTE_LIB_H__EBEE094C_FF6E_43a1_A6CE_D619564F9C6A__

/** @file MusicNoteLib.h
 * \brief Main header file for accessing the MusicNote Library
 */ 

/// <Summary>
/// This header file can be included directly in your project or through
/// MusicNoteLib.h of the MusicNoteDll project. If included directly, this
/// will be built directly as a satic library. If included through MusicNoteDll
/// this will use dllImports through MUSICNOTELIB_API
/// </Summary>
#ifndef MUSICNOTELIB_API
#define MUSICNOTELIB_API
#endif // MUSICNOTELIB_API

//#include "Player.h"

namespace MusicNoteLib /// Music Programming Library

typedef  void (__stdcall *LPFNTRACEPROC)(void* pUserData, const TCHAR* szTraceMsg);
typedef  void (__stdcall *LPFNERRORPROC)(void* pUserData, long lErrCode, const TCHAR* szErrorMsg, const TCHAR* szToken);

extern "C"

    MUSICNOTELIB_API typedef void MStringPlayer;

    MUSICNOTELIB_API void* GetCarnaticMusicNoteReader();

    /// <Summary>
    /// Creates a MusicString Player object.
    /// </Summary>
    MUSICNOTELIB_API MStringPlayer* CreateMusicStringPlayer();

    /// <Summary>
    /// Plays Music string notes on the default MIDI Output device with the default Timer Resolution.
    /// Use PlayMusicStringWithOpts() to use custom values.
    /// @param szMusicNotes the Music string to be played on the MIDI output device
    /// @return True if the notes were played successfully, False otherwise
    /// </Summary>
    MUSICNOTELIB_API bool PlayMusicString(const TCHAR* szMusicNotes);

    /// <Summary>
    /// Same as PlayMusicString() except that this method accepts Callbacks.
    /// The Trace and Error callbacks will be used during the Parse of the Music Notes.
    /// @param szMusicNotes the Music string to be played on the MIDI output device
    /// @param traceCallbackProc the Callback to used to report Trace messages
    /// @param errorCallbackProc the Callback to used to report Error messages
    /// @param pUserData any user supplied data that should be sent to the Callback
    /// @return True if the notes were played successfully, False otherwise
    /// </Summary>
    MUSICNOTELIB_API bool PlayMusicStringCB(const TCHAR* szMusicNotes,
                                            LPFNTRACEPROC traceCallbackProc, 
                                            LPFNERRORPROC errorCallbackProc, 
                                            void* pUserData);

    /// <Summary>
    /// Plays Music string notes on the given MIDI Output device using the given Timer Resolution.
    /// Use PlayMusicString() to use default values.
    /// @param szMusicNotes the Music notes to be played
    /// @param nMidiOutPortID the device ID of the MIDI output port to be used for the play
    /// @param nTimerResMS preferred MIDI timer resolution, in MilliSeconds
    /// @return True if Play was successful, False otherwise
    /// </Summary>
    MUSICNOTELIB_API bool PlayMusicStringWithOpts(const TCHAR* szMusicNotes, int nMidiOutPortID, unsigned int nTimerResMS);

    /// <Summary>
    /// Same as PlayMusicStringWithOpts() except that this method accepts Callbacks.
    /// The Trace and Error callbacks will be used during the Parse of the Music Notes.
    /// @param szMusicNotes the Music notes to be played
    /// @param nMidiOutPortID the device ID of the MIDI output port to be used for the play
    /// @param nTimerResMS preferred MIDI timer resolution, in MilliSeconds
    /// @param traceCallbackProc the Callback to used to report Trace messages
    /// @param errorCallbackProc the Callback to used to report Error messages
    /// @param pUserData any user supplied data that should be sent to the Callback
    /// @return True if Play was successful, False otherwise
    /// </Summary>
    MUSICNOTELIB_API bool PlayMusicStringWithOptsCB(const TCHAR* szMusicNotes, 
                                                    int nMidiOutPortID, 
                                                    unsigned int nTimerResMS,
                                                    LPFNTRACEPROC traceCallbackProc, 
                                                    LPFNERRORPROC errorCallbackProc, 
                                                    void* pUserData);
    /// <Summary>
    /// Save the given MusicString content into a MIDI output file
    /// @param szMusicNotes Music Notes to be converted to MIDI output
    /// @param szOutputFilePath path of the MIDI output file
    /// @return True if the the content was saved successfully, False otherwise
    /// </Summary>
    MUSICNOTELIB_API bool SaveAsMidiFile(const TCHAR* szMusicNotes, const char* szOutputFilePath);

    //MUSICNOTELIB_API typedef void (*ParseErrorProc)(const MusicNoteLib::CParser*, MusicNoteLib::CParser::ErrorEventHandlerArgs* pEvArgs);
    //MUSICNOTELIB_API typedef void (*ParseTraceProc)(const MusicNoteLib::CParser*, MusicNoteLib::CParser::TraceEventHandlerArgs* pEvArgs);

    MUSICNOTELIB_API void Parse(const TCHAR* szNotes, LPFNTRACEPROC traceCallbackProc, void* pUserData);

 // extern "C"



 // namespace MusicNoteLib

#endif // __MUSICNOTE_LIB_H__EBEE094C_FF6E_43a1_A6CE_D619564F9C6A__

【问题讨论】:

嗯,错误消息是说某处缺少类型,例如int foo(bar)(bar 没有类型),因此假定您的意思是int bar 您是否以任何方式修改过此标头?我看到“player.h”被注释掉了。 不!我没有修改它。 您能指出哪一行是第 22 行(错误的来源)吗?始终让您的问题尽可能容易回答。 【参考方案1】:

错误在这一行:

typedef  void (__stdcall *LPFNTRACEPROC)(void* pUserData, const TCHAR* szTraceMsg);

自从我“愤怒”地学习 C/C++ 以来已经有一段时间了,所以我在这里有点生疏了。某处存在类型不匹配 - 事实上,正如您通过将字符串包装在 _T 宏中所发现的那样。

【讨论】:

谢谢,很难进入这样的库(对我来说)...我总是遇到编译错误...我通过发送一个像这样 MusicNoteLib 的 c 样式命令来修复它: :SaveAsMidiFile(_T("C C E C C C"), _T("Output.Mid"));但是我不知道_T代表什么。。主页上的示例似乎与可下载的代码不匹配?!? @mrbuxley - 自从我“愤怒”地执行 C/C++ 以来已经有一段时间了,所以我有点生疏了,但 _T 是 unicode 字符串的字符转换宏(我认为) . 上面引用的那一行是LPFNTRACEPROC的定义。 @Larry - 我说我已经有一段时间没有愤怒地使用 C/C++ 了!我会更新答案。【参考方案2】:

在 CPP 文件中包含 MusicNoteLib.h 之前,您需要包含声明 LPFNTRACEPROC 和 TCHAR 的标头。它可能是 Player.h,可能不应该像 ChrisF 建议的那样被注释掉,它也可能是 Windows 标头。

基本思想是您需要查看编译器抱怨的行,找出这些行上的符号在哪里声明,并包含具有这些声明的适当头文件。

【讨论】:

以上是关于c++ / c混淆的主要内容,如果未能解决你的问题,请参考以下文章

C++ fstream 混淆

C++:混淆声明语义

C++ Continue 语句混淆

如何避免名称混淆?

C++类构造函数混淆

C++ For 循环和数组混淆