c++中的toString()方法
toString() method in C++?
不知道我做错了什么,但我的代码是这样的:
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
您可能应该这样修改名为MediaDescription
的类:
class MediaDescription {
/*code*/
public: /*or anything you need*/
/*code*/
std::wstring toString();
}
如果在子类的虚函数上指定了override
c++ 11关键字,则其父类必须具有相同的虚函数签名。
生活例子
#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()
{
}
另一个选项是添加覆盖std::to_string
:
#include <string>
std::string std::to_string(MediaDescription &value) {
// convert to string ...
}
这也是一个选项
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 如何在 c++ 的 toString 方法中使用 setw(x) 并将其放入变量或建议中?
- 为什么我没有使用班级的tostring方法获得输出
- c++中的toString()方法