-3

Not sure what I am doing wrong but my code is this:

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"}");
} 

The error I am getting is:

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

But I do not understand how to modify the syntax properly or what I am missing. This is trying to transition a toString from Java to C++ syntax.

My header file is as follows:

#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();
}; 

if i do:

virtual std::wstring toString() override;

then i get the following error:

'MediaDescription::toString': method with override specifier 'override' did not override any base class methods
Chris Moretti
  • 585
  • 3
  • 13
  • 31
  • 5
    Dose the class definition have a `toString()` function defined? – NathanOliver Sep 02 '15 at 16:13
  • 5
    Your class doesn't have a `toString()` method listed as a member, and neither does the parent class. C++ is not Java. Almost nothing you know about Java transfers to C++. C++ classes aren't all derived from a stupid universal object base class. – Rob K Sep 02 '15 at 17:05
  • Also note that in C++ it's idiomatic to define `ostream& operator<<(ostream&, const YourClass&)`. Then, if you need a `toString` method, you can implement one in terms of `std::stringstream`. – dan04 Sep 02 '15 at 21:33

3 Answers3

2

You should probably modify the class called MediaDescription like that:

class MediaDescription {
/*code*/
public: /*or anything you need*/
    /*code*/
    std::wstring toString();
}
ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

If you specify override C++11 keyword on virtual function in a child class, its parent class MUST have same virtual function signature.

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()
{
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Richard Dally
  • 1,432
  • 2
  • 21
  • 38
-1

Another option is to add an override of std::to_string:

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

This would also be an option.

Community
  • 1
  • 1
Functino
  • 1,939
  • 17
  • 25
  • That's not a specialization; that's an override. The difference is significant, because user code is not permitted to add new functions to the `std` namespace, but is permitted to add specializations of templates in the `std` namespace. On the other hand, providing a `to_string` (or `to_wstring` as appears to be intended) in *the same namespace as the class* is a very good idea. See [When should I prefer non-member non-friend functions to member functions?](/q/7821315/4850040) – Toby Speight Sep 02 '15 at 17:55