Linker errors with Namespaces in .h and .cpp

Hi.

I have a .h like this
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
27
28
29
30
31
32
#pragma once

namespace ged
{
	namespace Data
	{
		class GString
		{
		private:
			char* mainString;
			int size;
		public:
			//GString();
			GString(const char* CstyleString = "");
			GString(const GString& copy);
			~GString();

			GString ToUpper();
			GString& ToLower();						

			void Append(const char* toAdd);
			void Append(char toAdd);

			int Find(const char charToSearch, int start) const;
			int HowManyTimes(const char charToSearch) const;
			static GString Reverse(const GString& str);
			static bool bIsPalindrome(const GString& str);

			GString& SubString(int begin, int end) const;			
		};
	}
}


and a .cpp like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "GString.h"

using std::cout;
using std::endl;
using namespace ged::Data;


		GString::GString(const char* CstyleString)
		{
		}

		GString::GString(const GString & copy)
		{
		}

		void GString::Append(const char* toAdd)
		{
		}

		void GString::Append(char toAdd)
		{
		}


When I use this class, I get linker errors: http://prntscr.com/a2vyht

This didn't happen when in the .cpp I used to write every single function like this:

int ged::Data::GString::toUpper(...)

The latter though is too confusing

How can I solve =(

check your other files, the code is correct and it works for me

look for redefinitions
Last edited on
.H

1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace ged
{
   namespace Data
   {
        class GString
        {
             friend std::ostream& operator<<(std::ostream& s, const GString& other)
             {
	         std::cout << other.mainString;
	         return s;
             }
        };
   }
}


In the cpp I don't even mention that operator overloading function.

The error says:

"class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl ged::Data::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class ged::Data::GString const &)" (??6Data@ged@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV23@ABVGString@01@@Z) already defined in main.obj

Last edited on
> http://prntscr.com/a2vyht
don't do that again.
The message is text, post it as text.

you haven't provide a body for ToUpper()

> already defined in main.obj
http://www.cplusplus.com/forum/general/140198/


> int ged::Data::GString::toUpper(...)
> The latter though is too confusing
good thing that you discovered that you may do
1
2
3
4
5
namespace ged{
   namespace Data{
      //function definitions
   }
}
Also, one can use a namespace alias, if referring to a class from elsewhere, like in main() or some other function say:

namespace gd = ged::Data; // use whatever abbreviation that suits you

Then,

gd::Gstring MyString;

That way it's easier to deal with nested namespaces and you can avoid bringing in the whole namespace.

I find myself doing this all the time when using boost:

namespace bg = boost::geometry;
Topic archived. No new replies allowed.