I've got error "redefinition of class Book". Thats my code:
Book.cpp
#include "Book.h"
using namespace std;
class Book {
string author, title;
public:
Book()
{
cout<<"Book()"<<endl;
author = "-";
title = "-";
}
Book(const string& autor, const string& tytul)
{
cout<<"Book(const string& autor, const string& tytul)"<<endl;
author = autor;
title = tytul;
}
Book(string&& autor, string&& tytul)
{
cout<<"Book(Book&& autor, Book&& tytul)"<<endl;
author=autor;
title=tytul;
}
Book(const Book& other)
:author(other.author),title(other.title)
{
cout<<"Book(const Book& other)"<<endl;
}
string GetAuthor()
{
return author;
}
string GetTitle()
{
return title;
}
void SetAuthor(const string &autor)
{
cout<<"Setter ze stalymi l-ref"<<endl;
author=autor;
}
void SetTitle(const string &tytul)
{
cout<<"Setter ze stalymi l-ref"<<endl;
title=tytul;
}
void SetAuthor(string &&autor)
{
cout<<"Setter r-ref"<<endl;
author=autor;
}
void SetTitle(string &&tytul)
{
cout<<"Setter r-ref"<<endl;
title=tytul;
}
//OPERATORY
Book& operator=(const Book& right)
{
cout<<"Book& operator=(const Book& right)"<<endl;
Book tmp = right;
swap(author,tmp.author);
swap(title,tmp.title);
return *this;
}
Book& operator=(Book&& right)
{
cout<<"Book& operator=(Book&& right)"<<endl;
swap(author,right.author);
swap(title,right.title);
return *this;
}
};
Book.h
#include <string>
using namespace std;
class Book {
string author, title;
public:
Book();
Book(const string& autor, const string& tytul);
Book(string&& autor, string&& tytul);
Book(const Book& other);
string GetAuthor();
string GetTitle();
void SetAuthor(const string &autor);
void SetTitle(const string &tytul);
void SetAuthor(string &&autor);
void SetTitle(string &&tytul);
//OPERATORY
Book& operator=(const Book& right);
Book& operator=(Book&& right);
};
SOLUTIONS: after time, I can see few problems:
- Never add
using namespace std;
in header files (namespace in header files) - cpp file should define methods from header file
- declare class in header file and implement in cpp file
- class template thats how class should looks like
- when you want to define method from header file do this in proper way:
ClassName::method_type method_name (){...}