good day.. i am starting to learn..and experiment in namespaces

just simple one... calling void functions..then print

f.h

#include<iostream>
#include<limits>
namespace A
{
	void f()
	{
		std::cout<< "Testing \"void f()\"";
		std::cout<<std::endl;
	}
}

f.cpp

#include "f.h"

int main ()
{
	A::f();
	

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
	std::cin.get();
}

g.h

#include<iostream>
#include<limits>
namespace A
{
	void g()
	{
		std::cout<< "Testing \"void g()\"";
	}
}

g.cpp

#include "g.h"
int main ()
{
	A::g();

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
	std::cin.get();
}

errors
Error 3 fatal error LNK1169: one or more multiply defined symbols found
Error 2 error LNK2005: _main already defined in f.obj g.obj
Error 1 error LNK2005: "void __cdecl A::g(void)" (?g@A@@YAXXZ) already defined in f.obj


i already tried two headers then calling it in 1 cpp.. and it works...
but i want it in two cpp..

help me..thanks

Recommended Answers

All 5 Replies

I could be because you have 2 main() methods in the same project.

try making another .cpp file, then call your main() in g.cpp and f.cpp something else ( like main2()). Then call g.main from the new .cpp file.

hope thats makes sense.

I could be because you have 2 main() methods in the same project.

try making another .cpp file, then call your main() in g.cpp and f.cpp something else ( like main2()). Then call g.main from the new .cpp file.

hope thats makes sense.

try making another .cpp file, then call your main() in g.cpp and f.cpp something else ( like main2()). Then call g.main from the new .cpp file

how to do that?

Ok what Danny is saying that you currently have 2 mains in the same project i agree,

I would like to suggest that even for trivial examples try using more meaningful names as this will be handy later on.

What he suggests is that you currently have 2 sets of files g.h/.cpp and f.h/.cpp

In each of these change int main() to say void functionF(), and void functionG(), i suggest void as the return type as you aren't returning anything so void will be fine, if you do indeed want to return something include a return statement with a value to the function.

Then create a main.cpp file. in it include f.h and g.h and call thier functions in the new file such as below.

//main.cpp
//program main entry point.

#include "f.h"
#include "g.h"

int main(void) //program starts here when executed
{
   A::functionF();
   A::functionG();

   return(0); //bracket's not neccessary but its how i personally like to do things 
}

Now to explain why your program has problems

the compiler looks for the main function because as the name would suggest it is the main (primary most important first, whatever way you want to think of it) function. Everything is set into motion by this function, therefore by having two main functions you are causeing the compiler some headaches. It does not know what to do with two main functions, just as it does not know what to do with two functions with the same input, return type and name such as if you have, int myFn(int), and somewhere else create another int myFn(int) it does not know what to do with having two of the same name. For a real world example imagine you are the compiler (a perosn but in the compilers position) the programmer Charley asks you to say hellow to Ed, but there are two people called Ed in the room, how would you know which Ed to speak to?, the compiler has the same problem but it cant just ask charley which Ed did you mean (in a way it does, it gives the error you got) so it cannot complete the task its given.

There are ways around the same name issue such as if you declare int A in one namespace (nsOne) you can create a new namespace(nsTwo) with the same variable int A, to tell the difference you would fully qualify them such as nsOne::A, and nsTwo::A.

I hope this explains to you what the problem was, why and how to solve it in a way such as above, if you indeed wanted two functions with the same name call them something other than main, put them each in different namespaces and call them something like i have done below (function is called someFn, and using the two name spaces as above)

//inside main or somewhere

//call someFn from nsOne
returnVar1 = nsOne::someFn(inputVar);

//call someFn from nsTwo
returnVar2 = nsTwo::someFn(inputVar);

In the above code i have shown where return and input var's would go (if any) as like any function.

Think that covers every angle, any further questions or errors (im only human too) feel free to reply post here or PM me :)

Thanks for the explanation Kanoisa... it helped...:)
i understand my errors now..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.