2015-09-02 30 views
-3

不知道我做錯了,但我的代碼是這樣的:toString()方法在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 
+5

劑量類定義具有'的toString()'函數定義? – NathanOliver

+5

你的類沒有列出成員的'toString()'方法,父類也沒有。 C++不是Java。幾乎沒有關於Java轉移到C++的知識。 C++類並不都是從一個愚蠢的通用對象基類派生的。 –

+0

另請注意,在C++中,它定義了'ostream&operator <<(ostream&,const YourClass&)'。然後,如果你需要'toString'方法,你可以用'std :: stringstream'來實現。 – dan04

回答

2

你或許應該修改調用的類MediaDescription那樣:

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

另一種選擇是增加的std::to_string一個覆蓋:

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

This也將是一個選項。

+0

這不是專業化;這是一個覆蓋。區別很大,因爲用戶代碼不允許向'std'命名空間添加新的函數,但是允許在'std'命名空間中添加模板的特化。另一方面,在與*類相同的名稱空間中提供'to_string'(或'to_wstring')看來是個不錯的主意。參見[我應該何時更喜歡非會員非朋友功能?](/ q/7821315/4850040) –

0

如果您在子類中的虛函數中指定了override C++ 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() 
{ 
}