【问题标题】:toString() method in C++?C++ 中的 toString() 方法?
【发布时间】:2015-09-02 16:12:08
【问题描述】:

不知道我做错了什么,但我的代码是这样的:

std::wstring MediaDescription::toString()
{
    std::wstring theFile = std::wstring(L"\"fileName\":\"") + mFilename + std::wstring(L"\"");
    std::wstring theAuthor = std::wstring(L"\"author\":\"") + mAuthor + std::wstring(L"\"");
    std::wstring theAlbum = std::wstring(L"\"album\":\"") + mAlbum + std::wstring(L"\"");
    std::wstring theGenre = std::wstring(L"\"genre\":\"") + mGenre + std::wstring(L"\"");
    std::wstring theType = std::wstring(L"\"mediaType\":") + mMediaType;
    std::wstring theTitle = std::wstring(L"\"title\":\"") + mTitle + std::wstring(L"\"");
    return std::wstring(L"{") + theFile + std::wstring(L",") + theAuthor + std::wstring(L",") + theAlbum + std::wstring(L",") + theGenre + std::wstring(L",") + theType + std::wstring(L",") + theTitle +     std::wstring(L"}");
} 

我得到的错误是:

 class:"MediaDescription" has no member of "toString"

但我不明白如何正确修改语法或我缺少什么。这是试图将 toString 从 Java 语法转换为 C++ 语法。

我的头文件如下:

#pragma once

#include <iostream>
#include <memory>
#include <string>

class MediaDescription : public std::enable_shared_from_this<MediaDescription>
{

/// <summary>
/// A media type of either Music or Video.
/// </summary>
private:
const std::wstring mMediaType;
/// <summary>
/// A title of a song or a video/movie.
/// </summary>
const std::wstring mTitle;
/// <summary>
/// A name for the author/actor/actress of the media.
/// </summary>
const std::wstring mAuthor;
/// <summary>
/// A name for the album of the song.
/// </summary>
const std::wstring mAlbum;
/// <summary>
/// A genre of the video.
/// </summary>
const std::wstring mGenre;
/// <summary>
/// A filename of the media
/// </summary>
const std::wstring mFilename;

public:
MediaDescription();

/// <summary>
/// MediaDescription constructor. </summary>
/// <param name="mediaType"> a media type of music or video </param>
/// <param name="title"> the title of the media </param>
/// <param name="author"> the author of the media </param>
/// <param name="album"> the album only applying to music </param>
/// <param name="genre"> the genre of the media </param>
/// <param name="filename"> the filename of the media </param>
MediaDescription(const std::wstring &mediaType, const std::wstring &title, const std::wstring &author, const std::wstring &album, const std::wstring &genre, const std::wstring &filename);

/// <summary>
/// Returns a media type of either music or video. </summary>
/// <returns> a media type. </returns>
virtual std::wstring getMediaType();

/// <summary>
/// Returns a title of a song or video/movie.
/// </summary>
/// <returns> a title of the media. </returns>
virtual std::wstring getTitle();

/// <summary>
/// Returns a name for the author of the media or leading actor/actress of video.
/// </summary>
/// <returns> a name of the author/actor/actress </returns>
virtual std::wstring getAuthor();

/// <summary>
/// Returns a name for the album of the song.
/// </summary>
/// <returns> a name of the album. </returns>
virtual std::wstring getAlbum();   

/// <summary>
/// Returns a genre of the video/movie.
/// </summary>
/// <returns> a genre of the video. </returns>
virtual std::wstring getGenre();

/// <summary>
/// Returns a filename of the media file.
/// </summary>
/// <returns> a filename of the media. </returns>
virtual std::wstring getFilename();
}; 

如果我这样做:

virtual std::wstring toString() override;

然后我得到以下错误:

'MediaDescription::toString': method with override specifier 'override' did not override any base class methods

【问题讨论】:

  • 类定义是否定义了toString() 函数?
  • 您的类没有将toString() 方法列为成员,父类也没有。 C++ 不是 Java。您几乎对 Java 向 C++ 的迁移一无所知。 C++ 类并非都派生自愚蠢的通用对象基类。
  • 另请注意,在 C++ 中定义ostream&amp; operator&lt;&lt;(ostream&amp;, const YourClass&amp;) 是惯用的。然后,如果您需要toString 方法,您可以根据std::stringstream 实现一个。

标签: c++ tostring


【解决方案1】:

您可能应该像这样修改名为MediaDescription 的类:

class MediaDescription {
/*code*/
public: /*or anything you need*/
    /*code*/
    std::wstring toString();
}

【讨论】:

    【解决方案2】:

    如果您在子类的虚函数上指定overrideC++11 关键字,则其父类必须具有相同的虚函数签名。

    Live example

    #include <iostream>
    using namespace std;
    
    struct Parent
    {
        virtual void DoWork1() {}
        void DoWork2() {}
    };
    
    struct Child : Parent
    {
        void DoWork1() override {} // OK !
        void DoWork1(int) override {} // error !
    
        void DoWork2() override {} // error !
    };
    
    int main()
    {
    }
    

    【讨论】:

      【解决方案3】:

      另一种选择是添加std::to_string的覆盖:

      #include <string>
      std::string std::to_string(MediaDescription &value) {
          // convert to string ...
      }
      

      This 也是一个选项。

      【讨论】:

      • 那不是专业;这是一个覆盖。区别很明显,因为不允许用户代码向std 命名空间添加新功能,但允许在std 命名空间中添加模板的特化。另一方面,在与类相同的命名空间中提供to_string(或to_wstring)是一个非常好的主意。见When should I prefer non-member non-friend functions to member functions?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2014-12-26
      相关资源
      最近更新 更多