I have a Class called Song with Inner class Creator . Below is the Song.h File. Please ignore the comments.
#ifndef SONG_H
#define SONG_H
#include <iostream>
class Song
{
public:
class Creator{
public:
//Creator& name(const std::string name){};
void name(const std::string name){};
//std::string getName(){};
};
private:
std::string mName;
};
#endif // SONG_H
and here is Song.cpp class
#include "Song.h"
using std::string;
//using Song::Creator;
//Song::Creator& Song::Creator::name(const string name)
void Song::Creator::name(const string name)
{
mName=name;
std::cout<<name;
//return *this;
}
//string Creator::getName()
//{
//return mName;
//}
and the main class
#include <iostream>
#include "Song.h"
int main()
{
Song::Creator sc;
sc.name("vickey");
//std::cout<<sc.getName();
return 0;
}
On compilation it throws
/home/vickey/qtprojects/innerClass-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Release/../innerClass/Song.cpp:6: error: redefinition of 'void Song::Creator::name(std::string)'
and if I change the name function to
void Creator::name(const string name)
{
mName=name;
std::cout<<name;
//return *this;
}
I get this error
/home/vickey/qtprojects/innerClass-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Release/../innerClass/Song.cpp:6: error: 'Creator' has not been declared
what is that I m doing wrong ? Thanks in advance.
Remove the definition from the class declaration:
change to
The
{}represents a definition.