LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2009, 09:07 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
c++ : multiple definition of 'namespaceName'


Hallo,
the following code hasn't got any meaning, but i'd like to know, what the error "multiple definition of 'Company2" in the given context means.

Main.cpp
Code:
#ifndef PERSON_CPP
#define PERSON_CPP
	#include "Person.cpp"
#endif

int main() {
	company::Person p;	
}
Person.h
Code:
#ifndef STRING_H
#define STRING_H
	#include <string>
#endif

using namespace std;	
	
namespace company {
	struct Person {
		string name;
		int age;
	};
}
Person.cpp
Code:
#ifndef PERSON_H
#define PERSON_H
	#include "Person.h"
#endif
#include <string>

using namespace std;
	
namespace company2 {
	
	string Person2Str(company::Person) {
		return "aperson";
	}
}
 
Old 03-10-2009, 08:18 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Please remove the multiple-inclusion guards from the .cpp files. They are not necessary, nor should they ever be used other than in a header file.

Also, do not include a .cpp file within another .cpp file; that is crude programming.

Lastly, never specify a "using namespace" in a header file.

Here's a better approach:
Person.h:
Code:
#ifndef PERSON_H
#define PERSON_H

#include <string>

namespace company
{
  struct Person
  {
    std::string name;
    int         age;
  };
}

namespace company2
{
  std::string Person2Str(const company::Person& p);
}

#endif
Person.cpp:
Code:
#include "Person.h"
#include <string>

namespace company2
{
  std::string Person2Str(const company::Person& p)
  {
    return p.name;
  }
}
Main.cpp:
Code:
#include "Person.h"
#include <string>

int main()
{
  using namespace company;

  Person p = { "Fred", 30 };

  std::string name = company2::Person2Str(p);
  ...
}
To compile multiple modules:
Code:
g++ Main.cpp Person.cpp -o myprog
-----------

EDIT: Sorry for the post-edits; your code still confuses me. But hopefully what I have above will clarify things. I have not attempted to compile the code, so you may still have trouble.

Last edited by dwhitney67; 03-10-2009 at 08:36 AM.
 
Old 03-10-2009, 10:50 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
As dwhitney67 pointed out your header guards are wrong. What I suspect is happening is that you have added Person.cpp to your project so it gets compiled and so company2 exists. Then the compiler comes to Main.cpp and it is asked to include a cpp file, something that it will do but you shouldn't (!) and so once more a company2 is created. Hence the error.

Notice how in dwhitney67 solution the header guard is just in the Person.h file and that it embraces the whole file contents.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++: gcc barfs with "Multiple definition of ...". What am I doing wrong? map250r Programming 2 02-23-2009 12:59 PM
multiple definition of lrios Programming 4 04-27-2008 10:19 PM
multiple definition of destructor? TurtleFace Programming 3 10-28-2006 07:17 AM
make multiple definition of ...?????????????? bendeco13 Programming 3 04-20-2006 04:50 PM
multiple definition ? lackluster Linux - Software 1 11-27-2003 05:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration