Question : "one or more multiply defined symbols found" Error in VS C++ 2010

This one I've been working on for hours and cannot figure out. I created a method inside a pair of namespaces called format date:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
#ifndef MXF_DATE_HPP
#define MXF_DATE_HPP

namespace mxf
{
	namespace date
	{
		std::string formatDate(std::string formatStr)
		{
			...
		}
	}
}

#endif


When I try and call mxf::date::formatDate("..."); inside main I keep getting this error:
stdafx.obj : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl mxf::date:: formatDate(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (? formatDate@date@mxf@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V34@@Z) already defined in Sandbox.obj
C:\Documents and Settings\Owner\My Documents\Visual Studio 2010\Projects\f\Sandbox.exe : fatal error LNK1169: one or more multiply defined symbols found


I've verifyed over and over again that the hpp is included only once and that there are no similarly named functions. Here's my main():
1:
2:
3:
4:
5:
6:
7:
int main(int argc, char* argv[])
{
	string a;
	a = mxf::date::formatDate("...");

	return 0;
}


I'm hoping someone can tell me why I'm getting this error. I must have tried a 100 different things including renaming the function, renaming the namespace, removing a namespace, etc.

Thanks for the help!

Dan

Answer : "one or more multiply defined symbols found" Error in VS C++ 2010

Although I was able to reproduce the problem by violating one of your basic principles - "I've verifyed over and over again that the hpp is included only once". I know from experience that headers have a way of creeping in unexpectedly. You didn't mention whether you checked header dependencies, so that could be an issue. By any chance, is the file that includes your header also a header file; and if so, then is that header file called in multiple locations?

The solution for me was to declare the formatDate function in the header and move the body to its own .cpp file. (You can add it to an existing .cpp file if you wish.) I think you should do this to see what happens.

The error says formatDate is defined in Sandbox. If Sandbox.cpp is the file that has the main() that you posted, then no problem. If not, then do you see formatDate there?

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
// header file - revised
namespace mxf
{
	namespace date
	{
		std::string formatDate(std::string formatStr);
	}
}

// mxf_date.cpp new file - added to project
#include <string>
#include "mxf_date.h"
std::string mxf::date::formatDate(std::string formatStr)
{
	return "abc";
}
Random Solutions  
 
programming4us programming4us