Class problem: multiple definition of

Sep 18, 2009 at 4:24am
Hi there!, I have created a simple project with this header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Book.h

#ifndef BOOK_H_
#define BOOK_H_

#include <string>
	using std::string;

class Book{

	public:
		Book(string name);
		void setName(string name);
		string getName();
		void printMessage();

	private:
		 string courseName;
};
#endif /* BOOK_H_ */

---and this .cpp file---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Book.cpp

#include <iostream>
 using std::cout;
 using std::endl;

#include "Book.h"


 Book::Book(string name){
	 setName(name);
 }

 void Book::setName(string name){
	 courseName = name;
 }

 string Book::getName(){
	 return courseName;
 }

 void Book::printMessage(){
	 cout <<"Welcome to\n" << getName()
		  <<"!" << endl;
 }

When I compile I got these errors
multiple definition of `Book::Book(std::string)'
multiple definition of `Book::getName(std::string)'
multiple definition of `Book::setName(std::string)'

I don't know where the problem lies...thanks in advanced
Sep 18, 2009 at 10:56am
Are these the only file you have?
Sep 18, 2009 at 1:05pm
Yes, please post other source files along with information on how you are compiling your project.

Completely unrelated to that, I strongly encourage you NOT to:
a) Write "using" clauses in header files; (line 7)
b) Include additional headers AFTER writing any "using" clauses in your .cpp files (lines 4-5)

Sep 18, 2009 at 11:11pm
Hi again!!

Well thanks both for try to help me...jsmith I follow your advices (B) and then magically it worked (but I don't know why I must do it in that way).

I have a question know...how can I ommit the using clause in my header file?...I try in this way (std::string) in lines 12, 13 , 14 and 18...but I get errors.

Thanks again

Sep 19, 2009 at 2:39am
You should just be able to prefix all the strings with std:: and then remove the
using std::string (but keep the include). Is that not working for you?

Sep 20, 2009 at 4:07am
Right was my mistake......thank you for be so patient!!
Topic archived. No new replies allowed.